题意有n次操作,每一次向加一个数x,或者减一个数x,(x必须在集合中存在),然后给你一些数,集合中最大和他xor得到的数是多大?(操作为2e5)每次的数为小于1e9
**思路**01trie,这个trie的方法就是你每次都用一个0,或者1来当字典,然后你每次查找的时候用一个贪心的思想去查找。,。,。,。能1就尽量取,如果实在没招也只能取0,最后就得到一个值,输出出来就好了。
以前的trie的模板,。,。,。我当时没有学到精髓,学长就走了。。。。现在只好换了个模板,还好人家学到了精髓。。。。只好换模板了,抄模板的坏处就是学不到精髓啊,。。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 2e5 + 10;
int lmax = 35 , tail = 0;
int trie[32 * N][2] , s[40] , number[32 * N] , d[40];
void Insert()
{
int p = 0;
for(int i = lmax ; i >= 1 ; i--){
int x = s[i];
if(trie[p][x] == 0) trie[p][x] = ++tail;
p = trie[p][x] , number[p]++;
}
}
void Add(int x)
{
int l = 0;
while(l <= lmax) s[++l] = x % 2 , x /= 2;
Insert();
}
void Delete()
{
int p = 0;
for(int i = lmax ; i >= 1 ; i--){
int x = s[i];
p = trie[p][x] , number[p]--;
}
}
void Era(int x)
{
int l = 0;
while(l <= lmax) s[++l] = x % 2 , x /= 2;
Delete();
}
void Xor()
{
int p = 0 , cnt = 0;
for(int i = lmax ; i >= 1 ; i--){
int x = s[i];
if(number[trie[p][1 ^ x]] > 0) p = trie[p][1 ^ x] , d[++cnt] = 1;
else p = trie[p][0 ^ x] , d[++cnt] = 0;
}
int sum = 0;
for(int i = 1 ; i <= cnt ; i++) sum = sum*2 + d[i];
printf("%d\n",sum);
}
void solve(int x)
{
int l = 0;
while(l <= lmax) s[++l] = x % 2 , x /= 2;
Xor();
}
int main()
{
int n;
Add(0);
scanf("%d",&n);
for(int i = 1 ; i <= n ; i++){
int x;
char button[3];
scanf("%s %d",button , &x);
if(button[0] == '+') Add(x);
else if(button[0] == '-') Era(x);
else{
solve(x);
}
}
return 0;
}