This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200010;
int lazy[4*N];
void Update(int node, int l, int r, int L, int R, int val){
if(L>r || R<l || l>r)return;
if(L<=l && R>=r){
lazy[node]+=val;
return;
}
int mid=(l+r)/2;
Update(2*node+1, l, mid, L, R, val);
Update(2*node+2, mid+1, r, L, R, val);
}
int Get(int node, int l, int r, int ind){
if(l>r || l>ind || r<ind)return 0;
if(l==r)return lazy[node];
int mid=(l+r)/2;
int ret = Get(2*node+1, l, mid, ind)+Get(2*node+2, mid+1, r, ind) + lazy[node];
return ret;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q, dec, inc;
cin >> n >> q >> dec >> inc;
int a[n+1];
for(int i=0; i< n+1; i++){
cin >> a[i];
}
int temp=0;
for(int i=1; i<=n; i++){
if(a[i]-a[i-1]>0){
temp-=dec*(a[i]-a[i-1]);
}
else temp+=inc*(a[i-1]-a[i]);
}
while(q--){
int l, r, x;
cin >> l >> r >> x;
int pl, pll, pr, prr;
int nl, nr;
pl=Get(0, 1, n+1, l)+a[l];
if(l==1)pll=0;
else pll=Get(0, 1, n+1, l-1)+a[l-1];
pr=Get(0, 1, n+1, r)+a[r];
if(r==n)prr=pr;
else prr=Get(0, 1, n+1, r+1)+a[r+1];
nl=pl+x;
nr=pr+x;
if(pl<pll){
temp-=inc*(pll-pl);
}
else temp+=dec*(pl-pll);
if(pr<prr){
if(r!=n)temp+=dec*(prr-pr);
}
else{
if(r!=n)temp-=inc*(pr-prr);
}
if(nl<pll){
temp+=inc*(pll-nl);
}
else temp-=dec*(nl-pll);
if(nr>prr){
if(r!=n)temp+=inc*(nr-prr);
}
else {
if(r!=n)temp-=dec*(prr-nr);
}
Update(0, 1, n+1, l, r, x);
cout << temp << '\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... |