题目:https://nanti.jisuanke.com/t/39451
用set可以很好地解决这道题
#include<bits/stdc++.h> using namespace std; typedef long long LL; set<LL>se; LL quick(LL a,LL b) { LL res=1; while(b) { if(b&1)res=res*a; b>>=1; a=a*a; } return res; } int main() { LL m,k,q; cin>>m>>k>>q; LL a,b; if(m==1) { while(q--) { scanf("%lld%lld",&a,&b); if(a==1)se.insert(b); else se.erase(se.find(b)); if(se.empty())printf("%lld ",k); else printf("%lld ",*se.begin()); } } else { LL sum=quick(2,k)-1; while(q--) { LL ans=sum; scanf("%lld%lld",&a,&b); if(a==1)se.insert(b); else se.erase(se.find(b)); set<LL>::iterator it; for(it=se.begin();it!=se.end();it++) { int flag=0; LL temp=*it; if(temp==0) { ans=0;break; } while(temp)//查看其祖先是否已删除,即是否在set里面 { if(se.find(temp)!=se.end()&&se.find(temp)!=it) { flag=1;break; } temp=(temp-1)/2;//(temp+1)/2-1 } if(!flag)ans-=(quick(2,k-(int)log2(*it+1))-1); } printf("%lld ",ans); } } return 0; }