Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
来源:互联网 发布:什么软件可以套花呗 编辑:程序博客网 时间:2024/06/10 08:53
传送门
题意:求区间没有的最小的那个数,可以异或x。
思路:首先明确对[1,2^n]区间内的所有数亦或x,仍然是[1,2^n]的所有数。
设给定集合为A,全集C,A的补集为B。易知每次亦或A无需真的改动A数组,A^x1^x2 = A^(x1^x2)。
每次query对A中每个数亦或x, 然后查询min(C-A^x)。
又A^x+B^x = A + B = C,得所求即为min(B^x)。用字典树来求解亦或最值问题。
//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define fOR(n,x,i) for(int i=n;i>=x;i--)#define fOr(n,x,i) for(int i=n;i>x;i--)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long longtypedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=3e5+10;const int maxx=1e6+100;const double EPS=1e-8;const double eps=1e-8;const int mod=1e9+7;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template <class T>inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;int next[maxn*20][2];int val[maxn*20];int st;void init(){ me(next[0],0); me(val,0); st=1;}void insert(LL x){ int u=0; for(int i=20;i>=0;i--) { int c=((x>>i)&1); if(!next[u][c]) { me(next[st],0); next[u][c]=st++; } u=next[u][c]; ++val[u]; }}LL query(LL x){ int t=0; LL ans=0; for(int i=20;i>=0;i--) { int k=((x>>i)&1); if(!next[t][k]) return ans; if(val[next[t][k]]==(1<<i)) { ans|=(1<<i); t=next[t][1-k]; } else t=next[t][k]; } return ans;}int n,m,vis[maxn];int main(){ scan_d(n); scan_d(m); init(); int x; FOR(1,n,i) { scan_d(x); if(vis[x]) continue; else { vis[x]=1; insert(x); } } int y=0; FOR(1,m,i) { scan_d(x); y^=x; print(query(y)); }}
#include <iostream> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <algorithm> #include <functional> #include <utility> #include <cstring> #include <cstdio> #include <cstdlib> #include <ctime> #include <cmath> #include <cctype> #define CLEAR(a, b) memset(a, b, sizeof(a)) #define IN() freopen("in.txt", "r", stdin) #define OUT() freopen("out.txt", "w", stdout) #define LL long long #define maxn 524288 #define maxm 6000005 #define mod 10007 #define INF 1000000007 #define EPS 1e-7 #define PI 3.1415926535898 #define N 4294967296 using namespace std; //-------------------------CHC------------------------------// struct Node { Node *next[2]; Node() { next[0] = next[1] = NULL; } }node[maxm]; int total; void insert(int num, Node *root) { Node *cur = root; for (int i = 19; i >= 0; --i) { int id = num >> i & 1; if (!cur->next[id]) cur->next[id] = &node[total++]; cur = cur->next[id]; } } int query(int num, Node *root) { int ret = 0; Node *cur = root; for (int i = 19; i >= 0; --i) { int id = num >> i & 1; if (!cur->next[id]) id = !id, ret |= (1 << i); cur = cur->next[id]; } return ret; } bool vis[maxn+10]; int main() { int n, q; scanf("%d%d", &n, &q); int x; for (int i = 0; i < n; ++i) scanf("%d", &x), vis[x] = 1; Node *root = &node[total++]; for (int i = 0; i <= maxn; ++i) if (!vis[i]) insert(i, root); int y = 0; while (q--) { scanf("%d", &x); y ^= x; printf("%d\n", query(y, root)); } return 0; }
阅读全文
0 0
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- Codeforces Round #430-01字典树&类异或最大值-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) D. Vitya and Strange Lesson
- 【Codeforces Round #430 (Div. 2) D】 D. Vitya and Strange Lesson ("带lazy" 的字典树)
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson(01Trie)
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson [trie]
- Codeforces Round #430 (Div. 2):D. Vitya and Strange Lesson(模拟建树)
- [Codeforces 842D Vitya and Strange Lesson]异或字典树
- Code Forces 824D Round#430 Div2D:Vitya and Strange Lesson :01字典树
- Codeforces 842D Vitya and Strange Lesson【逆向思维+字典树查询亦或最小值】
- codeforces 842D. Vitya and Strange Lesson
- Codeforces 842D Vitya and Strange Lesson
- Codeforces 842 D Vitya and Strange Lesson
- CodeForces 842D Vitya and Strange Lesson
- Codeforces Round #430 Vitya and Strange Lesson 逆向思维+01trie
- 【codevs 2833】奇怪的梦境
- IDEA 运行maven命令时报错: -Dmaven.multiModuleProjectDirectory system propery is not set
- 读入输出优化
- JVM垃圾收集(GC)
- PRC 和ROC
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- [C++] boost::function boost::bind 与 回调
- MyISAM和InnoDB的区别
- [C++] boost::less_than_comparable
- 当面试官问我们vector扩容机制时,他想问什么?
- MyEclipse如何编写JUnit测试类
- 整除的尾数
- 数位DP入门 + 待补上最后一题
- [C++] ACE书籍推荐