#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=2e5+7;
int a[maxn];
int d[maxn];
int n,q;
struct segtree {
struct info{
int border[2];
int dp[2][2];
};
vector<info> t;
int n;
segtree(){}
segtree(int n) : n(n),t(4 * n){}
info combine(int x){
info ret;
info le=t[x*2];
info ri=t[x*2+1];
ret.border[0]=le.border[0];
ret.border[1]=ri.border[1];
ret.dp[0][0]=ret.dp[1][0]=ret.dp[0][1]=ret.dp[1][1]=0;
for(int l=0;l<2;l++){
for(int m=0;m<2;m++){
for(int o=0;o<2;o++){
for(int r=0;r<2;r++){
if(m&&o){
if((le.border[1]<0)==(ri.border[0]<0))
ret.dp[l][r]=max(ret.dp[l][r],le.dp[l][m]+ri.dp[o][r]);
}
else{
ret.dp[l][r]=max(ret.dp[l][r],le.dp[l][m]+ri.dp[o][r]);
}
}
}
}
}
return ret;
}
void build()
{
build(1,1,n-1);
}
void build(int id,int l,int r){
if(l==r){
t[id].border[0]=d[l];
t[id].border[1]=d[l];
t[id].dp[0][1]=t[id].dp[1][0]=t[id].dp[0][0]=0;
t[id].dp[1][1]=abs(d[l]);
return;
}
int m=(l+r)/2;
build(id*2,l,m);
build(id*2+1,m+1,r);
t[id]=combine(id);
}
void upd(int pos,int val){
upd(1,1,n-1,pos,val);
}
void upd(int id,int l,int r,int pos,int val){
if(l==r){
t[id].border[0]+=val;
t[id].border[1]+=val;
t[id].dp[1][1]=abs(t[id].border[0]);
return;
}
int m=(l+r)/2;
if(pos<=m)upd(id*2,l,m,pos,val);
else upd(id*2+1,m+1,r,pos,val);
t[id]=combine(id);
}
int qry()
{
return t[1].dp[1][1];
}
};
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++)d[i]=a[i+1]-a[i];
segtree t(n);
t.build();
// cout<<t.qry()<<"\n";
while(q--){
int l,r,x;
cin>>l>>r>>x;
if(l-1>=1)t.upd(l-1,x);
if(r<=n-1)t.upd(r,-x);
cout<<t.qry()<<"\n";
}
return 0;
}
Compilation message
Main.cpp: In constructor 'segtree::segtree(long long int)':
Main.cpp:15:6: warning: 'segtree::n' will be initialized after [-Wreorder]
15 | int n;
| ^
Main.cpp:14:18: warning: 'std::vector<segtree::info> segtree::t' [-Wreorder]
14 | vector<info> t;
| ^
Main.cpp:17:2: warning: when initialized here [-Wreorder]
17 | segtree(int n) : n(n),t(4 * n){}
| ^~~~~~~
Main.cpp: At global scope:
Main.cpp:79:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
79 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 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 |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 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 |
2516 KB |
Output is correct |
7 |
Correct |
5 ms |
3164 KB |
Output is correct |
8 |
Correct |
4 ms |
3108 KB |
Output is correct |
9 |
Correct |
4 ms |
3164 KB |
Output is correct |
10 |
Correct |
5 ms |
3164 KB |
Output is correct |
11 |
Correct |
5 ms |
3164 KB |
Output is correct |
12 |
Correct |
5 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 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 |
2516 KB |
Output is correct |
7 |
Correct |
5 ms |
3164 KB |
Output is correct |
8 |
Correct |
4 ms |
3108 KB |
Output is correct |
9 |
Correct |
4 ms |
3164 KB |
Output is correct |
10 |
Correct |
5 ms |
3164 KB |
Output is correct |
11 |
Correct |
5 ms |
3164 KB |
Output is correct |
12 |
Correct |
5 ms |
3164 KB |
Output is correct |
13 |
Correct |
453 ms |
50292 KB |
Output is correct |
14 |
Correct |
408 ms |
50260 KB |
Output is correct |
15 |
Correct |
415 ms |
50112 KB |
Output is correct |
16 |
Correct |
392 ms |
50004 KB |
Output is correct |
17 |
Correct |
412 ms |
50104 KB |
Output is correct |
18 |
Correct |
325 ms |
50772 KB |
Output is correct |