#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define ff first
#define ss second
#define TREE int st,int l,int r
#define left st*2,l,(l+r)/2
#define right st*2+1,(l+r)/2+1,r
const int N=2e5+5;
int ind,a[N],n,q;
ll t[4*N][2][2],b[N],ans;
void update(TREE){
if(r<ind or l>ind)return;
if(l==r){
t[st][1][1]=abs(b[ind]);
return;
}
int mid=(l+r)/2,x=st*2,y=st*2+1;
update(left);update(right);
if((b[mid]>=0 and b[mid+1]>=0) or (b[mid]<=0 and b[mid+1]<=0)){
t[st][0][1]=max(t[x][0][1],t[x][0][0])+max(t[y][0][1],t[y][1][1]);
t[st][1][0]=max(t[x][1][1],t[x][1][0])+max(t[y][0][0],t[y][1][0]);
t[st][0][0]=max(t[x][0][0],t[x][0][1])+max(t[y][0][0],t[y][1][0]);
t[st][1][1]=max(t[x][1][0],t[x][1][1])+max(t[y][0][1],t[y][1][1]);
}
else{
t[st][0][1]=max(t[x][0][0]+t[y][1][1],t[x][0][1]+t[y][0][1]);
t[st][1][0]=max(t[x][1][0]+t[y][1][0],t[x][1][1]+t[y][0][0]);
t[st][0][0]=max(t[x][0][0]+t[y][1][0],t[x][0][1]+t[y][0][0]);
t[st][1][1]=max(t[x][1][0]+t[y][1][1],t[x][1][1]+t[y][0][1]);
}
}
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(NULL);
cin>>n>>q;
for(int i=0;i<n;i++){
cin>>a[i];
if(i>0){
b[i]=a[i]-a[i-1];
ind=i;
update(1,1,n-1);
}
}
while(q--){
int l,r,x;cin>>l>>r>>x;
if(l-1>=1){
b[l-1]+=x;
ind=l-1;
update(1,1,n-1);
}
if(r<n){
b[r]-=x;
ind=r;
update(1,1,n-1);
}
ans=max(t[1][0][0],t[1][0][1]);
ans=max(ans,t[1][1][0]);
ans=max(ans,t[1][1][1]);
cout<<ans<<"\n";
//b[r]=a[r+1]-a[r]
//b[l]=a[l+1]-a[l]
}
}
/*
3 5 8 6 4 10 12= 9
3 5 8 | 6 | 4 10 12=13
x a,b y =y-x
b<a
a-x+y-b=y-x+a-b
3 5 | 8 6 | 4 10 12 =2+2+8=12
3 5 8 6 10 12=9
3 5 8 | 6 10 12=5+6=11
12 - 3
8-3+12-6=12-3 +8-6
dpkl[i]=dpkl[i-1]+1
b[i]<0
+=x
b[i]>=0
dpkl[i-1]
dpkl[i-n]--
int st,x=st*2,y=st*2+1;
if((b[mid]>=0 and b[mid+1]>=0) or (b[mid]<=0 and b[mid+1]<=0)){
t[st][0][1]=max(t[x][0][1],t[x][0][0])+max(t[y][0][1],t[y][1][1]);
t[st][1][0]=max(t[x][1][1],t[x][1][0])+max(t[y][0][0],t[y][1][0]);
t[st][0][0]=max(t[x][0][0],t[x][0][1])+max(t[y][0][0],t[y][1][0]);
t[st][1][1]=max(t[x][1][0],t[x][1][1])+max(t[y][0][1],t[y][1][1]);
}
t[st][0][1]=max(t[x][0][0]+t[y][1][1],t[x][0][1]+t[y][0][1]);
t[st][1][0]=max(t[x][1][0]+t[y][1][0],t[x][1][1]+t[y][0][0]);
t[st][0][0]=max(t[x][0][0]+t[y][1][0],t[x][0][1]+t[y][0][0]);
t[st][1][1]=max(t[x][1][0]+t[y][1][1],t[x][1][1]+t[y][0][1]);
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4576 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4576 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
4 ms |
4688 KB |
Output is correct |
8 |
Correct |
4 ms |
4688 KB |
Output is correct |
9 |
Correct |
3 ms |
4688 KB |
Output is correct |
10 |
Correct |
4 ms |
4860 KB |
Output is correct |
11 |
Correct |
3 ms |
4688 KB |
Output is correct |
12 |
Correct |
3 ms |
4676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
2 ms |
4576 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
4 ms |
4688 KB |
Output is correct |
8 |
Correct |
4 ms |
4688 KB |
Output is correct |
9 |
Correct |
3 ms |
4688 KB |
Output is correct |
10 |
Correct |
4 ms |
4860 KB |
Output is correct |
11 |
Correct |
3 ms |
4688 KB |
Output is correct |
12 |
Correct |
3 ms |
4676 KB |
Output is correct |
13 |
Correct |
278 ms |
30792 KB |
Output is correct |
14 |
Correct |
281 ms |
30792 KB |
Output is correct |
15 |
Correct |
280 ms |
30792 KB |
Output is correct |
16 |
Correct |
281 ms |
30536 KB |
Output is correct |
17 |
Correct |
276 ms |
30636 KB |
Output is correct |
18 |
Correct |
271 ms |
31304 KB |
Output is correct |