http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D

题目:就是有3种操作

+ x向集合里添加 x

- x 删除x元素,(保证存在

? x 查询 x |  集合中元素的最大值

思路:就是利用字典树,从高位到低位进行贪心。 比如说给一个数 x=3  , 对x 各位取反(二进制)(x=~x ),

于是就是 1-0-0-1; 拿 1-0-0-1,从左到右(从高位到地位)顺序,来在字典树中寻找。如果能找到(if ),就接着找下去;

如果找不到(else),就退而求其次(贪心),找另外一个分岔点。

在构造的时候从32或者31开始,大于1e9,相当于0-0-0-0-0-0-0-0-0-0-0-0-1-0-1-1 这样 ,在树中 前面多几个0没关系。

不过数组要尽量开大一点(尽管我不知道具体应该开多少)

 #include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const int INF= 0x3f3f3f3f;
const int N=4e6+; int n,cnt=;
int trie[N][]={},num[N]={}; void insert(int c,ll x)
{
int rt=;
for(int i=;i>=;i--)
{
int k=(x>>i)&; //二进制高位到低位的 0 1情况
if(!trie[rt][k])
trie[rt][k]=cnt, cnt++;
rt=trie[rt][k]; num[rt]+=c;
}
} ll find(ll x)
{
x=~x;
int rt=;
ll ans=;
for(int i=;i>=;i--)
{
int k=(x>>i)&;
ans=ans<<;
if( num[trie[rt][k] ] && trie[rt][k] )
{
ans++;
rt=trie[rt][k];
}
else
rt=trie[rt][-k]; //贪心
}
return ans;
}
int main()
{
cin>>n;
char c;
ll x; insert(,);
for(int i=;i<=n;i++)
{
cin>>c>>x;
if(c=='+')
insert(,x);
else if(c=='-')
insert(-,x);
else
printf("%lld\n",find(x));
//cout<<c<<x<<endl;
}
}

Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset

    题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...

  5. 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 ...

  6. 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 ...

  7. codeforces 706D D. Vasiliy's Multiset(trie树)

    题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...

  8. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  9. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

随机推荐

  1. 接口请求报错 504 Gateway Time-out

    最近程序接口请求报了一个错误,如图 很明显的请求超时,以前也没出现过这个问题,突然就报了这个错,很懵. 百度之后网上说是nginx的问题,然后突然想起来,因为业务需要我在nginx里配了接口的转发. ...

  2. SpringBoot RequestBody ajax提交对象

    前端实现: var student = { "name":1, "age":2, "score":3 }; $.ajax({ url:&qu ...

  3. linux 文件描述符表 打开文件表 inode vnode

      在Linux中,进程是通过文件描述符(file descriptors,简称fd)而不是文件名来访问文件的,文件描述符实际上是一个整数.Linux中规定每个进程能最多能同时使用NR_OPEN个文件 ...

  4. box-shadow 用法总结

    一.基础知识 box-shadow 属性向框添加一个或多个阴影. 语法 box-shadow: offset-x offset-y blur spread color inset; box-shado ...

  5. 【转】MySQL中EXISTS的用法

    原文链接:https://www.cnblogs.com/qlqwjy/p/8598091.html 比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,Compan ...

  6. (二)Spring Boot 官网文档学习之入门

    文章目录 Spring Boot 是什么 系统要求 Servlet 容器 Maven方式安装Spring Boot 编写第一个 Spring Boot 项目 原文:https://docs.sprin ...

  7. 修改织梦DedeCMS投票漏洞

    织梦/dedecms系统我们都知道是有很多漏洞的,我在调试投票功能的时候正好要用到投票功能,这不就出现了漏洞,下面我就给大家展示如何修复这个织梦投票漏洞 首先我们打开//dedevote.class. ...

  8. 机器学习-EM算法-pLSA模型笔记

    pLSA模型--基于概率统计的pLSA模型(probabilistic Latent Semantic Analysis,概率隐语义分析),增加了主题模型,形成简单的贝叶斯网络,可以使用EM算法学习模 ...

  9. react组件懒加载

    组件懒加载方式-:react新增的lazy const Alert = lazy(() => import('./components/alert')); export default func ...

  10. 面试必问:Golang高阶-Golang协程实现原理

    引言 实现并发编程有进程,线程,IO多路复用的方式.(并发和并行我们这里不区分,如果CPU是多核的,可能在多个核同时进行,我们叫并行,如果是单核,需要排队切换,我们叫并发) 进程和线程的区别 进程是计 ...