我是靠谱客的博主 淡定西牛,这篇文章主要介绍CF 817E --- Choosing The Commander(字典树),现在分享给大家,希望可以做个参考。

字典树模板题,自己又敲了一遍,1A,开心o(* ̄▽ ̄*)ブ,代码比较美观了,思维也是清晰了许多。

代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<map> #include<cmath> using namespace std; typedef long long ll; int n,m,q,p; int ch[32],chh[32],a,b; int rt[3000005][2],num,kk[3000005]; void add() { int root=0; for(int i=0;i<=31;i++) { if(rt[root][ch[i]]) root=rt[root][ch[i]]; else root=rt[root][ch[i]]=++num; kk[root]++; } } void del() { int root=0; for(int i=0;i<=31;i++) { if(rt[root][ch[i]]) root=rt[root][ch[i]]; else return; kk[root]--; } } void query() { int ans=0; int root=0; for(int i=0;i<=31;i++) { if(chh[i]==0) { if(rt[root][ch[i]]) root=rt[root][ch[i]]; else break; } else { ans+=kk[rt[root][ch[i]]]; if(rt[root][!ch[i]]) root=rt[root][!ch[i]]; else break; } } printf("%dn",ans); return; } int main() { scanf("%d",&q); while(q--) { scanf("%d",&p); if(p==1) { scanf("%d",&a); int k=0; for(int i=31;i>=0;i--) { if((1<<i)&a) ch[k++]=1; else ch[k++]=0; } add(); } else if(p==2) { scanf("%d",&a); int k=0; for(int i=31;i>=0;i--) { if((1<<i)&a) ch[k++]=1; else ch[k++]=0; } del(); } else if(p==3) { scanf("%d%d",&a,&b); int k=0; for(int i=31;i>=0;i--) { if((1<<i)&a) ch[k]=1; else ch[k]=0; if((1<<i)&b) chh[k]=1; else chh[k]=0; k++; } query(); } } }

 

最后

以上就是淡定西牛最近收集整理的关于CF 817E --- Choosing The Commander(字典树)的全部内容,更多相关CF内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部