# 基础线段树入门 NOI2017整数

——吕秀才武林外传

n次操作维护一个长度为n的二进制整数x，初始为丰，支持以下两种操作：
1.将这个整数加上ai · $2^{bi}$

2. 询问这个整数二进制意义下第ki位的值。

n<=1000000  $\left | a[i] \right |$<=1e9 b<=30

#include<bits/stdc++.h>
using namespace std;
const int N=3000000;
const long long  base=30,all=(1<<30)-1;
long long n,opt,m,a,b,pos,zhi,tmp;
long long mark[N],val[N];
int x=0,f=1;
char c=getchar();
while((c<'0')||(c>'9')){if(c=='-')f=-1;c=getchar();}
while((c>='0')&&(c<='9'))x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
void update(int x)
{
if (mark[x]==1) {  mark[x*2]=1; mark[x*2+1]=1;val[x*2]=all; val[x*2+1]=all;}
if (mark[x]==0) {  mark[x*2]=0; mark[x*2+1]=0;val[x*2]=0; val[x*2+1]=0;}
}
int  add(int a1,int l,int r,int pos,int zhi)
{
int res=0;
if (zhi==0) return 0;
if (l==r) {if (all-val[a1]<zhi)
{val[a1]=zhi+val[a1]-all-1;
if (val[a1]==0) mark[a1]=0; else mark[a1]=-1; return 1;}
else {val[a1]=val[a1]+zhi;
if (val[a1]==all) mark[a1]=1; else mark[a1]=-1; return 0;}}
int mid=(l+r)/2;
update(a1);
if((zhi==1)&&(mark[a1]==1)&&(pos==l)){
mark[a1]=0;
return 1;}
}
mark[a1]=(mark[a1*2]==mark[a1*2+1])? mark[a1*2]:-1;
return res;

}
int  del(int a1,int l,int r,int pos,int zhi)
{    int res=0;
if (zhi==0) return 0;
if (l==r) {if (val[a1]<zhi)  {   val[a1]=all+1+val[a1]-zhi;
if (val[a1]==all) mark[a1]=1;
else mark[a1]=-1; return 1;}
else  {   val[a1]=val[a1]-zhi;
if (val[a1]==0) mark[a1]=0;
else mark[a1]=-1; return 0;}}
int mid=(l+r)/2;
update(a1);
if((zhi==1)&&(mark[a1]==0)&&(pos==l)){
mark[a1]=1;
return 1;
}
if (pos<=mid) { res=del(a1*2,l,mid,pos,zhi);
if (res) res=del(a1*2+1,mid+1,r,mid+1,1);}
else res=del(a1*2+1,mid+1,r,pos,zhi);
mark[a1]=(mark[a1*2]==mark[a1*2+1])? mark[a1*2]:-1;
return res;

}
long long  query(int a1,int l,int r,int pos)
{ long long res;
if (l==r) return val[a1];
update(a1);
int mid=(l+r)/2;
if (pos<=mid)  res=query(a1*2,l,mid,pos);  else res=query(a1*2+1,mid+1,r,pos);
mark[a1]=(mark[a1*2]==mark[a1*2+1])? mark[a1*2]:-1;
return res;
}
int main()
{
memset(val,0,sizeof(val));
for (int i=1;i<=n;i++)
pos=b/base;  tmp=b%base;
else
{  a=-a; pos=b/base;  tmp=b%base;
del(1,0,n,pos,(a&((1<<(base-tmp))-1) )<<tmp);
del(1,0,n,pos+1,a>>(base-tmp));
}

}
else
}