NC 112209 Vitya and Strange Lesson(01字典树)

在这里插入图片描述

题意:给你n个数的序列m个操作,每次操作给你一个数,然后让序列中所有的数异或这个数最后求当前序列的mex(最小未出现的非负值)

思路:异或有结合律那么我们就把后面输入的数异或一下,然在n 个数上的字典树去找没出现的值,我们去记录一下当前树的节点,看他是不是满二叉树,如果是满的那说明在这条路径上没有未出现的数,我们就跳到另一边,然后此时当前位只能为1所以加上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;

}


猜你喜欢

转载自blog.csdn.net/qq_43619680/article/details/109499283
nc
今日推荐