Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小

来源:互联网 发布:什么软件可以套花呗 编辑:程序博客网 时间:2024/06/10 08:53

传送门

D. Vitya and Strange Lesson
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today at the lesson Vitya learned a very interesting function — mexMex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, mex([4, 33, 0, 1, 1, 5]) = 2 and mex([1, 2, 3]) = 0.

Vitya quickly understood all tasks of the teacher, but can you do the same?

You are given an array consisting of n non-negative integers, and m queries. Each query is characterized by one number x and consists of the following consecutive steps:

  • Perform the bitwise addition operation modulo 2 (xor) of each array element with the number x.
  • Find mex of the resulting array.

Note that after each query the array changes.

Input

First line contains two integer numbers n and m (1 ≤ n, m ≤ 3·105) — number of elements in array and number of queries.

Next line contains n integer numbers ai (0 ≤ ai ≤ 3·105) — elements of then array.

Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·105).

Output

For each query print the answer on a separate line.

Examples
input
2 21 313
output
10
input
4 30 1 5 6124
output
200
input
5 40 1 5 6 71145
output
2202

题意:求区间没有的最小的那个数,可以异或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
原创粉丝点击