trie树 Codeforces Round #367 D Vasiliy's Multiset

 1 // trie树 Codeforces Round #367 D Vasiliy‘s Multiset
 2 // 题意:给一个集合,初始有0,+表示添加元素,-去除元素,?询问集合里面与x异或最大的值
 3 // 思路:思路很好想,建立trie树,再贪心当前位是1则选0,0则选1
 4
 5
 6 #include <bits/stdc++.h>
 7 using namespace std;
 8 #define LL long long
 9 const double inf = 123456789012345.0;
10 const LL MOD =100000000LL;
11 const int N =1e7+10;
12 #define clc(a,b) memset(a,b,sizeof(a))
13 const double eps = 1e-7;
14 void fre() {freopen("in.txt","r",stdin);}
15 void freout() {freopen("out.txt","w",stdout);}
16 inline int read() {int x=0,f=1;char ch=getchar();while(ch>‘9‘||ch<‘0‘) {if(ch==‘-‘) f=-1; ch=getchar();}while(ch>=‘0‘&&ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();}return x*f;}
17 int d[N][2];
18 int cnt=1;//节点下标
19 int num[N];
20 void update(int x){
21     int k=1;
22     for(int i=30;i>=0;i--){
23         if(d[k][(x>>i)&1]==0) d[k][(x>>i)&1]=++cnt;
24         k=d[k][(x>>i)&1];
25         num[k]++;
26     }
27 }
28
29 void update1(int x){
30     int k=1;
31     num[1]--;
32     for(int i=30;i>=0;i--){
33         k=d[k][(x>>i)&1];
34         num[k]--;
35     }
36 }
37
38 int query(int x){
39     int k=1;
40     int ans=0;
41     for(int i=30;i>=0;i--){
42         int t=(x>>i)&1;
43         if(num[d[k][1^t]]) k=d[k][1^t],ans+=(1<<i);
44         else k=d[k][t];
45     }
46     return ans;
47 }
48
49 int main(){
50     int q;
51     scanf("%d",&q);
52     update(0);
53     while(q--){
54         char s[2];
55         int x;
56         scanf("%s%d",s,&x);
57         if(s[0]==‘+‘) update(x);
58         else if(s[0]==‘-‘) update1(x);
59         else {
60             int ans=query(x);
61             printf("%d\n",ans);
62         }
63     }
64     return 0;
65 }

trie树 Codeforces Round #367 D Vasiliy's Multiset

时间: 2024-07-30 23:56:26

trie树 Codeforces Round #367 D Vasiliy's Multiset的相关文章

Codeforces Round #367 (Div. 2) D. Vasiliy&#39;s Multiset

题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 题解: trie树xjb搞就行,每次要贪心,尽量满足高位为1. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 using namespace std; 4 5 namespace trie 6 {

十字链表 Codeforces Round #367 E Working routine

1 // 十字链表 Codeforces Round #367 E Working routine 2 // 题意:给你一个矩阵,q次询问,每次交换两个子矩阵,问最后的矩阵 3 // 思路:暴力肯定不行.我们可以每个元素建立十字链表,记录右边和下边的元素,和每个元素的下标(从0开始),每次询问只需要交换四条边的指向即可. 4 // 本题要建立(n+1)*(m+1)的矩阵 5 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define

set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

题目传送门 1 /* 2 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 3 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 4 查找左右相邻的位置,更新长度为r - l - 1的最大值,感觉线段树结构体封装不错! 5 详细解释:http://blog.csdn.net/u010660276/article/details/46045777 6 其实还有其他解法,先掌握这种:) 7 */ 8 #include <cstd

Codeforces Round #367 (Div. 2) ABCD

A. Beru-taxi 题解: 水,求下距离就行了 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define ll long long #define CLR(x) memset(x,0,sizeof x) #define MC(x,y) memcpy(x,y,sizeof(x)) #

线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

题目传送门 1 /* 2 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 3 详细解释:http://www.xuebuyuan.com/1154895.html 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <c

Codeforces Round #367 (Div. 2) 套题

吐槽:只能说是上分好场,可惜没打,唉 A:Beru-taxi (水题,取最小值) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string>

C. Watto and Mechanism 字典树 Codeforces Round #291 (Div. 2)

C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a ce

Codeforces Round #367 (Div. 2)

A. Beru-taxi (Codeforces 706A) 水题,求一下到每个点的距离,取最小值即可 #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #define N using namespace std; int n,x,y,p,q,s; double t,ans=1000000000; int main() { scanf("%d%d%d&

Codeforces Round #367 Div. 2 [11110]

#include <iostream> #include <map> #include <math.h> using namespace std; int main() { double a, b; cin >> a >> b; int n; cin >> n; double MIN = 1e10; while (n--) { double x, y, v; cin >> x >> y >> v;