CF817E Choosing The Commander(01Trie)

// Problem: CF817E Choosing The Commander
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF817E
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
  
inline void out(ll x){
    if (x < 0) x = ~x + 1, putchar('-');
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
  
inline void write(ll x){
    if (x < 0) x = ~x + 1, putchar('-');
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
  
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b,ll p)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a%p;
        a = a * a %p;
        b >>= 1;
    }
    return res;
}

const int inf = 0x3f3f3f3f;
const int maxn=6e6+7;

int tr[maxn][2],cnt[maxn],idx=1;

void add(int x,int val){
	int p=1;
	for(int i=30;i>=0;i--){
		int u=(x>>i&1);
		if(val==1){
			if(tr[p][u]==0) tr[p][u]=++idx;
		}
		p=tr[p][u];
		cnt[p]+=val;
	}
}

int qask(int x,int y){
	int p=1,sum=0;
	for(int i=30;i>=0;i--){
		int t=y>>i&1,t1=x>>i&1;
		if(t) sum+=cnt[tr[p][t1]],p=tr[p][t1^1];
		else p=tr[p][t1];
	}
	return sum;
}

int main(){
	int _=read;
	while(_--){
		int op=read;
		if(op==1){
			int x=read;
			add(x,1);
		}
		else if(op==2){
			int x=read;
			add(x,-1);
		}
		else{
			int x=read,y=read;
			cout<<qask(x,y)<<"\n";
		}
	}
	
	
	return 0;
}


posted @ 2021-08-03 12:14  OvO1  阅读(45)  评论(0编辑  收藏  举报