Educational Codeforces Round 23 817E. Choosing The Commander 字典树 位运算
2017-07-17 11:17
399 查看
题目链接: Choosing The Commander
题目大意
士兵有一个个性值p将领有一个个性值p, 领导力l
(1≤p,l≤108)
如果将领和士兵个性值按位异或的得到的值小于将领的领导力, 这个士兵会尊重这个将领
三种操作:
1 p:添加一名个性值为p的士兵
2 p:减少一名个性值为p的士兵
3 p l:个性值为p, 领导力为l的将领, 输出有多少士兵尊重他
思路
建立字典树将个性值以二进制形式从高位到低位(31位-0位)插入字典树, 沿途经过的节点cnt值+1(每个节点cnt值代表的是经过这个节点的所有士兵数量)
减少士兵则将沿途节点cnt-1
对于每一个将领
也是将他的p和l从最高位到最低位一位位的处理
如果将领的p在这位的值为0
那么对于这位为1的士兵, 异或得1, 为0的士兵异或为0, 当将领领导力l在这位为0时, 为1的士兵一定不会尊重它, 接下来处理为0的士兵, 当将领领导力l在这位为1是, 为0的士兵一定会尊重它(高位有一位大于了, 整个数字一定大于), 接下去处理为1的士兵
如果将领在这位的值为1
对于为1的士兵, 异或得0, 为0的士兵异或得1, 接下来和过程就类似了
代码
#include <bits/stdc++.h> using namespace std; struct node { node * nxt[2]; int cnt; node() { cnt = 0; nxt[0] = nxt[1] = nullptr; } }; void insert(node * root, int x) { node * p = root; for(int i=31; i>=0; --i) { int t = bool(x & (1<<i)); if(p->nxt[t] == nullptr) p->nxt[t] = new node; p = p->nxt[t]; p->cnt++; } } void del(node * root, int x) { node * p = root; for(int i=31; i>=0; --i) { int t = bool(x & (1<<i)); p = p->nxt[t]; p->cnt--; } } int query(node * root, int x, int l) { node * p = root; int ret = 0; for(int i=31; i>=0; --i) { int tx = bool(x & (1<<i)); int tl = bool(l & (1<<i)); if(tx) { if(tl) { if(p->nxt[1]) ret += p->nxt[1]->cnt; p = p->nxt[0]; } else { p = p->nxt[1]; } } else { if(tl) { if(p->nxt[0]) ret += p->nxt[0]->cnt; p = p->nxt[1]; } else { p = p->nxt[0]; } } if(p==nullptr) break; } return ret; } int main() { int q, a, b, c; node * root = new node; for(scanf("%d", &q); q; --q) { scanf("%d%d", &a, &b); if(a==1) insert(root, b); else if(a==2) del(root, b); else { scanf("%d", &c); printf("%d\n", query(root, b, c)); } } return 0; }
相关文章推荐
- Educational Codeforces Round 23 E. Choosing The Commander (字典树)
- Educational Codeforces Round 23 F. MEX Queries(线段树区间设值)
- Educational Codeforces Round 23 A - Treasure Hunt
- Educational Codeforces Round 23 B - Makes And The Product
- Educational Codeforces Round 12 E. Beautiful Subarrays【字典树】
- Educational Codeforces Round 23 E. Choosing The Commander trie数
- Educational Codeforces Round 23 817-D Imbalanced Array 单调栈
- Educational Codeforces Round 23总结
- Educational Codeforces Round 23 补题小结
- Educational Codeforces Round 23 D. Imbalanced Array(单调栈)
- Educational Codeforces Round 23 A-F
- Educational Codeforces Round 23 B. Makes And The Product
- Educational Codeforces Round 23 E. Choosing The Commander (trie)
- Educational Codeforces Round 23 C. Really Big Numbers
- Codeforces Educational Round 23
- Educational Codeforces Round 23 C. Really Big Numbers 暴力
- Educational Codeforces Round 23 F. MEX Queries 离散化+线段树
- Educational Codeforces Round 23 E. Choosing The Commander(01Trie)
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理前缀+字典树优化★ ★
- Educational Codeforces Round 23 A. Treasure Hunt