题面
大致意思是有多个区间修改操作,对[l,r]内的数加上或减去c。
Input
Output
Examples
Input
4 2
-3 6 8 4
4 4 -2
3 3 1
Output
-3 6 9 2
Input
2 1
5 -2
1 2 4
Output
9 2
Input
1 2
0
1 1 -8
1 1 -6
Output
-14
思路
只修改不查询或者修改和查询分开,用差分。差分只记录整个过程中修改值在何处变化,比如[l,r]+c,修改值在l处+c,在r+1处-c(r+1<=n+1,所以多开一点空间)。
差分是一种思想,在有些时候有妙用。
代码
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
typedef long long LL;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define mem(a,s) memset(a,s,sizeof(a))
int n,q;
LL a[200010];
LL b[200010];
int main(){
//freopen("1.txt","r",stdin);
ios::sync_with_stdio(false);
cin>>n>>q;
mem(b,0);
rep(i,1,n)
cin>>a[i];
rep(i,1,q){
int l,r,x;
cin>>l>>r>>x;
b[l]+=x;
b[r+1]-=x;
}
LL x=0;
rep(i,1,n){
x+=b[i];
cout<<a[i]+x;
if(i<n)
cout<<' ';
else cout<<endl;
}
return 0;
}