CodeForces #367 div2 D Trie
题目链接:Vasiliy's Multiset
题意:这里有一个set容器,有三种操作,+ num, - num, ? num,分别代表往容器里加上num,或者拿走num,或着从容器里找一个数temp使得temp^num的值最大。输出这个最大值。
思路:对于XOR操作,一般都要拆位考虑,拆完之后用Trie或者线段树维护,然后,这个题把每个数的二进制30位(前面不够的用0补全)插入Trie,查询的时候,对于每一位先尝试往相反的方向走,因为异或 只要越高位
相反值越大,然后再尝试往相同的方向走。最后如果没有找到这个数的话,ans即为该数字本身,因为0一直在容器里。
tree[maxn][2],maxn应为n*30,因为一个数字占30个节点,总结点数不会超过n*30.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define maxn 200010*30
using namespace std; int tree[maxn][2]; // Trie
int cnt[maxn]; // 每个节点的经过次数
int val[maxn]; //以每个节点为终点的数字
int tot; // 每个节点的编号
int num;
int now[30]; // 保存每个数字的30位 void update(int x, int type) {
int rt = 0; // 插入当前数字时的地址
num = 0;
int t = x;
memset(now, 0, sizeof(now));
while(t) {
now[num++] = t%2;
t /= 2;
}
for (int i=29; i>=0; --i) {
int temp = now[i];
cnt[rt] += type;
if (!tree[rt][temp]) {
tree[rt][temp] = tot++;
}
rt = tree[rt][temp];
}
cnt[rt] += type;
val[rt] = x;
} int query(int x) {
int t = x;
int rt = 0;
num = 0;
memset(now, 0, sizeof(now));
while(t) {
now[num++] = t%2;
t /= 2;
}
int ans = 0;
for (int i=29; i>=0; --i) {
int temp = now[i];
if ((tree[rt][temp^1]) && cnt[tree[rt][temp^1]]) {
rt = tree[rt][temp^1];
}
else if ((tree[rt][temp]) && cnt[tree[rt][temp]]) {
rt = tree[rt][temp];
}
else {
ans = -1;
break;
}
}
if (ans != -1) {
ans = max(val[rt]^x, x);
}
else ans = x;
return ans;
} int main() {
// freopen("in.cpp", "r", stdin);
int n;
while(~scanf("%d", &n)) {
tot = 1;
memset(tree, 0, sizeof(tree));
memset(cnt, 0, sizeof(cnt));
memset(val, -1, sizeof(val)); for (int i=0; i<n; ++i) {
char op;
int temp;
getchar();
scanf("%c%d", &op, &temp);
//cout << op << "==" << temp << endl;
if (op == '+') {
update(temp, 1);
}else if (op == '-') {
update(temp, -1);
}else {
int ans = query(temp);
printf("%d\n", ans);
}
}
}
return 0;
}
CodeForces #367 div2 D Trie的更多相关文章
- CodeForces #367 div2 C
题目链接: Hard problem 题意:每个字符串可以选择反转或者不反转,给出反转每个字符串的代价,问使最少的代价使得这个字符串序列成字典序. dp[i][j] = x : 第一维是第i个字符串, ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)
http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...
- Codeforces #430 Div2 D
#430 Div2 D 题意 给出一些数,每次操作先将所有数异或一个值,再求这些数中没有出现过的最小的非负整数. 分析 对于更新操作,对于 \(x\) 所有为 \(1\) 的位给相应层添加一个标记,当 ...
随机推荐
- 什么是Servlet?它有哪些特点
什么是Servlet? 它有哪些特点? Servlet是运行在JSP服务器端,用来生成Web页面的一种java程序 特点: (1)效率点 (2)功能强大 (3) Servlet之间能够共享数据 (4 ...
- js 获取 根目录
//js获取项目根路径,如: http://localhost:8083/uimcardprjfunction getRootPath(){ //获取当前网址,如: http://localhost: ...
- POJ - 2339 Rock, Scissors, Paper
初看题目时就发了个错误,我因为没有耐心看题而不了解题目本身的意思,找不到做题的突破口,即使看了一些题解,还是没有想到方法. 后来在去问安叔,安叔一语道破天机,问我有没有搞清题目的意思,我才恍然大悟,做 ...
- POJ 3281:Dining(最大流)
http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...
- 使用Android studio 出现的问题解决
问题一.安装过程中出现的报错Failed to install Intel HAXM 解决:重启电脑,启动BIOS,我的是惠普的,开机时按F10就进去了 切换到Configuration选项,将设置I ...
- -webkit-transform:scale(1.04)放大缩小效果
<p>[鼠标移动进去图片放大一倍效果:运用了-webkit-transform:scale(1.04)效果]</p> 如图:鼠标移动上去的时候图片放大一倍的效果, <!D ...
- OpenCV安装配置的简单记录
在ubuntu16.04下安装OpenCV 2.4.11的简单记录 1. 安装cmake,执行$apt-get install cmake即可,cmake -version验证 2. 下载OpenCV ...
- iOS 编码规范
Coding Guidelines for Cocoa https://developer.apple.com/library/prerelease/content/documentation/Coc ...
- jquery.validate使用 - 2
jQuery.validate.js API说明 参考http://ideabean.javaeye.comPlugin methods Name Type validate( options ) R ...
- 。【自学总结 1】------3ds Max 界面
3ds Max 界面包含4部分(7区域) 4部分:菜单.控制工具.命令面板.窗口区 7区域: 1.标题栏:主要用于显示当前工作文件的名称,可以看到文件存储路径. 2.菜单栏:菜单中的命令如果带有省略号 ...