[Codeforces 817E] Choosing The Commander Trie树
来源:互联网 发布:杭州linux运维 编辑:程序博客网 时间:2024/05/23 15:06
As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires.
Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors.
Each warrior is represented by his personality — an integer number pi. Each commander has two characteristics — his personality pjand leadership lj (both are integer numbers). Warrior i respects commander j only if ( is the bitwise excluding OR ofx and y).
Initially Vova's army is empty. There are three different types of events that can happen with the army:
- 1 pi — one warrior with personality pi joins Vova's army;
- 2 pi — one warrior with personality pi leaves Vova's army;
- 3 pi li — Vova tries to hire a commander with personality pi and leadership li.
For each event of the third type Vova wants to know how many warriors (counting only those who joined the army and haven't left yet)respect the commander he tries to hire.
The first line contains one integer q (1 ≤ q ≤ 100000) — the number of events.
Then q lines follow. Each line describes the event:
- 1 pi (1 ≤ pi ≤ 108) — one warrior with personality pi joins Vova's army;
- 2 pi (1 ≤ pi ≤ 108) — one warrior with personality pi leaves Vova's army (it is guaranteed that there is at least one such warrior in Vova's army by this moment);
- 3 pi li (1 ≤ pi, li ≤ 108) — Vova tries to hire a commander with personality pi and leadership li. There is at least one event of this type.
For each event of the third type print one integer — the number of warriors who respect the commander Vova tries to hire in the event.
51 31 43 6 32 43 6 3
10
In the example the army consists of two warriors with personalities 3 and 4 after first two events. Then Vova tries to hire a commander with personality 6 and leadership 3, and only one warrior respects him (, and 2 < 3, but , and 5 ≥ 3). Then warrior with personality 4 leaves, and when Vova tries to hire that commander again, there are no warriors who respect him.
题解:
博主很菜啊,并没有什么码力,只能写写小水题辣!
xor相关,没有什么别的好的性质,于是考虑xor的原始定义-------->按位异或----------->于是可以想到01Trie树.
建立出01Trie树后,可以对于每个节点记录这个以这个节点为根的子树上的士兵总数,
从而可以O(logp)进行添加,删除和询问操作 (话说trie树把递归变成循环能快不少呢)
时间复杂度:O(qlogp).
Code:
#include <bits/stdc++.h>using namespace std;inline int read(){int x=0;char c=getchar();while (c<'0'||c>'9') {c=getchar();}while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}return x;}struct trie{int s,ch[2];}t[3000005];int cnt=1,rt=1;inline void add(int x){int pos=1,i,tg;t[pos].s++;for (i=26;i>=0;i--){if (x&(1<<i)) {tg=1;}else {tg=0;}if (!t[pos].ch[tg]){t[pos].ch[tg]=++cnt;}pos=t[pos].ch[tg];t[pos].s++;}return;}inline void del(int x){int pos=1,i,tg;t[pos].s--;for (i=26;i>=0;i--){if (x&(1<<i)) {tg=1;}else {tg=0;}pos=t[pos].ch[tg];t[pos].s--;}return;}inline void ask(int x,int tar){int ans=0,pos=1,i,nt=0,ok;for (i=26;i>=0;i--){if (nt+(1<<i)-1<tar) {ok=1;nt=(nt+(1<<i));}else {ok=0;}int p=x&(1<<i),tag;if (p>0) {tag=0;}else {tag=1;}if (!ok) {tag^=1;}else {ans+=t[t[pos].ch[tag^1]].s;}pos=t[pos].ch[tag];}printf ("%d\n",ans);return;}int main (){int p,q,l,opt;q=read();while (q--){opt=read();if (opt==1) {p=read();add(p);}if (opt==2) {p=read();del(p);}if (opt==3) {p=read();l=read();ask(p,l);}}return 0;}
- [Codeforces 817E] Choosing The Commander Trie树
- CodeForces 817E Choosing The Commander题解
- Educational Codeforces Round 23 E. Choosing The Commander(01Trie)
- Educational Codeforces Round 23 817E. Choosing The Commander 字典树 位运算
- Educational Codeforces Round 23 E. Choosing The Commander (字典树)
- CODEFORCES, 265E Choosing Balls
- Sausage Maximization - CodeForces 282 E Trie树
- Codeforces 665E. Beautiful Subarrays (trie树)
- Codeforces 665E Beautiful Subarrays (Trie树)
- Educational Codeforces Round 12 E trie树
- Codeforces Round #190 (Div. 2) E. Ciel the Commander 题目与题解翻译
- Codeforces 321C Ciel the Commander 树分治
- CodeForces 321 C.Ciel the Commander(树分治)
- 暑假- Trie树-(E - Remember the Word)
- [博弈 杂题] Codeforces 794E Round #414 E. Choosing Carrot
- Codeforces-665E-Beautiful Subarrays Trie树(字典树)
- Codeforces 557E Ann and Half-Palindrome (Trie树)
- Codeforces 665E Beautiful Subarrays【01trie树】
- 微调caffemodel来得到属于自己的高精度分类模型
- ng-model、ng-bind、ng-value使用区别
- 利用ms12_027漏洞生成文字马
- PAT刷题:1033. 旧键盘打字(20)
- qduoj 真-签到-炸老师的日记(贪心)
- [Codeforces 817E] Choosing The Commander Trie树
- Java Swing 弹出对话框的使用
- TypeError: 'numpy.float64' object cannot be interpreted as an index
- 项目实训-千寻-服务端代码集成测试
- Android Binder
- mongodb的分组查询和处理
- 机器学习模型应用以及模型优化的一些思路
- Ngrok内网穿透配合msf木马
- git---基本操作、新建分支,合并分支branch到主干master ,git pull ,git push