Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D
题意:
有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给定的数的异或值最大。
思路:
因为这道题目涉及到删除操作,所以用一个变量cnt来记录前缀的数量,加入时就+1,删除时就减1。查询时前缀数量>0时就说明是存在的。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = + ;
typedef long long ll;
int num = ; struct Trie
{
int son[];
int cnt;
}t[*maxn]; void init(int x)
{
t[x].cnt = ;
memset(t[x].son,,sizeof(t[x].son));
} void insert(ll x)
{
int u = ;
for(int i=;i>=;i--)
{
int c = ((x>>i)&);
if(!t[u].son[c])
{
num++;
init(num);
t[u].son[c] = num;
}
u = t[u].son[c];
t[u].cnt++;
}
} void del(ll x)
{
int u = ;
for(int i=;i>=;i--)
{
int c = ((x>>i)&);
u = t[u].son[c];
t[u].cnt--;
}
} ll query(ll x)
{
ll ans = ;
int u = ;
for(int i=;i>=;i--)
{
int c = ((x>>i)&);
if(t[u].son[c^] && t[t[u].son[c^]].cnt)
{
ans+=(<<i);
u = t[u].son[c^];
}
else u = t[u].son[c];
}
return ans;
} int main()
{
//freopen("in.txt","r",stdin);
int n;
scanf("%d",&n);
insert();
while(n--)
{
char op[]; ll x;
scanf("%s%lld",op,&x);
if(op[]=='+') insert(x);
else if(op[]=='-') del(x);
else if(op[]=='?') printf("%lld\n",query(x));
}
return ;
}
Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)的更多相关文章
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset
题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)
http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)
E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
随机推荐
- Linux基础命令---pgrep
pgrep pgrep指令可以按名字或者其他属性搜索指定的进程,显示出进程的id到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...
- Linux基础命令---chsh
chsh 改变用户登录时使用的shell,默认使用bash.如果命令行上没有给出shell,chsh将提示输入一个shell.chsh将接受系统上任何可执行文件的完整路径名.但是,如果shell未在“ ...
- websocket 群聊单聊
websocket 介绍 介绍引自 https://segmentfault.com/a/1190000012709475 群聊 from flask import Flask, request, r ...
- bzoj4445 小凸想跑步
题目链接 半平面交,注意直线方向!!! 对于凸包上任意一条边$LINE(p_i,p_{i+1})$都有$S_{\Delta{p_i} {p_{i + 1}}p} < S_{\Delta{p_0} ...
- How to install john deere service advisor 4.2.005 on win 10 64bit
How to install john deere service advisor 4.2.005 with the February 2016 data base disks on a machin ...
- pytest+request 接口自动化测试
1.安装python3brew update brew install pyenv 然后在 .bash_profile 文件中添加 eval “$(pyenv init -)” pyenv insta ...
- java之分隔符问题
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ ^ 报这个错的原因是因为在java中“ ...
- slideDown留言板
<!doctype html> <html lang="en"> <head> <meta http-equiv="Conten ...
- Python 内置函数sorted()在高级用法
对于Python内置函数sorted(),先拿来跟list(列表)中的成员函数list.sort()进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. ...
- Pytorch-学习记录 卷积操作 cnn output_channel, etc.
参考资料: pytorch中文文档 http://pytorch-cn.readthedocs.io/zh/latest/