chenfy27的刷题记录

导航

luoguP4513 小白逛公园

有n个公园,小白对第i个公园的评分为A[i],有m次操作:

  • 1 a b 表示在[a,b]范围内选择一段连续的公园遛狗;
  • 2 a b 表示小白对公园a的评分修改为b;

对于操作1,输出可以取得的最大评分。

分析:线段树维护区间子段和。

#include <bits/stdc++.h>
using llong = long long;

const int inf = 1e9;

// SegmentTree模板...

struct Info {
    int pre;
    int max;
    int suf;
    int sum;
    Info(int s=-inf) {
        pre = max = suf = sum = s;
    }
    friend Info operator+(const Info &a, const Info &b) {
        if (a.max == -inf) return b;
        if (b.max == -inf) return a;
        Info ans;
        ans.pre = std::max(a.pre, a.sum + b.pre);
        ans.suf = std::max(b.suf, a.suf + b.sum);
        ans.sum = a.sum + b.sum;
        ans.max = std::max({a.max, b.max, a.suf + b.pre});
        return ans;
    }
    friend std::ostream& operator<<(std::ostream &out, Info &info) {
        out << "info:(" << ")";
        return out;
    }
};

void solve() {
    int n, m;
    std::cin >> n >> m;
    std::vector<int> A(n);
    for (int i = 0; i < n; i++) {
        std::cin >> A[i];
    }
    SegmentTree<Info> tr(A);
    for (int i = 0; i < m; i++) {
        int k, a, b;
        std::cin >> k >> a >> b;
        if (k == 1) {
            if (a > b) std::swap(a, b);
            std::cout << tr.rangeQuery(a-1, b).max << "\n";
        } else if (k == 2) {
            tr.assign(a-1, b);
        }
    }
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}

posted on 2024-07-14 11:54  chenfy27  阅读(22)  评论(0)    收藏  举报