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;
const int N = 2e6 + 7;
const int inf = 1e9 + 7;
const long long infll = 1e18 + 7;
int n, q;
long long a[N], b[N];
long long seg[4 * N][2][2];
void update(int id, int l, int r, int pos, int v) {
if (r < pos || l > pos) return;
if (l == r) {
if (l == pos) b[l] += v;
seg[id][1][1] = abs(b[l]);
seg[id][0][0] = 0;
return;
}
int mid = (l + r) / 2;
update(id * 2, l, mid, pos, v);
update(id * 2 + 1, mid + 1, r, pos, v);
int x = id * 2, y = id * 2 + 1;
//merge two segment;
seg[id][0][0] = max(seg[x][0][0] + seg[y][0][0], max(seg[x][0][1] + seg[y][0][0], seg[x][0][0] + seg[y][1][0]));
seg[id][1][0] = max(seg[x][1][0] + seg[y][0][0], max(seg[x][1][1] + seg[y][0][0], seg[x][1][0] + seg[y][1][0]));
seg[id][0][1] = max(seg[x][0][0] + seg[y][0][1], max(seg[x][0][1] + seg[y][0][1], seg[x][0][0] + seg[y][1][1]));
seg[id][1][1] = max(seg[x][1][0] + seg[y][0][1], max(seg[x][1][1] + seg[y][0][1], seg[x][1][0] + seg[y][1][1]));
if ((b[mid] < 0 && b[mid + 1] < 0) || (b[mid] > 0 && b[mid + 1] > 0)) {
seg[id][0][0] = max(seg[id][0][0], seg[x][0][1] + seg[y][1][0]);
seg[id][1][0] = max(seg[id][0][0], seg[x][1][1] + seg[y][1][0]);
seg[id][0][1] = max(seg[id][0][0], seg[x][0][1] + seg[y][1][1]);
seg[id][1][1] = max(seg[id][0][0], seg[x][1][1] + seg[y][1][1]);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
update(1, 1, n - 1, i, a[i] - a[i + 1]);
}
while (q--) {
int l, r;
long long v;
cin >> l >> r >> v;
update(1, 1, n - 1, l - 1, -v);
update(1, 1, n - 1, r, v);
cout << seg[1][1][1];
if (q > 0) cout << '\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... |