树状数组1:
#include <iostream>
#define lowbit(x) x&-x
using namespace std;
const int maxn = 500005;
int C[maxn],u,v,z,n,m;
void update(int x,int y)
{
for(int i = x ; i <= n ; i +=lowbit(i))C[i] += y;
}
int getsum(int x)
{
int res = 0;
for(int i=x;i>0;i-=lowbit(i))res += C[i];
return res;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>z;
update(i,z);
}
while(m--)
{
cin>>z>>u>>v;
if(z-1)cout<<getsum(v)-getsum(u-1)<<endl;
else update(u,v);
}
return 0;
}
树状数组2
#include <iostream>
#define lowbit(x) x&-x
using namespace std;
const int maxn = 500005;
int C[maxn],x,y,w,last,z,m,n;
void update(int v,int b)
{
for(int i=v;i<=n;i+=lowbit(i))C[i]+=b;
}
int getsum(int v)
{
int res = 0;
for(int i = v; i>0;i-=lowbit(i))res+=C[i];
return res;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>x;
update(i,x-last);
last = x;
}
while(m--)
{
cin>>w;
if(w-1)
{
cin>>x;
cout<<getsum(x)<<endl;
}
else
{
cin>>x>>y>>z;
update(x,z);
update(y+1,-z);
}
}
return 0;
}