Codeforces 706D Trie树/multiset
2016-09-01 09:20
363 查看
题目:http://codeforces.com/problemset/problem/706/D
题意:
+表示吧这个数加到集合中,-表示把这个数从集合中减去一次,?表示集合里面的一个y使的x^y最大
分析:
要使异或最大,那么尽量从高位使两个数不同即可。
Trie树。
或者可以直接用multiset模拟:
题意:
+表示吧这个数加到集合中,-表示把这个数从集合中减去一次,?表示集合里面的一个y使的x^y最大
分析:
要使异或最大,那么尽量从高位使两个数不同即可。
Trie树。
#include<bits/stdc++.h> using namespace std; const int N=1e7+9; int a [2],cal ; int tot; void add(int x,int y) //插入或删除一个元素 { int cur=0; for(int i=31;i>=0;i--){ int t=(x>>i)&1; if(!a[cur][t])a[cur][t]=tot++; cur=a[cur][t]; cal[cur]+=y; } } int query(int x) { int cur=0,ans=0; for(int i=31;i>=0;i--){ int t=(x>>i)&1; if(cal[a[cur][t^1]]){ ans+=(1<<i); cur=a[cur][t^1]; } else cur=a[cur][t]; } return ans; } int main() { int n,x; char op; tot=1; add(0,1); scanf("%d",&n); for(int i=0;i<n;i++){ cin>>op>>x; if(op=='+'){ add(x,1); } else if(op=='-'){ add(x,-1); } else{ printf("%d\n",query(x)); } } return 0; } /* Input 10 + 8 + 9 + 11 + 6 + 1 ? 3 - 8 ? 3 ? 8 ? 11 Output 11 10 14 13 */
或者可以直接用multiset模拟:
#include<bits/stdc++.h> using namespace std; const int N=1e7+9; int main() { int n,x; char op; multiset<int>s; s.insert(0); scanf("%d",&n); for(int i=0;i<n;i++){ cin>>op>>x; if(op=='+'){ s.insert(x); } else if(op=='-'){ s.erase(s.find(x)); } else{ int ans=0; for(int i=31;i>=0;i--){ ans|=(~x&(1<<i)); multiset<int>::iterator it=s.lower_bound(ans); if(it==s.end()||*it>=ans+(1<<i)){ // cout<<ans<<endl; ans^=1<<i; } } printf("%d\n",x^ans); } } return 0; }
相关文章推荐
- Codeforces 706D-Vasiliy's Multiset(Trie树)
- codeforces 706D. Vasiliy's Multiset 带删除操作的字典树(真模版)
- codeforces 706D trie树 或 muliset
- 【CodeForces - 706D 】Vasiliy's Multiset 【01 Trie 插入删除查询操作】
- CodeForces 674 D.Bearish Fanpages(set+multiset)
- CodeForces 527C - Glass Carving set multiset 二分
- Codeforces 633C Spy Syndrome 2(DP + Trie树)
- [Codeforces 557E] Ann and Half-Palindrome (Trie树+子串排序)
- Vasiliy's Multiset CodeForces - 706D [字典树] 类似套路~
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
- Codeforces 514C Watto and Mechanism Trie树 + dfs
- codeforces 710F Trie树+kmp Trie树板子和kmp板子
- codeforces 665E E. Beautiful Subarrays(trie树)
- Codeforces 655E Beautiful Subarrays【01trie树】
- Codeforces 665E Beautiful Subarrays【01trie树】
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树
- Codeforces 706d 字典树+贪心
- Codeforces 282E. Sausage Maximization【trie树(非指针版)】
- Codeforces 665E. Beautiful Subarrays (trie树)
- 字符串哈希 Trie树 Watto and Mechanism : CodeForces - 514C