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\) 的位给相应层添加一个标记,当 ...
随机推荐
- Leetcode: 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- CSS3 border-image详解、应用
一.border-image的兼容性 border-image可以说是CSS3中的一员大将,将来一定会大放光彩,其应用潜力真的是非常的惊人.可惜目前支持的浏览器有限,仅Firefox3.5,chrom ...
- 【转】PowerShell入门(七):管道——在命令行上编程
转至:http://www.cnblogs.com/ceachy/archive/2013/02/22/PowerShell_Pipeline.html 管道对于Shell来说是个化腐朽为神奇的东西, ...
- NSDate获取当前时区的时间
[NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...
- [问题2014A02] 复旦高等代数 I(14级)每周一题(第四教学周)
[问题2014A02] 求下列 \(n\) 阶行列式的值, 其中 \(a_i\neq 0\,(i=1,2,\cdots,n)\): \[ |A|=\begin{vmatrix} 0 & a_ ...
- [问题2014S04] 复旦高等代数II(13级)每周一题(第四教学周)
[问题2014S04] 设 \(A\in M_n(\mathbb{C})\) 为可对角化的 \(n\) 阶复方阵, \(f(x)\in\mathbb{C}[x]\) 为复系数多项式, 证明: \[B ...
- printf("%*s%s%*s",——)是什么?
我们可能知道scanf里用*修饰符,是起到过滤读入的作用.比如一个有三列数值的数据,我只想得到第2列数值,可以在循环里用scanf(“%*d%d%*d”, a[i])来读入第i行的第2个数值到a[i] ...
- 关于SQLite数据库的作业
数据库的SQL预览代码我忘了复制了 只能截图 Students表: Course表: Score表: Teachcer表:
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- (三)kafka集群扩容后的topic分区迁移
kafka集群扩容后的topic分区迁移 kafka集群扩容后,新的broker上面不会数据进入这些节点,也就是说,这些节点是空闲的:它只有在创建新的topic时才会参与工作.除非将已有的partit ...