#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) build(2 * s, l, mid);
else build(2 * s + 1, mid + 1, r);
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 |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2392 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
3 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2392 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
3 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2392 KB |
Output is correct |
7 |
Correct |
220 ms |
4700 KB |
Output is correct |
8 |
Correct |
262 ms |
4840 KB |
Output is correct |
9 |
Correct |
227 ms |
4840 KB |
Output is correct |
10 |
Correct |
235 ms |
4700 KB |
Output is correct |
11 |
Correct |
231 ms |
4840 KB |
Output is correct |
12 |
Correct |
172 ms |
4840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2392 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
3 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2392 KB |
Output is correct |
7 |
Correct |
220 ms |
4700 KB |
Output is correct |
8 |
Correct |
262 ms |
4840 KB |
Output is correct |
9 |
Correct |
227 ms |
4840 KB |
Output is correct |
10 |
Correct |
235 ms |
4700 KB |
Output is correct |
11 |
Correct |
231 ms |
4840 KB |
Output is correct |
12 |
Correct |
172 ms |
4840 KB |
Output is correct |
13 |
Execution timed out |
2040 ms |
22764 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |