Educational Codeforces Round 23 817E. Choosing The Commander 字典树 位运算
来源:互联网 发布:知乎首页 编辑:程序博客网 时间:2024/05/18 13:31
题目链接: Choosing The Commander
题目大意
士兵有一个个性值p
将领有一个个性值p, 领导力l
(
如果将领和士兵个性值按位异或的得到的值小于将领的领导力, 这个士兵会尊重这个将领
三种操作:
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;}
阅读全文
0 0
- Educational Codeforces Round 23 817E. Choosing The Commander 字典树 位运算
- Educational Codeforces Round 23 E. Choosing The Commander (字典树)
- Educational Codeforces Round 23 E. Choosing The Commander(01Trie)
- CodeForces 817E Choosing The Commander题解
- [Codeforces 817E] Choosing The Commander Trie树
- Educational Codeforces Round 12 E. Beautiful Subarrays【字典树】
- Educational Codeforces Round 12 E trie树
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理前缀+字典树优化★ ★
- Educational Codeforces Round 29 E. Turn Off The TV
- Educational Codeforces Round 21E
- Educational Codeforces Round 26 E
- Educational Codeforces Round 903E
- Codeforces Round #190 (Div. 2) E. Ciel the Commander 题目与题解翻译
- Educational Codeforces Round 6(E)DFS序,线段树
- CF Educational Codeforces Round 6 E题 dfs+线段树
- [博弈 杂题] Codeforces 794E Round #414 E. Choosing Carrot
- Educational Codeforces Round 12 E. Beautiful Subarrays
- Educational Codeforces Round 12-E. Beautiful Subarrays
- 连涨几个月后 香港楼市增速或将放缓
- Kubernetes初探:原理及实践应用
- 51Nod 1088 最长回文子串
- 【ZOJ2112】Dynamic Ranking(主席树)
- 《KyLin学习理解》-02-KyLin的网页界面使用
- Educational Codeforces Round 23 817E. Choosing The Commander 字典树 位运算
- Hrbust 1401 九连环【递推+矩阵快速幂】
- SQLite基础知识
- 对象的生命历程
- idea里web.xml中的classpath:xml/applicationContext.xml文件路径找不到
- 项目流程
- 用Python 2.7进行科学计算与曲线绘制
- [Cousera ML Notetaking] Supervised Learning & Unsupervised Learning
- 错误代码#1045 Access denied for user 'root'@'localhost' (using password:YES)