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\) 的位给相应层添加一个标记,当 ...
随机推荐
- url.py中的配置问题
urlpatterns = [ url(r'^add$', calc_views.add, name='add'), url(r'^admin/', admin.site.urls), ...
- C# 窗体(登录界面)
首先拖动一个 lable(写用户名) 后面 跟一个Textbox 再lable(写密码) 后面 跟一个Textbox(需设置一下属性—行为—useSystemPasswordChar(默认输入的密 ...
- tp生成验证码
视图层: <input type="text" name="code" value="" /> <img o ...
- 02-FPGA设计流程介绍——小梅哥FPGA设计思想与验证方法视频教程配套文档
芯航线——普利斯队长精心奉献 课程目标: 1.了解并学会FPGA开发设计的整体流程 2.设计一个二选一选择器并进行功能仿真.时序仿真以及板级验证 实验平台:芯航线FPGA开发板.杜邦线 实验内容: 良 ...
- 转 java中的session
书中讲:以下情况,Session结束生命周期,Servlet容器将Session所占资源释放:1.客户端关闭浏览器2.Session过期3.服务器端调用了HttpSession的invalidate( ...
- YOU KNOW NOTHING , SNOW
JVM运行时数据区域 方法区: 用 于存储虚拟机加载的类信息,常量,静态变量,JIT编译后的代码,所有线程共享 堆:所有线程共享,用来存储实例对象. 虚拟机栈:线程私有,生命周期与线程相同,每个方法被 ...
- JuQueen(线段树 lazy)
JuQueen Time Limit: 5 Sec Memory Limit: 512 MB Description Input Output Sample Input 10 10 5 state ...
- Android动画View Animation
Animations 一.Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转.缩放.淡入淡出等, ...
- web前段 弹出小例子
<html> <head> <meta charset="utf-8"> <meta name="viewport" ...
- Displaying Window In Center In Oracle Forms 6i
Center window automatically in Oracle Forms 6i, use the following procedure by passing window name ...