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\) 的位给相应层添加一个标记,当 ...
随机推荐
- Spring MVC 入门示例讲解
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
- csuoj 1503: 点到圆弧的距离
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 1503: 点到圆弧的距离 时间限制: 1 Sec 内存限制: 128 MB Speci ...
- Pyhton 学习总结 21 :fileinput模块
fileinput模块可以对一个或多个文件中的内容进行迭代.遍历等操作.该模块的input()函数有点类似文件readlines()方法,区别在于前者是一个迭代对象,需要用for循环迭代,后者是一次性 ...
- 将Cocos2dX渲染到MFC窗口上
引用:http://www.cnblogs.com/windeer/archive/2012/11/18/2767750.html 引言 现在智能手机已经慢慢进入大众化,移动类应用开始火爆起来,游戏类 ...
- tar命令参数笔记
是指的linux下的tar命令,该命令的用法相当多,以下的内容来自tar的info手册 --numeric-owner This option will notify 'tar' thar it sh ...
- yield return的作用
测试1: using UnityEngine; using System.Collections; public class test1 : MonoBehaviour { // Use this f ...
- js 图片懒加载
图片懒加载(图片出现在可视区域再加载) 兼容性:兼容目前流行的全部浏览器,包括:兼容到IE6) 使用方法: 引入相应的js文件<script src="js/lazy.js" ...
- bat 命令分行写
myprog parameter parameter parameter parameter parameter parameter parameter parameter parameter par ...
- ShareSDK第三方登录代码
- (IBAction)YYSJBut:(UIButton *)sender{ if (sender.tag == 7) { [self AuthLogin:SSDKPlat ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序使用异步及存储过程
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第九篇:为ASP.NET MVC应用程序 ...