xiangzhenze611 @ 2024-08-31 22:00:58
代码:
#include <iostream>
#define ll long long
using namespace std;
const int maxn = 1000001;
ll lzy1[maxn * 4], lzy2[maxn * 4], tree[maxn * 4];
int arr[maxn];
void make_tag1(int u, int len, int x) {
lzy1[u] = x;
lzy2[u] = 0;
tree[u] = x;
}
void make_tag2(int u, int len, int x) {
lzy2[u] += x;
tree[u] += x;
}
void pushdown(int u, int L, int R) {
int m = (L + R) / 2;
if (lzy1[u]) {
make_tag1(u * 2, m - L + 1, lzy1[u]);
make_tag1(u * 2 + 1, R - m, lzy1[u]);
}else {
make_tag2(u * 2, m - L + 1, lzy2[u]);
make_tag2(u * 2 + 1, R - m, lzy2[u]);
}
lzy1[u] = lzy2[u] = 0;
}
bool inRange(int L, int R, int l, int r) {
return l <= L && R <= r;
}
bool outofRange(int L, int R, int l, int r) {
return R < l || L > r;
}
ll query(int u, int L, int R, int l, int r) {
if (inRange(L, R, l, r)) return tree[u];
else if (!outofRange(L, R, l, r)) {
int m = (L + R) / 2;
pushdown(u, L, R);
return max(query(u * 2, L, m, l, r), query(u * 2 + 1, m + 1, R, l, r));
}else return 0;
}
void pushup(int u) {
tree[u] = max(tree[u * 2], tree[u * 2 + 1]);
}
void updata1(int u, int L, int R, int l, int r, int x) {
if (inRange(L, R, l, r)) make_tag1(u, R - L + 1, x);
else if (!outofRange(L, R, l, r)) {
int m = (L + R) / 2;
pushdown(u, L, R);
updata1(u * 2, L, m, l, r, x);
updata1(u * 2 + 1, m + 1, R, l, r, x);
pushup(u);
}else return;
}
void updata2(int u, int L, int R, int l, int r, ll x) {
if (inRange(L, R, l, r)) make_tag2(u, R - L + 1, x);
else if (!outofRange(L, R, l, r)) {
int m = (L + R) / 2;
pushdown(u, L, R);
updata2(u * 2, L, m, l, r, x);
updata2(u * 2 + 1, m + 1, R, l, r, x);
pushup(u);
}else return;
}
void build(int u, int L, int R) {
if (L == R) {
tree[u] = arr[L];
return;
}
int m = (L + R) / 2;
build(u * 2, L, m);
build(u * 2 + 1, m + 1, R);
pushup(u);
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> arr[i];
build(1, 1, n);
while (m--) {
int op, x, y;
cin >> op >> x >> y;
if (op == 1) {
int k;
cin >> k;
updata1(1, 1, n, x, y, k);
}else if (op == 2) {
ll k;
cin >> k;
updata2(1, 1, n, x, y, k);
}else cout << query(1, 1, n, x, y) << "\n";
}
return 0;
}
by Ivan422 @ 2024-08-31 22:05:23
注意答案可以为负数。
区间查询答案不在区间内应为