【字典树】【贪心】Codeforces 706D Vasiliy's Multiset

时间:2023-03-08 23:39:51
【字典树】【贪心】Codeforces 706D  Vasiliy's Multiset

题目链接:

  http://codeforces.com/contest/706/problem/D

题目大意

  三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值。(可重复)

题目思路:

  【字典树】【贪心】

  维护一个字典树,左0右1。查询时从上往下走。

  

 //
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 5000004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int t[N];
int ch[N][];
char c[];
void add(int x)
{
int i,j,k=;
for(i=;i>=;i--)
{
j=((<<i)&x)>;
if(!ch[k][j])ch[k][j]=++lll;
k=ch[k][j];
t[k]++;
}
}
void del(int x)
{
int i,j,k=;
for(i=;i>=;i--)
{
j=((<<i)&x)>;
k=ch[k][j];
t[k]--;
}
}
int find(int x)
{
int i,j,k=;
ans=;
for(i=;i>=;i--)
{
j=((<<i)&x)==;
if(t[ch[k][j]])
{
ans|=(<<i);
k=ch[k][j];
}
else k=ch[k][-j];
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,x;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
mem(ch,);
mem(t,);
lll=;
add();
for(i=;i<=n;i++)
{
scanf("%s%d",c,&x);
if(c[]=='+')
add(x);
else if(c[]=='-')
del(x);
else printf("%d\n",find(x));
}
}
return ;
}
/*
// //
*/