This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5;
int n, q, arr[N + 5];
struct info {
int mx[2][2] = {}, first, last;
info operator+(info x) {
info ans;
ans.first = first, ans.last = x.last;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
ans.mx[i][l] = max(ans.mx[i][l], mx[i][j] + x.mx[k][l] + (j == 1 && k == 1 ? abs(last - x.first) : 0));
}
}
}
}
return ans;
}
};
info tree[4*N + 5];
int lazy[4*N + 5];
void init(int v, int tl, int tr) {
if (tl == tr) tree[v].first = tree[v].last = arr[tl];
else {
int tm = (tl + tr)/2;
init(v*2, tl, tm);
init(v*2 + 1, tm + 1, tr);
tree[v] = tree[v*2] + tree[v*2 + 1];
}
}
void extend(int v, int tl, int tr) {
tree[v*2].first += lazy[v], tree[v*2].last += lazy[v];
tree[v*2 + 1].first += lazy[v], tree[v*2 + 1].last += lazy[v];
lazy[v*2] += lazy[v], lazy[v*2 + 1] += lazy[v];
lazy[v] = 0;
}
void update(int v, int tl, int tr, int l, int r, int x) {
if (l > r) return;
else if (tl == l && tr == r) tree[v].first += x, tree[v].last += x, lazy[v] += x;
else {
extend(v, tl, tr);
int tm = (tl + tr)/2;
update(v*2, tl, tm, l, min(tm, r), x);
update(v*2 + 1, tm + 1, tr, max(l, tm + 1), r, x);
tree[v] = tree[v*2] + tree[v*2 + 1];
}
}
info query(int v, int tl, int tr, int l, int r) {
if (tl == l && tr == r) return tree[v];
else {
extend(v, tl, tr);
int tm = (tl + tr)/2;
bool lef = l <= min(tm, r), rig = max(l, tm + 1) <= r;
if (lef && rig) return query(v*2, tl, tm, l, min(tm, r)) + query(v*2 + 1, tm + 1, tr, max(l, tm + 1), r);
else if (lef) return query(v*2, tl, tm, l, min(tm, r));
else return query(v*2 + 1, tm + 1, tr, max(l, tm + 1), r);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> arr[i];
init(1, 1, n);
while (q--) {
int l, r, x;
cin >> l >> r >> x;
update(1, 1, n, l, r, x);
cout << max({tree[1].mx[0][0], tree[1].mx[0][1], tree[1].mx[1][0], tree[1].mx[1][1]}) << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |