CodeForces 706D Vasiliy's Multiset
来源:互联网 发布:安全数据交换控制系统 编辑:程序博客网 时间:2024/05/16 14:09
Author has gone out of the stories about Vasiliy, so here is just a formal task description.
You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries:
- "+ x" — add integer x to multiset A.
- "- x" — erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query.
- "? x" — you are given integer x and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.
Multiset is a set, where equal elements are allowed.
The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform.
Each of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer xi (1 ≤ xi ≤ 109). It's guaranteed that there is at least one query of the third type.
Note, that the integer 0 will always be present in the set A.
For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.
10+ 8+ 9+ 11+ 6+ 1? 3- 8? 3? 8? 11
11101413
After first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.
The answer for the sixth query is integer — maximum among integers , , , and .
#include <iostream>#include <cstdio>#include <cstring>#include <malloc.h>using namespace std;const int maxn=2;struct trie { trie *next[maxn];//指向该节点的下两个节点 int v;//该节点的访问次数};inline int read() {//读入外挂 int x = 0, f = 1; char ch = getchar(); while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x*f;}inline void bulid(trie *root,int num) { trie *p=root; for(int i=30;i>=0;i--) { int id=((1<<i)&num)>>i;//取num的二进制第i位 if(p->next[id]==nullptr) {//num的二进制第i位不存在于字典树,新建节点 trie *q=(trie *)malloc(sizeof(trie)); for(int j=0;j<maxn;j++) q->next[j]=nullptr;//新节点的下面的节点置为nullptr q->v=1;//新节点的访问次数置为1 p->next[id]=q;//p指向下一节点 p=p->next[id]; } else { p->next[id]->v++;//否则该节点的访问次数++ p=p->next[id]; } }}inline int ask(trie *root,int num) { int res=0;//异或运算的性质,按如下方式取节点,如果num的第i位为1,则取下一节点值为0的节点,并res+=(1<<i),没有0节点取1节点,但是不加,反之亦然 trie *p=root; for(int i=30;i>=0;i--) { int id = ((1 << i)&num) >> i; if(id) { if(p->next[0]!=nullptr) { p=p->next[0]; res+=(1<<i); } else p=p->next[1]; } else { if(p->next[1]!=nullptr) { p=p->next[1]; res+=(1<<i); } else p=p->next[0]; } } return res;}inline void del(trie *root,int num) { trie *p=root;//删除操作时找到第一个访问次数为1的节点,将其置为nullptr for(int i=30;i>=0;i--) { int id=((1<<i)&num)>>i; if(p->next[id]!=nullptr) { if(p->next[id]->v>1) { p->next[id]->v--; p=p->next[id]; } else { if(p->next[id]->v==1) { p->next[id]=nullptr; return ; } } } }}void dele(trie* T) { int i; if (T == NULL) return ;//释放字典树的储存空间,非必需 for (i = 0; i<maxn; i++) if (T->next[i] != NULL) dele(T->next[i]); free(T); return ;}int main() { int n; n=read(); trie *root=(trie *)malloc(sizeof(trie)); for(int i=0;i<maxn;i++) root->next[i]=nullptr; bulid(root,0);//先把0存入字典树,否则缺值,也是题目要求 while(n--) { char op[2]; int num; scanf("%s",op); num=read(); if(op[0]=='+') bulid(root,num);//+操作,bulid字典树 if(op[0]=='-') del(root,num);//-操作,删除字典树的num节点 if(op[0]=='?') printf("%d\n",ask(root,num));//询问num异或的最大值 } dele(root); return 0;}
- Codeforces-706D Vasiliy's Multiset
- CodeForces 706D Vasiliy's Multiset
- 706D. Vasiliy's Multiset
- CodeForces 706D Vasiliy's Multiset 字典树
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
- Codeforces 706D-Vasiliy's Multiset(Trie树)
- Codeforces 706D Vasiliy's Multiset (字典树求异或最大值)
- CodeForces 706D Vasiliy's Multiset(0/1树)
- Codeforces 706D Vasiliy's Multiset【贪心+字典树】
- Codeforces Round #367 D. Vasiliy's Multiset
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (multiset)
- CF 706D Vasiliy's Multiset
- 【35.20%】【CF 706D】Vasiliy's Multiset
- CodeForces 367D Vasiliy's Multiset Trie树
- Codeforces 706D Vasiliy's Multiset(异或字典树)
- codeforces 706D. Vasiliy's Multiset 带删除操作的字典树(真模版)
- Codeforces 706D Vasiliy's Multiset(异或字典树)
- 【Codeforces Round 367 (Div 2) D】【字典树典型题】Vasiliy's Multiset
- 关于用ST-Link V2下载出现internal command error和keil无法识别ST—LINK的问题解决法法如下
- Win7下安装linux双系统
- myeclipse egit不能pull解决
- Objective-C Autorelease Pool 的实现原理
- Maven依赖排查
- CodeForces 706D Vasiliy's Multiset
- JAVA学习代码——方法
- <Android 基础(十九)> CoordinatorLayout
- codeforces603CLieges of Legendre+SG博弈
- VirtualBox中安装Window10系统出错
- Search in Rotated Sorted Array
- Netty之WebSocket开发
- Android eclipse 基本项目结构
- 最后一个字符——奇虎360笔试题