#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 << max(max(seg[1][0][0], seg[1][0][1]), max(seg[1][1][0], seg[1][1][1]));
if (q > 0) cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
880 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
4 ms |
860 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
880 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
4 ms |
860 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
294 ms |
29156 KB |
Output is correct |
14 |
Correct |
308 ms |
29136 KB |
Output is correct |
15 |
Correct |
295 ms |
29092 KB |
Output is correct |
16 |
Correct |
295 ms |
29012 KB |
Output is correct |
17 |
Correct |
281 ms |
28804 KB |
Output is correct |
18 |
Correct |
254 ms |
29524 KB |
Output is correct |