搞出每一位与前一位的差,然后区间修改只是会影响区间的端点,所以只修改一下端点的值就好。
%%%高一神犇线段树
1 #include<bits/stdc++.h> 2 #define N 100005 3 #define LL long long 4 using namespace std; 5 inline LL ra() 6 { 7 int x=0,f=1; char ch=getchar(); 8 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 9 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 10 return x*f; 11 } 12 LL S,T,ans,a[N<<1],b[N<<1]; 13 int n,Q; 14 int main() 15 { 16 n=ra(); Q=ra(); S=ra(); T=ra(); 17 for (int i=0; i<=n; i++) a[i]=ra(); 18 for (int i=1; i<=n; i++) 19 { 20 b[i]=a[i]-a[i-1]; 21 if (b[i]>0) ans-=S*abs(b[i]); 22 if (b[i]<0) ans+=T*abs(b[i]); 23 } 24 while (Q--) 25 { 26 int l=ra(),r=ra(); LL v=ra(); 27 if (l>r) swap(l,r); 28 if (b[l]>0) ans+=S*abs(b[l]); 29 if (b[l]<0) ans-=T*abs(b[l]); 30 b[l]+=v; 31 if (b[l]>0) ans-=S*abs(b[l]); 32 if (b[l]<0) ans+=T*abs(b[l]); 33 if (r==n) {printf("%lld\n",ans); continue;} 34 if (b[r+1]>0) ans+=S*abs(b[r+1]); 35 if (b[r+1]<0) ans-=T*abs(b[r+1]); 36 b[r+1]-=v; 37 if (b[r+1]>0) ans-=S*abs(b[r+1]); 38 if (b[r+1]<0) ans+=T*abs(b[r+1]); 39 printf("%lld\n",ans); 40 } 41 }