题目分析:
首先这题的询问和位(bit)有关,不难想到是用线段树维护位运算。
现在我们压32位再来看这道题。
对于一个加法操作,它的添加位置可以得到,剩下的就是做不超过32的位移。这样根据压位的理论。它最多只会对线段树的两个叶子产生影响,我们分开来考虑两个叶子。
对于一个加法的进位,它实际就是把它之后连续的全为1的位赋值成0,然后更改第一个不是全为1的位,不难想到用lazytag实现。
减法操作与加法操作相反。所以我们要两个标记和两个lazy标记。
对于一个询问,在线段树上查找即可。
代码:
#include<bits/stdc++.h>
using namespace std; const int base = ; int n,maxx; struct SEGT{
unsigned int dt;
bool lazy0,lazy1;
bool all0,all1;
}T[(<<)]; void build_tree(int now,int l,int r){
T[now].all0 = ;
if(l == r) return;
int mid = (l+r)/;
build_tree(now<<,l,mid);
build_tree(now<<|,mid+,r);
} void push_down0(int now){
T[now<<].all0 = ; T[now<<].all1 = ;
T[now<<].lazy0 = ; T[now<<].lazy1 = ;
T[now<<|].all0 = ; T[now<<|].all1 = ;
T[now<<|].lazy0 = ; T[now<<|].lazy1 = ;
T[now<<].dt = ; T[now<<|].dt = ; T[now].lazy0 = ;
} void push_down1(int now){
T[now<<].all1 = ; T[now<<].all0 = ;
T[now<<].lazy1 = ; T[now<<].lazy0 = ;
T[now<<|].all1 = ; T[now<<|].all0 = ;
T[now<<|].lazy1 = ; T[now<<|].lazy0 = ;
T[now<<].dt = (1ll<<)-; T[now<<|].dt = (1ll<<)-; T[now].lazy1 = ;
} void push_up(int now){
if(T[now<<].all0 && T[now<<|].all0) T[now].all0 = ; else T[now].all0 = ;
if(T[now<<].all1 && T[now<<|].all1) T[now].all1 = ; else T[now].all1 = ;
} void leafpd(int now){
if(T[now].dt == ) T[now].all0 = ; else T[now].all0 = ;
if(T[now].dt == (1ll<<)-) T[now].all1 = ; else T[now].all1 = ;
} int tag = ;
void uppaint(int now,int tl,int tr,int place){
if(tl != tr && T[now].lazy0) push_down0(now);
if(tl != tr && T[now].lazy1) push_down1(now);
if(tl >= place){
if(T[now].all1){
T[now].all0 = ;T[now].all1 = ;
T[now].lazy0 = ;T[now].lazy1 = ;
T[now].dt = ;
return;
}else{
if(tl == tr){T[now].dt++;tag = ;leafpd(now);return;}
int mid = (tl+tr)/;
uppaint(now<<,tl,mid,place);
if(!tag) uppaint(now<<|,mid+,tr,place);
push_up(now);
}
}else{
int mid = (tl+tr)/;
if(place > mid) uppaint(now<<|,mid+,tr,place);
else{
uppaint(now<<,tl,mid,place);
if(!tag) uppaint(now<<|,mid+,tr,place);
}
push_up(now);
}
} void downpaint(int now,int tl,int tr,int place){
if(tl != tr && T[now].lazy0) push_down0(now);
if(tl != tr && T[now].lazy1) push_down1(now);
if(tl >= place){
if(T[now].all0){
T[now].all1 = ;T[now].all0 = ;
T[now].lazy1 = ;T[now].lazy0 = ;
T[now].dt = (1ll<<)-;
return;
}else{
if(tl == tr){T[now].dt--;tag=;leafpd(now);return;}
int mid = (tl+tr)/;
downpaint(now<<,tl,mid,place);
if(!tag) downpaint(now<<|,mid+,tr,place);
push_up(now);
}
}else{
int mid = (tl+tr)/;
if(place > mid) downpaint(now<<|,mid+,tr,place);
else{
downpaint(now<<,tl,mid,place);
if(!tag) downpaint(now<<|,mid+,tr,place);
}
push_up(now);
}
} void TModify(int now,int tl,int tr,int place,long long data){
if(tl == tr){
if(data >= ){
long long res = data + T[now].dt;
T[now].dt = (res&((1ll<<)-));
if(res >= (1ll<<)){ tag = ;uppaint(,,n,place+);}
}else{
long long res = T[now].dt + data;
if(res >= ) T[now].dt = res;
else{
T[now].dt = res + (1ll<<);
tag = ;downpaint(,,n,place+);
}
}
leafpd(now);
return;
}
if(T[now].lazy0) push_down0(now);
if(T[now].lazy1) push_down1(now);
int mid = (tl+tr)/;
if(place <= mid) TModify(now<<,tl,mid,place,data);
else TModify(now<<|,mid+,tr,place,data);
push_up(now);
} void Modify(){
int dr,data,bit; scanf("%d%d",&data,&bit);
if(data < ) dr = -; else dr = ;
data = abs(data);
int a1 = bit>>,a2 = bit&;
if((1ll*data<<a2) >= (1ll<<)){
long long fw = 1ll*data<<a2;
TModify(,,n,a1,(fw&((1ll<<)-))*dr);
fw >>= ;
TModify(,,n,a1+,fw*dr);
}else{
TModify(,,n,a1,(1ll*data<<a2)*dr);
}
} int QTree(int now,int tl,int tr,int place,int bit){
if(T[now].all0) return ; if(T[now].all1) return ;
if(tl == tr){ if(T[now].dt & (<<bit)) return ; else return ; }
int mid = (tl+tr)/;
if(place <= mid) return QTree(now<<,tl,mid,place,bit);
else return QTree(now<<|,mid+,tr,place,bit);
} void Query(){
int kth; scanf("%d",&kth);
int a1 = kth>>,a2 = kth&;
printf("%d\n",QTree(,,n,a1,a2));
} void work(){
for(int i=;i<=n;i++){
int cas; scanf("%d",&cas);
if(cas == ){Modify();}
else Query();
}
} int main(){
scanf("%d",&n); int x; scanf("%d%d%d",&x,&x,&x);
build_tree(,,n);
work();
return ;
}