#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define pp pair<ll,int>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
#define lb(x,i) lower_bound(all(x),i)-x.begin()
#define t3 tuple<int,int,int>
using namespace std;
const int mxn=2e5+5;
struct node{
ll dp[2][2]{0};
}t[4*mxn];ll a[mxn],b[mxn]{0};
node sum(node x,node y,int m){
node rs;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
rs.dp[i][j]=0;
for(int k=0;k<2;k++){
for(int l=0;l<2;l++){
if(l==1&&k==1&&((b[m]<0&&b[m+1]>0)||(b[m]>0&&b[m+1]<0)))continue;
rs.dp[i][j] = max(rs.dp[i][j],x.dp[i][k]+y.dp[l][j]);
}
}
}
}return rs;
}
void build(int i,int l,int r){
if(l==r)return void(t[i].dp[1][1]=abs(b[l]));
int m=(l+r)>>1;
build(2*i,l,m);build(2*i+1,m+1,r);
t[i]=sum(t[2*i],t[2*i+1],m);
}
void update(int i,int l,int r,int idx){
if(r<idx||l>idx)return;
if(l==r)return void(t[i].dp[1][1]=abs(b[l]));
int m=(l+r)>>1;
update(2*i,l,m,idx);update(2*i+1,m+1,r,idx);
t[i]=sum(t[2*i],t[2*i+1],m);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,q;cin>>n>>q;for(int i=1;i<=n;i++)cin>>a[i];
for(int i=2;i<=n;i++)b[i]=a[i]-a[i-1];
build(1,1,n);
while(q--){
int l,r,x;cin>>l>>r>>x;
if(l!=1){
b[l]+=x;
update(1,1,n,l);
}
if(r!=n){
b[r+1]-=x;
update(1,1,n,r+1);
}
cout<<max({t[1].dp[0][0],t[1].dp[1][0],t[1].dp[0][1],t[1].dp[1][1]})<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |