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 ...
随机推荐
- BZOJ2729 [HNOI2012]排队 【高精 + 组合数学】
题目链接 BZOJ2729 题解 高考数学题... 我们先把老师看做男生,女生插空站 如果两个老师相邻,我们把他们看做一个男生,女生插空站 对于\(n\)个男生\(m\)个女生的方案数: \[n!m! ...
- 【BZOJ 1724】[Usaco2006 Nov]Fence Repair 切割木板 堆+贪心
堆对于stl priority_queue ,我们自己定义的类自己重载<,对于非自定义类我们默认大根堆,如若改成小根堆则写成std::priority<int,vector<int& ...
- 【BZOJ 1647】[Usaco2007 Open]Fliptile 翻格子游戏 模拟、搜索
第一步我们发现对于每一个格子,我们只有翻和不翻两种状态,我们发现一旦确定了第一行操作,那么第二行的操作也就随之确定了,因为第一行操作之后我们要想得到答案就得把第一行全部为0,那么第二行的每一个格子的操 ...
- 【BZOJ 3123】 [Sdoi2013]森林 主席树启发式合并
我们直接按父子关系建主席树,然后记录倍增方便以后求LCA,同时用并查集维护根节点,而且还要记录根节点对应的size,用来对其启发式合并,然后每当我们合并的时候我们都要暴力拆小的一部分重复以上部分,总时 ...
- remove computer from join with powershell
Removes the local computer from its domain. Remove-Computer [-UnjoinDomainCredential] <PSCredenti ...
- ActiveMQ(2) ActiveMQ创建HelloWorld
启动ActiveMQ: 请参见:ActiveMQ(1) 初识ActiveMQ 创建Maven工程: pom文件: <project xmlns="http://maven.apache ...
- windows 系统下git 的使用
前言: 最新版本的git for windows也是有界面的,不再是以前的纯命令行操作,但是我习惯了乌龟,所以感觉还是直接用乌龟比较方便点~~ 前提,已安装以下: git for windows,未安 ...
- flume高级组件及各种报错
1,one source two channel 创建conf文件,内容如下: #定义agent名, source.channel.sink的名称 access.sources = r1 access ...
- 【BZOJ4657】tower [网络流]
炮塔 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output 一行一个整数表示答案. Sample Input 4 5 0 ...
- set .net principle
var ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(FormsA ...