CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种:
+ x: 表示向集合中添加一个元素x
- x:表示删除集合中值为x的一个元素
? x:表示查询集合中与x异或的最大值为多少
析:这是一个字典树的应用,不过确实没看出来。。。。主要思想是这样,先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除,
剩下的就是查询,那么尽量让0和1XOR是最大的,所以,对于给定数,我们要去尽量他的XOR数,如果找到就加上,找不到,就找下一个。这样就是最大的。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <stack>
- using namespace std;
- typedef long long LL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const double inf = 0x3f3f3f3f3f3f;
- const LL LNF = 100000000000000000;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 4e6 + 5;
- const int mod = 1e9 + 7;
- const char *mark = "+-*";
- const int dr[] = {-1, 0, 1, 0};
- const int dc[] = {0, 1, 0, -1};
- int n, m;
- inline bool is_in(int r, int c){
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- inline LL Max(LL a, LL b){ return a < b ? b : a; }
- inline LL Min(LL a, LL b){ return a > b ? b : a; }
- inline int Max(int a, int b){ return a < b ? b : a; }
- inline int Min(int a, int b){ return a > b ? b : a; }
- int ch[maxn][2];
- int t[maxn];
- int cnt = 0;
- void add(int x){
- int k = 1;
- for(int i = 30; i >= 0; --i){
- int j = ((1<<i) & x) > 0;
- if(!ch[k][j]) ch[k][j] = ++cnt;
- k = ch[k][j];
- ++t[k];
- }
- }
- void del(int x){
- int k = 1;
- for(int i = 30; i >= 0; --i){
- int j = ((1<<i) & x) > 0;
- k = ch[k][j];
- --t[k];
- }
- }
- int query(int x){
- int k = 1;
- int ans = 0;
- for(int i = 30; i >= 0; --i){
- int j = ((1<<i) & x) == 0;
- if(t[ch[k][j]]){
- ans |= (1<<i);
- k = ch[k][j];
- }
- else k = ch[k][1-j];
- }
- return ans;
- }
- int main(){
- while(scanf("%d", &n) == 1){
- cnt = 1;
- memset(ch, 0, sizeof(ch));
- memset(t, 0, sizeof(t));
- add(0);
- while(n--){
- char s[3];
- int x;
- scanf("%s %d", s, &x);
- if(s[0] == '+') add(x);
- else if(s[0] == '-') del(x);
- else printf("%d\n", query(x));
- }
- }
- return 0;
- }
CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)的更多相关文章
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
题目链接: http://codeforces.com/contest/706/problem/D 题目大意: 三种操作,1.添加一个数,2.删除一个数,3.查询现有数中与x异或最大值.(可重复) 题 ...
- Codeforces 706D Vasiliy's Multiset(可持久化字典树)
[题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...
- Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- CodeForces 706D Vasiliy's Multiset
字典树. 比较经典的题目了.把每一个数字都插入到字典树中,询问的时候如果$x$的第$i$位是$p$,那么尝试着在字典树上往$pXOR1$的节点走下去,没有$pXOR1$节点的话再走$p$的.删除操作的 ...
- Codeforces 706 D. Vasiliy's Multiset (字典树贪心)
题目链接:http://codeforces.com/contest/706/problem/D 题意很简单不多说. 把一个数当作二进制插入字典树中,按照xor的性质,1找0,0找1,贪心即可. 注意 ...
- codeforces 706D D. Vasiliy's Multiset(trie树)
题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)
题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...
随机推荐
- Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-nu ...
- android中getSystemService详解
android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...
- Qt之启动外部程序
简述 QProcess可以用来启动外部程序,并与它们交互. 要启动一个进程,通过调用start()来进行,参数包含程序的名称和命令行参数,参数作为一个QStringList的单个字符串. 另外,也可以 ...
- 解决编译报错:Unable to copy file, because it is being used by another process.
Error 63 Unable to copy file "D:\DEV\XXX Website\trunk\4 Source Code\Common\WebControls\b ...
- Nginx - 指定log_format,常用于 Awstats 分析
1. vim /etc/nginx/nginx.conf (下面格式, Awstats 使用) log_format new_log '$remote_addr - $remote_user [$ti ...
- Swift入门篇-Hello World
提示:如果您使用手机和平板电脑看到这篇文章,您请在WIFI的环境下阅读,里面有很多图片, 会浪费很多流量. 博主语文一直都不好(如有什么错别字,请您在下评论)望您谅解,没有上过什么学的 最近这2天主要 ...
- Mac下开发常用目录
1:Snippets Xcode 代码段的文件表示 ~/Library/Developer/Xcode/UserData/CodeSnippets/ 2: Services 可以添加workf ...
- Hide Xcode8 strange log.
Product > Scheme > Edit Scheme Environment Variables set OS_ACTIVITY_MODE = disable
- IT经理,你在这个位置吗
事实上我离这个位置还远着,或者说它可能并不是我以后的方向,但是作为一个码农,这个发展路线还是需要了解的.主要的还是喜欢下面这个图,因为里面我的发展方向,有我的目标. 对 于一个IT从业者,让你谋得工作 ...
- Android-使用getIdentifier()获取资源Id
使用getIdentifier()获取资源Id int i= getResources().getIdentifier("icon", "drawable", ...