yhylivedream @ 2024-04-02 12:58:12
//author : yhy
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<LL, LL>;
const LL kMaxN = 1e6 + 5, inf = -1e18;
LL n, q, a[kMaxN];
struct Node {
LL maxv, add, mdf;
} t[kMaxN << 2];
void push_up(LL cur) {
t[cur].maxv = max(t[cur * 2].maxv, t[cur * 2 + 1].maxv);
}
void addtag(LL cur, LL x, LL ty) {
if (ty == 1) {
t[cur] = {x, 0, x};
} else {
t[cur].maxv += x, (t[cur].mdf != inf ? t[cur].mdf += x : t[cur].add += x);
}
}
void push_down(LL cur) {
if (!t[cur].add && t[cur].mdf == inf) {
return;
}
addtag(cur * 2, t[cur].mdf, 1), addtag(cur * 2 + 1, t[cur].mdf, 1);
addtag(cur * 2, t[cur].add, 2), addtag(cur * 2 + 1, t[cur].add, 2);
t[cur].add = 0, t[cur].mdf = inf;
}
void build (LL cur, LL l, LL r) {
t[cur].add = 0, t[cur].mdf = inf;
if (l == r) {
t[cur].maxv = a[l];
return;
}
LL mid = (l + r) / 2;
build(cur * 2, l, mid), build(cur * 2 + 1, mid + 1, r);
push_up(cur);
}
void update(LL cur, LL l, LL r, LL qx, LL qy, LL val, LL ty) {
if (qx <= l && r <= qy) {
addtag(cur, val, ty);
return;
} if (r < qx || l > qy) {
return;
}
push_down(cur);
LL mid = (l + r) / 2;
update(cur * 2, l, mid, qx, qy, val, ty);
update(cur * 2 + 1, mid + 1, r, qx, qy, val, ty);
push_up(cur);
}
LL query (LL cur, LL l, LL r, LL qx, LL qy) {
if (qx <= l && r <= qy) {
return t[cur].maxv;
} if (r < qx || l > qy) {
return -inf;
}
push_down(cur);
LL mid = (l + r) / 2;
return max(query(cur * 2, l, mid, qx, qy), query(cur * 2 + 1, mid + 1, r, qx, qy));
}
signed main () {
ios::sync_with_stdio(0), cin.tie(nullptr);
cin >> n >> q;
for (LL i = 1; i <= n; i++) {
cin >> a[i];
}
build(1, 1, n);
for (LL ty, x, y, val; q; q--) {
cin >> ty >> x >> y;
if (ty != 3) {
cin >> val;
update(1, 1, n, x, y, val, ty);
} else {
cout << query(1, 1, n, x, y) << '\n';
}
}
return 0;
}
by frotms @ 2024-04-02 13:00:19
666,打线段树是吧
by yhylivedream @ 2024-04-02 14:23:19
已过,此贴结。