#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
struct node{
ll val[2][2] = {};
ll lval = 0, rval = 0;
node() {}
node(ll x){
lval = rval = x;
val[0][0] = 0;
val[1][0] = 0;
val[0][1] = 0;
val[1][1] = abs(x);
}
void upd(ll v){
lval += v;
rval += v;
val[1][1] = abs(lval);
}
};
struct segtree{
node op(node &l, node &r){
node res;
res.lval = l.lval;
res.rval = r.rval;
for(int ll = 0; ll < 2; ++ll){
for(int lr = 0; lr < 2; ++lr){
for(int rl = 0; rl < 2; ++rl){
for(int rr = 0; rr < 2; ++rr){
if(lr && rl){
if((l.rval <= 0 && r.lval <= 0) || (l.rval >= 0 && r.lval >= 0)){
res.val[ll][rr] = max(res.val[ll][rr], l.val[ll][lr] + r.val[rl][rr]);
}
}else{
res.val[ll][rr] = max(res.val[ll][rr], l.val[ll][lr] + r.val[rl][rr]);
}
}
}
}
}
return res;
}
node cur;
int L, R, m;
segtree *left, *right;
segtree(int l, int r, vector<node> &num){
L = l;
R = r;
m = (l + r) / 2;
if(r - l == 1){
cur = num[l];
return;
}
left = new segtree(l, m, num), right = new segtree(m, r, num);
cur = op(left->cur, right->cur);
}
void update(int i, ll v){
if(R - L == 1){
cur.upd(v);
return;
}
if(i < m){
left->update(i, v);
}else{
right->update(i, v);
}
cur = op(left->cur, right->cur);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<ll> v(n);
vector<node> a(n - 1);
for(int i = 0; i < n; ++i) cin >> v[i];
for(int i = 0; i < n - 1; ++i) a[i] = node(v[i + 1] - v[i]);
segtree sg(0, n - 1, a);
while(q--){
int l, r;
ll x;
cin >> l >> r >> x;
l--;
if(l - 1 >= 0){
sg.update(l - 1, x);
}
if(r < n){
sg.update(r - 1, -x);
}
cout << sg.cur.val[1][1] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
1108 KB |
Output is correct |
8 |
Correct |
7 ms |
1108 KB |
Output is correct |
9 |
Correct |
6 ms |
1108 KB |
Output is correct |
10 |
Correct |
7 ms |
1184 KB |
Output is correct |
11 |
Correct |
7 ms |
1108 KB |
Output is correct |
12 |
Correct |
6 ms |
1188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
6 ms |
1108 KB |
Output is correct |
8 |
Correct |
7 ms |
1108 KB |
Output is correct |
9 |
Correct |
6 ms |
1108 KB |
Output is correct |
10 |
Correct |
7 ms |
1184 KB |
Output is correct |
11 |
Correct |
7 ms |
1108 KB |
Output is correct |
12 |
Correct |
6 ms |
1188 KB |
Output is correct |
13 |
Correct |
724 ms |
57932 KB |
Output is correct |
14 |
Correct |
707 ms |
57936 KB |
Output is correct |
15 |
Correct |
688 ms |
57984 KB |
Output is correct |
16 |
Correct |
678 ms |
57804 KB |
Output is correct |
17 |
Correct |
689 ms |
57752 KB |
Output is correct |
18 |
Correct |
650 ms |
58572 KB |
Output is correct |