Codeforces #430 Div2 D
#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的更多相关文章
- Codeforces #430 Div2 C
#430 Div2 C 题意 给出一棵带点权的树,每一个节点的答案为从当前节点到根节点路径上所有节点权值的最大公因子(在求最大共因子的时候可以选择把这条路径上的任意一点的权值置为0).对于每一个节点单 ...
- 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 #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
随机推荐
- 什么是Redis的事务
一.什么是Redis的事务 可以一次执行多个命令,本质上是一组命令的集合.一个事务中的所有命令都会序列化,然后按顺序地串行化执行,而不会被插入其它命令. 二.Redis的事务可以做什么 一个队列中,一 ...
- Windows查看进程CMD命令和终止进程CMD命令
将小米路由器3刷机成openwrt的方法,请参考上篇文章< 家庭宽带多运营商接入方案>这里介绍怎么在已经刷成openwrt系统的小米路由器3上安装私有云nextcloud openwrt开 ...
- 网络编程:listen函数
listen函数仅由TCP服务器调用,它做两件事: 当socket函数创建一个套接字时,它被假设为一个主动套接字,也就是说,它是一个将调用connect发起连接的客户套接字.listen函数把一个未连 ...
- 安徽师大附中%你赛day5 T3 树上行走 解题报告
树上行走 题目背景 \(\mathrm{Smart}\) 的脑洞非常大, 经常幻想出一些奇怪的东西. 题目描述 某一天,\(\mathrm{Smart}\) 幻想出了一棵没有边际的二叉树,脑补着在那棵 ...
- poj 3422 洛谷P2045 K取方格数(方格取数加强版)
Description: 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来 ...
- CentOS 6通过yum升级Git
By francis_hao Mar 9,2017 在一个新机器上推送代码到github上时出现了下面的问题 error: The requested URL returned error: ...
- Java的外部类为什么不能使用private、protected进行修饰
对于顶级类(外部类)来说,只有两种修饰符:public和默认(default).因为外部类的上一单元是包,所以外部类只有两个作用域:同包,任何位置.因此,只需要两种控制权限:包控制权限和公开访问权限, ...
- Codeforces Round #526 (Div. 2) A.B
A. The Fair Nut and Elevator 题目链接:https://codeforces.com/contest/1084/problem/A 题意: 一栋房子有n层楼,同时有个电梯( ...
- oracle的rownum使用
对于rownum来说它是Oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...
- struts2学习笔记(二)
一. 国际化的目标 1). 如何配置国际化资源文件 I. Action 范围资源文件: 在Action类文件所在的路径建立名为 ActionName_language_country.properti ...