AcWing 797. 差分
原题链接
简单
#include<iostream>
using namespace std;
const int N=1E5+10;
int n,m;
int a[N],b[N];
void insert(int l,int r,int c)
{
b[l] += c;
b[r+1] -= c;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
insert(i,i,a[i]);
//解释:
//b[1] = a[1]
//b[2] = a[2] - a[1] -> a[2] = b[2] + b[1]
//b[3] = a[3] - a[2] -> a[3] = b[3] + b[2]
}
while(m--)
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
insert(l,r,c);
}
for(int i=1;i<=n;i++)
{
// b[i] += b[i-1]; -> a[i] = b[i] + b[i-1]
// printf("%d ",b[i]);
a[i] = b[i-1]+b[i];
b[i] = a[i];
printf("%d ",a[i]);
}
}