题目
Thanks to everyone’s help last week, TT finally got a cute cat. But what TT didn’t expect is that this is a magic cat.
One day, the magic cat decided to investigate TT’s ability by giving a problem to him. That is select nn cities from the world map, and a[i]a[i] represents the asset value owned by the ii-th city.
Then the magic cat will perform several operations. Each turn is to choose the city in the interval [l,r][l,r] and increase their asset value by cc. And finally, it is required to give the asset value of each city after qq operations.
Could you help TT find the answer?
Input
The first line contains two integers n,qn,q (1≤n,q≤2⋅105)(1≤n,q≤2·105) — the number of cities and operations.
The second line contains elements of the sequence aa: integer numbers a1,a2,…,ana1,a2,…,an (−106≤ai≤106)(−106≤ai≤106).
Then qq lines follow, each line represents an operation. The ii-th line contains three integers l,rl,r and cc (1≤l≤r≤n,−105≤c≤105)(1≤l≤r≤n,−105≤c≤105) for the ii-th operation.
Output
Print nn integers a1,a2,…,ana1,a2,…,an one per line, and aiai should be equal to the final asset value of the ii-th city.
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
差分
思路
step0 在录入过程中计算录入元素的差分记录在b数组中
step1 将在l~r区间内+c转变成b[l]+c,b[r+1]-c,同时观察到即使r+1越界也不影响计算结果
step2 当所有操作完成后,通过累加b[1~i],输出第i个元素。
错误
1、在第17个点出现WA的情况,考虑前面过了那么多点,可能是变量开的大小不够,换成long long存储,问题解决
代码
#include<iostream>
using namespace std;
int main()
{
int l,r,c,n,q;
long long temp;
scanf("%d %d",&n,&q);
long long b[200001];
scanf("%lld",&b[1]);
temp=b[1];
for(int i=2;i<=n;i++)
{
scanf("%lld",&b[i]);
b[i]=b[i]-temp;//注意这里b[i]是a[i]-a[i-1]而非a[i]-b[i-1],b存储的均为差值
temp=b[i]+temp;
}
for(int i=0;i<q;i++)
{
scanf("%d %d %d",&l,&r,&c);
b[l]+=c;
b[r+1]-=c;
}
printf("%lld ",b[1]);
for(int i=2;i<=n;i++)
{
b[i]=b[i-1]+b[i];
printf("%lld ",b[i]);
}
printf("\n");
return 0;
}