B题 TT’s 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.
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
**解题思路:
差分的特点就是可以将A数组的区间加等价于B数组的单点修改
B[1]=A[1]
B[I]=A[i]-A[i-1]
A[L]-A[R]都加上C
等价于 B[L]+=C,B[R+1]-=C
然后再用A[i]=A[i-1]+B[i]对数组进行还原就可以啦
**
注意事项该题的数字范围较大,所以要用到long long
#include<iostream>
using namespace std;
const int N=200010;
long long a[N],b[N];
int main()
{
int n;
cin>>n;
int q;
cin>>q;
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
b[1]=a[1];
for(int i=2;i<=n;i++)
b[i]=a[i]-a[i-1];
while(q--)
{
long long l,r,c;
cin>>l>>r>>c;
b[l]+=c;
b[r+1]-=c;
}
a[1]=b[1];
cout<<a[1]<<" ";
for(int i=2;i<=n;i++)
{
a[i]=b[i]+a[i-1];
cout<<a[i]<<" ";
}
return 0;
}