题目链接
考察树状数组原理
#include<cstdio>
typedef long long ll;
ll lowbit(ll x)
{
return x&(-x);
}
ll query(ll x)
{
ll ans=0,tmp=1;
for(ll i=0;tmp<=x;i++)
{
ans+=(x/tmp-x/(tmp<<1))*tmp;
tmp<<=1;
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
ll n,q;
while(scanf("%lld%lld",&n,&q)!=EOF)
{
int op;
ll x,y,ans=0;
for(int i=1;i<=q;i++)
{
scanf("%d",&op);
if(op==1)
{
scanf("%lld %lld",&x,&y);
printf("%lld\n",query(y)-query(x-1));
}
if(op==2)
{
scanf("%lld",&x);
ans=0;
while(x<=n)
{
ans++;
x+=lowbit(x);
}
printf("%lld\n",ans);
}
}
}
return 0;
}