lowbit

最近蒟蒻刚学lowbit,贴以下代码

#include<iostream>
#include<algorithm>
#define lowbit(i) ((i)&(-i))
#define s 1000000
using namespace std;
typedef long long LL;
LL tree[s],in;
int n,m;
void add(int x,int v)
{
while(x<=n)
{
tree[x]+=v;
x+=lowbit(x);
}
}
LL gs(int x)
{
LL sum=0;
while(x!=0)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>in;
add(i,in);
}

for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
if(x==1)
add(y,z);
if(x==2)
cout<<gs(z)-gs(y-1)<<endl;
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/ct666rp/p/11881431.html