Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
2017-08-30 11:52
519 查看
题目链接:Vitya and Strange Lesson
题意:给出一个序列,询问每次xor一个数以后的序列的mex(mex是集合中最小的没有出现的自然数)
题解:由于每一次询问以后序列都会改变,我们可以把每一个询问看成从最开始询问的数的前缀异或和,这样我们每一次操作的对象就是同一个序列了。考虑把原序列建立一棵Trie,那么显然,我们可以知道,二进制表示具有某一个特定前缀的数有多少个(即Trie的某一个节点的子树有多少片叶子),我们把询问在Trie中查找,看当前节点的下一个要经过的节点的子树是不是一个满二叉树,如果是,那么改变下一个节点的前进方向并把对应的值加入贡献即可。
题意:给出一个序列,询问每次xor一个数以后的序列的mex(mex是集合中最小的没有出现的自然数)
题解:由于每一次询问以后序列都会改变,我们可以把每一个询问看成从最开始询问的数的前缀异或和,这样我们每一次操作的对象就是同一个序列了。考虑把原序列建立一棵Trie,那么显然,我们可以知道,二进制表示具有某一个特定前缀的数有多少个(即Trie的某一个节点的子树有多少片叶子),我们把询问在Trie中查找,看当前节点的下一个要经过的节点的子树是不是一个满二叉树,如果是,那么改变下一个节点的前进方向并把对应的值加入贡献即可。
#include <bits/stdc++.h> using namespace std; const int N = 300005; struct Trie{ int nxt[2],sz; void init(){ nxt[0]=nxt[1]=sz=0; } }a[N*20]; bool vis[1<<21]; int id,n,m,pre,k; void Ist(int x){ int now=0; vector<int>t; for(int i=1;i<=20;i++){ t.push_back(x%2); x/=2; } for(int i=19;i>=0;i--){ if(!a[now].nxt[t[i]%2]) a[now].nxt[t[i]%2]=++id; now=a[now].nxt[t[i]%2]; } a[now].sz=1; } void dfs(int last,int now){ for(int i=0;i<2;i++){ if(a[now].nxt[i]) dfs(now,a[now].nxt[i]); a[now].sz+=a[a[now].nxt[i]].sz; } } int ans ; int solve(int x){ if(ans[x]!=-1) return ans[x]; int r=x; vector<int>t; for(int i=1;i<=20;i++){ t.push_back(x%2); x/=2; } int now=0,cnt=0; for(int i=19;i>=0;i--){ if(!a[now].nxt[t[i]%2]) break; now=a[now].nxt[t[i]%2]; if(i&&a[a[now].nxt[t[i-1]%2]].sz==1<<(i-1)){ cnt+=1<<(i-1); t[i-1]^=1; } } return ans[r]=cnt; } int main(){ scanf("%d %d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&k); vis[k]=true; Ist(k); } dfs(-1,0); memset(ans,-1,sizeof ans); for(int i=0;i<N;i++) if(!vis[i]) ans[i]=0; while(m--){ scanf("%d",&k); k^=pre; pre=k; printf("%d\n",solve(k)); } return 0; }
相关文章推荐
- 【Codeforces Round #430 (Div. 2) D】 D. Vitya and Strange Lesson ("带lazy" 的字典树)
- Codeforces Round #430 (Div. 2) - D. Vitya and Strange Lesson (01字典树)
- 【Codeforces Round #430 (Div. 2) D】Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D - Vitya and Strange Lesson(补题,01Trie)
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson(01Trie)
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2):D. Vitya and Strange Lesson(模拟建树)
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2)C. Ilya And The Tree dfs D - Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson [trie]
- Codeforces Round #430 Vitya and Strange Lesson 逆向思维+01trie
- Codeforces Round #430-01字典树&类异或最大值-D. Vitya and Strange Lesson
- Codeforces Round #430 D. Vitya and Strange Lesson
- Codeforces Round #430 (Div. 2) B. Gleb And Pizza
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #430 (Div. 2) 842C Ilya And The Tree(暴力)