#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int n, q;
ll d[N], a[N];
ll it[4 * N][2][2];
ll all(int s){
ll ans = 0;
for (int i = 0; i <= 1; ++ i){
for (int j = 0; j <= 1; ++ j) ans = max(ans, it[s][i][j]);
}
return ans;
}
bool trai(ll a, ll b){
return (a == 0 || b == 0 || (a > 0 && b < 0) || (a < 0 && b > 0));
}
void mer(int s, int mid){
for (int i = 0; i <= 1; ++ i){
for (int j = 0; j <= 1; ++ j) it[s][i][j] = 0;
}
for (int i = 0; i <= 1; ++ i){
for (int j = 0; j <= 1; ++ j){
for (int u = 0; u <= 1; ++ u){
if (u & j && trai(d[mid], d[mid + 1])) continue;
for (int v = 0; v <= 1; ++ v){
it[s][i][v] = max(it[s][i][v], it[2 * s][i][j] + it[2 * s + 1][u][v]);
}
}
}
}
}
void build(int s, int l, int r){
if (l == r){
it[s][1][1] = abs(d[l]);
return;
}
int mid = l + r >> 1;
build(2 * s, l, mid);
build(2 * s + 1, mid + 1, r);
mer(s, mid);
}
void upd(int s, int l, int r, int u){
if (l == r){
it[s][1][1] = abs(d[l]);
return;
}
int mid = l + r >> 1;
if (mid >= u) upd(2 * s, l, mid, u);
else upd(2 * s + 1, mid + 1, r, u);
mer(s, mid);
}
void solve(){
cin >> n >> q;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
if (i > 1) d[i - 1] = a[i] - a[i - 1];
}
build(1, 1, n - 1);
while (q --){
int l, r, x;
cin >> l >> r >> x;
if (l > 1) {
d[l - 1] += x;
upd(1, 1, n - 1, l - 1);
}
if (r < n) {
d[r] -= x;
upd(1, 1, n - 1, r);
}
cout << all(1) << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:47:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'void upd(int, int, int, int)':
Main.cpp:59:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
5 ms |
4700 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
4 ms |
4700 KB |
Output is correct |
10 |
Correct |
4 ms |
4752 KB |
Output is correct |
11 |
Correct |
4 ms |
4700 KB |
Output is correct |
12 |
Correct |
4 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
5 ms |
4700 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
4 ms |
4700 KB |
Output is correct |
10 |
Correct |
4 ms |
4752 KB |
Output is correct |
11 |
Correct |
4 ms |
4700 KB |
Output is correct |
12 |
Correct |
4 ms |
4952 KB |
Output is correct |
13 |
Correct |
355 ms |
26980 KB |
Output is correct |
14 |
Correct |
347 ms |
29524 KB |
Output is correct |
15 |
Correct |
368 ms |
29008 KB |
Output is correct |
16 |
Correct |
390 ms |
29180 KB |
Output is correct |
17 |
Correct |
328 ms |
29016 KB |
Output is correct |
18 |
Correct |
302 ms |
29776 KB |
Output is correct |