#430 Div2 D

题意

给出一些数,每次操作先将所有数异或一个值,再求这些数中没有出现过的最小的非负整数。

分析

对于更新操作,对于 \(x\) 所有为 \(1\) 的位给相应层添加一个标记,当查询时走到这一层,如果有这个标记,就要把左子树当作右子树,右子树当做左子树。

对于查询操作,显然优先走左子树,如果左子树已经满了,才走右子树,并且答案就要加上对应的层的值,如果发现无路可走,说明后面可以全部取\(0\)了。

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 100;
struct Trie {
int val[MAXN], nxt[MAXN][2], L, root, siz[MAXN], flg[21];
int newnode() {
memset(nxt[L], -1, sizeof nxt[L]);
return L++;
}
void init() {
L = 0;
root = newnode();
}
void insert(int x) {
int now = root;
for(int i = 20; i >= 0; i--) {
int o = ((x >> i) & 1);
if(nxt[now][o] == -1) {
nxt[now][o] = newnode();
}
now = nxt[now][o];
siz[now]++;
}
}
void update(int q) {
for(int i = 20; i >= 0; i--) {
if((q >> i) & 1) {
flg[i] ^= 1;
}
}
}
int query() {
int now = root;
int ans = 0;
for(int i = 20; i >= 0; i--) {
int o = 0;
if(flg[i]) o = !o;
if(nxt[now][o] == -1) {
break;
} else {
if(siz[nxt[now][o]] < (1 << i)) {
now = nxt[now][o];
} else {
ans |= (1 << i);
if(nxt[now][!o] == -1) break;
else now = nxt[now][!o];
}
}
}
return ans;
}
}trie;
set<int> num;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
num.insert(x);
}
trie.init();
for(int i : num) {
trie.insert(i);
}
while(m--) {
int q;
cin >> q;
trie.update(q);
cout << trie.query() << endl;
}
return 0;
}

Codeforces #430 Div2 D的更多相关文章

  1. Codeforces #430 Div2 C

    #430 Div2 C 题意 给出一棵带点权的树,每一个节点的答案为从当前节点到根节点路径上所有节点权值的最大公因子(在求最大共因子的时候可以选择把这条路径上的任意一点的权值置为0).对于每一个节点单 ...

  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 #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. 【题解】SDOI2010地精部落

    强!强!强!强!劲啊劲啊劲啊!!!洛谷P2467 非常重要的,就在于发现以下的两条性质: 1.当i与i+1不相邻时,方案数是一样的:交换这两个数,<i+1的必然<i,>i+1的必然& ...

  2. [bzoj4860] [BeiJing2017]树的难题

    Description 给你一棵 n 个点的无根树.树上的每条边具有颜色. 一共有 m 种颜色,编号为 1 到 m.第 i 种颜色的权值为 ci.对于一条树上的简单路径,路径上经过的所有边按顺序组成一 ...

  3. [Leetcode] Convert sorted list to binary search tree 将排好的链表转成二叉搜索树

    ---恢复内容开始--- Given a singly linked list where elements are sorted in ascending order, convert it to ...

  4. BZOJ2286 [Sdoi2011]消耗战 【虚树 + 树形Dp】

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 4261  Solved: 1552 [Submit][Sta ...

  5. [NOI2002] 银河英雄传说 (带权并查集)

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...

  6. angularjs的验证信息的写法

    <div ng-messages="alarmDelayForm.alarmRuleName.$error" role="alert"> <d ...

  7. 忘记mysq rootl密码后解决办法

    如果mysql正在运行,/etc/init.d/mysqld stop 启动mysql(无需输入密码):bin/safe_mysqld –skip-grant-tables & 在bin目录下 ...

  8. Linux引导过程

    早期时,启动一台计算机意味着要给计算机喂一条包含引导程序的纸带,或者手工使用前端面板地址/数据/控制开关来加载引导程序.尽管目前的计算机已经装备了很多工具来简化引导过程,但是这一切并没有对整个过程进行 ...

  9. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿 ...

  10. POJ2154 Color

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10322   Accepted: 3360 Description Bead ...