NC 112209 Vitya and Strange Lesson (01 dictionary tree)

Insert picture description here

The meaning of the question: give you m operations for a sequence of n numbers, each operation gives you a number, and then let all the numbers in the sequence XOR this number and finally find the mex of the current sequence (the smallest non-negative value that does not appear)

Idea: XOR has associative law, then we XOR the numbers entered later, and then look for values ​​that do not appear in the dictionary tree over n numbers. Let's record the nodes of the current tree to see if it is full of the binary tree. , If it is full, it means that there is no unappeared number on this path, we jump to the other side, and then the current position can only be 1, so add 1<<i

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=1e7+5;

int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
int trie[N][2],root, tot;
int sum[N];
int vis[N];

void Insert(int  x)
{
    
    
    if(vis[x])return ;
    vis[x]=1;
    root = 0;
    for (int i = 30; i >=0; i--)
    {
    
    
        int id =x>>i&1;//转为2进制;
        if (!trie[root][id]){
    
       
            trie[root][id] = ++tot;
        }
        root = trie[root][id];
        sum[root]++;
    }
    //sum[root]=x;
}
int Search(int x)
{
    
    
    root = 0;
    int res=0;
    for (int i = 30; i>=0; i--)
    {
    
    
        int id =x>>i&1;
        if(sum[trie[root][id]]==(1<<i))///如果是满二叉树(全出现过
        {
    
    
            root=trie[root][id^1];
            res|=1<<i;
        }
        else
            root=trie[root][id];
    }
    return res;
}
int main()
{
    
    

  



      int n,m;
      scanf("%d%d",&n,&m);
      for(int i=1;i<=n;i++){
    
    
          int x;
          scanf("%d",&x);
          Insert(x);

      }
      int tm=0;
      while(m--)
      {
    
    
          int x;
          scanf("%d",&x);
          tm^=x;
          printf("%d\n",Search(tm));

      }
     
      
       
      

    return 0;

}


Guess you like

Origin blog.csdn.net/qq_43619680/article/details/109499283