3368 树状数组2
树状数组这个东西经常和差分一块操作
那么这道题哪里体现了要运用差分这个东西?
第一个操作查询这个区间的和,通过我们之前的积累,相信用差分很好的解决
不得不说树状数组是一个很优秀的东西,比线段树盛世
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#define lowbit(x) (x&-x);
using namespace std;
const int SIZE=1e6+5;
int n,m;
long long ans=0;
int opt;
int a;
int c[SIZE];
int l,r,k,x;
void change(int x,long long y)
{
while(x<=n)
{
c[x]+=y;
x+=lowbit(x);
}
}
long long sol(int x)
{
long long cnt=0;
while(x)
{
cnt+=c[x];
x-=lowbit(x);
}
return cnt;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a;
change(i,a-ans);//建树
ans=a;
}
while(m--)
{
cin>>opt;
if(opt==1)
{
cin>>l>>r>>k;
change(l,k);
change(r+1,-k);
}
else
{
cin>>x;
cout<<sol(x)<<endl;
}
}
return 0;
}