Educational Codeforces Round 23 E. Choosing The Commander(01Trie)
来源:互联网 发布:外链网盘系统源码 编辑:程序博客网 时间:2024/06/05 14:47
题意:三个操作:1增加一个数,2删除一个数,3求所有数^k<l的个数
思路:01Trie,求^k<l的个数时,如果l为1则加上0的,并往1走,如果l为0,就往0走。
代码:
#include<bits/stdc++.h>using namespace std;const int maxnode = 1e5*32+5;int ch[maxnode][2], val[maxnode], sz;void init(){ sz = 1; memset(ch[0], 0, sizeof(ch[0]));}void Insert(int x){ int u = 0; for(int i = 31; i >= 0; i--) { int idx = (x>>i)&1; if(!ch[u][idx]) { memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][idx] = sz++; } u = ch[u][idx]; val[u]++; }}void Delete(int x){ int u = 0; for(int i = 31; i >= 0; i--) { int idx = (x>>i)&1; u = ch[u][idx]; val[u]--; }}int Match(int x, int y){ int u = 0, ans = 0; for(int i = 31; i >= 0; i--) { int idxx = (x>>i)&1; int idxy = (y>>i)&1; if(!idxy) { if(!ch[u][idxx]) break; u = ch[u][idxx]; } else { if(ch[u][idxx]) ans += val[ch[u][idxx]]; if(!ch[u][!idxx]) break; u = ch[u][!idxx]; } } return ans;}int main(void){ int q; while(cin >> q) { init(); while(q--) { int cmd, x, y; scanf("%d%d", &cmd, &x); if(cmd == 1) Insert(x); else if(cmd == 2) Delete(x); else { scanf("%d", &y); printf("%d\n", Match(x, y)); } } } return 0;}
阅读全文
1 0
- Educational Codeforces Round 23 E. Choosing The Commander(01Trie)
- Educational Codeforces Round 23 E. Choosing The Commander (字典树)
- Educational Codeforces Round 23 817E. Choosing The Commander 字典树 位运算
- [Codeforces 817E] Choosing The Commander Trie树
- CodeForces 817E Choosing The Commander题解
- Educational Codeforces Round 12 E trie树
- Educational Codeforces Round 16 (A-E)
- Educational Codeforces Round 29 E. Turn Off The TV
- Educational Codeforces Round 21E
- Educational Codeforces Round 26 E
- Educational Codeforces Round 903E
- Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)
- codeforces Educational Codeforces Round 16-E(DP)
- Codeforces Round #190 (Div. 2) E. Ciel the Commander 题目与题解翻译
- 01背包 Educational Codeforces Round 21 E. Selling Souvenirs
- Educational Codeforces Round 5 E. Sum of Remainders(数学)
- Educational Codeforces Round 1 E Chocolate Bar(dp)
- Educational Codeforces Round 6(E)DFS序,线段树
- Activity的创建(从Activity的角度理解IWindowSession)
- 【数据挖掘知识点一】数据分布特征的描述
- 提示音标签使用
- 判断是否已经序列化 php
- 第一周
- Educational Codeforces Round 23 E. Choosing The Commander(01Trie)
- Java基础-----集合Arraylist
- hdu 2844 多重背包
- HttpClient使用详解
- 【js工具类方法】截取地址栏参数
- 剑指offer——两个链表的第一个公共结点
- Thymeleaf简介
- php yii mongoid 查询处理
- TortoiseSVN 1.9.5安装 与 Eclipse4.4.2中安装SVN插件 图解详解