#include<bits/stdc++.h>
#define int long long
using namespace std;
using ll = long long;
const int N = 2e5+5;
const ll inf = 1e16;
int n, q, a[N], sub[N], dp[3][N], st[N<<2][2][2];
int get_dp()
{
// dp[0]: sum of segment
// dp[1]: creating segment min -> max
// dp[2]: creating segment max -> min
dp[0][0] = 0;
dp[1][0] = dp[2][0] = -inf;
for(int i = 1; i <= n; i++){
dp[0][i] = max({dp[0][i-1], dp[1][i-1] + a[i], dp[2][i-1] - a[i]});
dp[1][i] = max(dp[1][i-1], dp[0][i-1] - a[i]);
dp[2][i] = max(dp[2][i-1], dp[0][i-1] + a[i]);
}
return dp[0][n];
}
//[0][0]: has the different sign with both start and end
//[0][1]: has the different sign with start and same with end
//[1][0]: has the same sign with start and different with end
//[1][1]: has the same sign with both start and end
void update(int id, int l, int r, int x){
if(l == r){
st[id][1][1] = (sub[l] < 0 ? -sub[l] : sub[l]);
return;
}
else{
int m = l+r>>1;
if(x <= m) update(id<<1, l, m, x);
else
update(id<<1|1, m+1, r, x);
// if mid sign and mid+1 sign is different
if(sub[m] * sub[m+1] < 0){
// our task is to make sure that mid sign and mid+1 sign always the same
st[id][0][0] = max(st[id<<1][0][1] + st[id<<1|1][0][0], st[id<<1][0][0] + st[id<<1|1][1][0]);
st[id][0][1] = max(st[id<<1][0][1] + st[id<<1|1][0][1], st[id<<1][0][0] + st[id<<1|1][1][1]);
st[id][1][0] = max(st[id<<1][1][1] + st[id<<1|1][0][0], st[id<<1][1][0] + st[id<<1|1][1][0]);
st[id][1][1] = max(st[id<<1][1][1] + st[id<<1|1][0][1], st[id<<1][1][0] + st[id<<1|1][1][1]);
}
else{
// we can take anything from [L,MID], [R,MID]
st[id][0][0] = max(st[id<<1][0][1], st[id<<1][0][0]) + max(st[id<<1|1][1][0], st[id<<1|1][0][0]);
st[id][0][1] = max(st[id<<1][0][1], st[id<<1][0][0]) + max(st[id<<1|1][1][1], st[id<<1|1][0][1]);
st[id][1][0] = max(st[id<<1][1][1], st[id<<1][1][0]) + max(st[id<<1|1][1][0], st[id<<1|1][0][0]);
st[id][1][1] = max(st[id<<1][1][1], st[id<<1][1][0]) + max(st[id<<1|1][1][1], st[id<<1|1][0][1]);
}
}
return;
}
void solve()
{
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(i-1){
sub[i] = a[i] - a[i-1];
update(1, 2, n, i);
}
}
while(q--){
int l,r,x; cin >> l >> r >> x;
sub[l] += x;
update(1, 2, n, l);
if(r+1 <= n){
sub[r+1] -= x;
update(1, 2, n, r+1);
}
cout << max({st[1][0][0], st[1][0][1], st[1][1][0], st[1][1][1]}) <<"\n";
}
return;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void update(long long int, long long int, long long int, long long int)':
Main.cpp:41:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int m = l+r>>1;
| ~^~
Main.cpp: In function 'int32_t main()':
Main.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2548 KB |
Output is correct |
4 |
Correct |
1 ms |
2436 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2548 KB |
Output is correct |
4 |
Correct |
1 ms |
2436 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
4 ms |
4688 KB |
Output is correct |
8 |
Correct |
3 ms |
4688 KB |
Output is correct |
9 |
Correct |
4 ms |
4688 KB |
Output is correct |
10 |
Correct |
4 ms |
4688 KB |
Output is correct |
11 |
Correct |
4 ms |
4688 KB |
Output is correct |
12 |
Correct |
4 ms |
4804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2548 KB |
Output is correct |
4 |
Correct |
1 ms |
2436 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
4 ms |
4688 KB |
Output is correct |
8 |
Correct |
3 ms |
4688 KB |
Output is correct |
9 |
Correct |
4 ms |
4688 KB |
Output is correct |
10 |
Correct |
4 ms |
4688 KB |
Output is correct |
11 |
Correct |
4 ms |
4688 KB |
Output is correct |
12 |
Correct |
4 ms |
4804 KB |
Output is correct |
13 |
Correct |
240 ms |
30024 KB |
Output is correct |
14 |
Correct |
230 ms |
30164 KB |
Output is correct |
15 |
Correct |
258 ms |
30024 KB |
Output is correct |
16 |
Correct |
278 ms |
30028 KB |
Output is correct |
17 |
Correct |
226 ms |
30036 KB |
Output is correct |
18 |
Correct |
227 ms |
30792 KB |
Output is correct |