用树状数组解决的问题
大家没听过的可以传送至: https://www.cnblogs.com/wendster/p/treearray.html
以及: https://blog.csdn.net/qq_43673818/article/details/108414996
#include <iostream>
//#define lowbit(x) ((x)&-x)
using namespace std;
const int MAXN = 100005;
int n;
int c[MAXN];
int lowbit(int x){
return (x)&-x;
}
void update(int i,int x)
{
while(i <= n)
{
c[i] += x;
i += lowbit(i);
}
}
int Sum(int x)
{
int ans = 0;
while(x > 0)
{
ans += c[x];
x -= lowbit(x);
}
return ans;
}
int main()
{
int m;
scanf("%d %d",&n,&m);
int las = 0;
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
update(i,x-las);
las = x;
}
for(int i=0;i<m;i++)
{
int le,ri,num;
scanf("%d %d %d",&le,&ri,&num);
update(le,num);
update(ri+1,-num);
}
for(int i=1;i<=n;i++)
printf("%d%c",Sum(i)," \n"[i == n]);
return 0;
}