题目链接: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的更多相关文章

  1. CodeForces #367 div2 C

    题目链接: Hard problem 题意:每个字符串可以选择反转或者不反转,给出反转每个字符串的代价,问使最少的代价使得这个字符串序列成字典序. dp[i][j] = x : 第一维是第i个字符串, ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  4. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  7. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  8. Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)

    http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...

  9. Codeforces #430 Div2 D

    #430 Div2 D 题意 给出一些数,每次操作先将所有数异或一个值,再求这些数中没有出现过的最小的非负整数. 分析 对于更新操作,对于 \(x\) 所有为 \(1\) 的位给相应层添加一个标记,当 ...

随机推荐

  1. hibernate 实现多表连接查询(转载)

    http://www.cnblogs.com/lihuiyy/archive/2013/03/28/2987531.html 为了方便,直接粘过来,方便查看.不收藏了 Hibernate主要支持两种查 ...

  2. 什么是Servlet?它有哪些特点

    什么是Servlet?  它有哪些特点? Servlet是运行在JSP服务器端,用来生成Web页面的一种java程序 特点: (1)效率点 (2)功能强大 (3) Servlet之间能够共享数据 (4 ...

  3. ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL

    ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL 引言--- 在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流量. ...

  4. React高级特性

    目录: 容器组件 JSX可展开属性 动画 : CSS3 Transition 默认属性 复用代码:mixin 容器组件 React元素也可以包含其他的子元素,这意味着响应的React组件是一个 容器组 ...

  5. C#随学随记

    1.Microsoft.NET是基于Windows平台的一种技术(简称.NET),它包含了能在.NET Framework平台运行的所有语言..NET Framework是微软为开发应用程序创建的一个 ...

  6. android 内存处理工具

    1. LeakCanary 检查内存泄露 LeakCanary 是一个开源的在debug版本中检测内存泄漏的java库. 让我们来看看一个cait的例子: 1 2 3 4 5 6 7 8 9 10 1 ...

  7. hibernate的二级缓存

    缓存(Cache): 计算机领域非常通用的概念.它介于应用程序和永久性数据存储源(如硬盘上的文件或者数据库)之间,其作用是降低应用程序直接读写永久性数据存储源的频率,从而提高应用的运行性能.缓存中的数 ...

  8. Linq join on 多条件

    var a = from m in DbContext.Set<T1>() join q in DbContext.Set<T2>() on new { m.ID, Phone ...

  9. 调用discuz编辑器再也不是问题了

    前面讲了如何开发一个discuz的特殊主题插件,详情可在此查看discuz特殊主题插件开发步骤和犯的愚蠢错误.上一篇文章讲解的是一些简单的开发步骤,不涉及到具体的编码.网页编辑器之类的都是系统默认带过 ...

  10. Hadoop笔记HDFS(2)

    高级Hadoop MapReduce管理 1 调试部署好的Hadoop的配置 2 运行基准测试检验Hadoop的安装 3 重新利用JVM提升性能 4 容错性 5 调试脚本-分析失败任务原因 6 设置失 ...