firstly,let’s see the problem.
sample Input
4 2
-3 6 8 4
4 4 -2
3 3 1
sample output
-3 6 9 2
sample Input
2 1
5 -2
1 2 4
sample output
9 2
sample input
1 2
0
1 1 -8
1 1 -6
sample output
-14
Answer:this problem is about prefix and difference,so we can use array
B to store the difference between A,and then use array ans to record the result
#include<stdio.h>
const int maxn=2*1e5+10;
long long int a[maxn],b[maxn],ans[maxn];
int main()
{
long long int n,q,l,r,c;
scanf("%ld%ld",&n,&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];
for(int j=0;j<q;j++)
{
scanf("%ld%ld%ld",&l,&r,&c);
b[l]+=c;
b[r+1]-=c;
}
ans[1]=b[1];sum[0]=0;
for(int i=1;i<=n;i++){
ans[i]=ans[i-1]+b[i];
printf("%lld ",ans[i]);
}
return 0;
}
↑上面这道题吓得我都不敢写C++了,TLE太阔怕了都,o(╥﹏╥)o