codeforces 842D Trie 树异或不存在的最小值

D. Vitya and Strange Lesson
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Today at the lesson Vitya learned a very interesting function — mex. Mex 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 2
1 3
1
3
output
1
0
input
4 3
0 1 5 6
1
2
4
output
2
0
0
input
5 4
0 1 5 6 7
1
1
4
5
output
2
2
0
2

题意:n个数,m个询问,问异或后不存在的最小的数

逆向思维
每一个数异异或一个数的值都是不一样的,第一种想法是把不存在的数加到字典树上,然后找异或到的最小值,得开 21*(1<<20)的内存有点多了。。

#include <bits/stdc++.h>

using namespace std;
const int N = (1<<20);
const int BB=22;
struct node
{
  int nxt[2];
}T[N*BB+1];

int mp[(1<<20)];

typedef long long ll;
int sz;
void insert(int d)
{
    int p=0;
    for(int i=21;i>=0;i--)
    {
       int dd=(d>>i)&1;
       if(!T[p].nxt[dd]) T[p].nxt[dd]=++sz;
       p=T[p].nxt[dd];
    }
}

int go[32];
int ans;
void ask()
{
   int p=0;
   for(int i=21;i>=0;i--)
   {
      int dd=go[i];
      if(T[p].nxt[dd]) p=T[p].nxt[dd];
      else
      {
           p=T[p].nxt[1-dd],ans|=(1<<i);
      }
   }

}


int main(){

      memset(T,0,sizeof(T));
      int n,m;
      scanf("%d%d",&n,&m);
      sz=0;
      for(int i=1;i<=n;i++)
      {
            int d;
            scanf("%d",&d);
            mp[d]=1;
      } 
      for(int i=0;i<=(1<<20);i++)
      {
          if(!mp[i])
          insert(i);
      }
      int t=0;
      while(m--){
         int x;
         scanf("%d",&x);
         t^=x;
         ans=0;
         for(int j=21;j>=0;j--)
         {
            int dd=(t>>j)&1;
            go[j]=dd;
         }
         ask();
         printf("%d\n",ans);
      }
}

第二种方法是判断子树是不是满的
往不满的子树走,异或同方向的优先
重复的点要删除

#include <bits/stdc++.h>

using namespace std;
const int N = 300010;
const int BB=22;
struct node
{
  int nxt[2];
}T[N*BB+1];
int size[N*BB+1];

typedef long long ll;
int sz;

int go[32];
int ask()
{
   int p=0,ans=0;
   for(int i=21;i>=0;i--)
   {
      int dd=go[i];
      if(!T[p].nxt[dd]) return ans;
      if(size[T[p].nxt[dd]]<(1<<i)) p=T[p].nxt[dd];
      else 
      {
          ans|=(1<<i);
          p=T[p].nxt[1-dd];
          if(!p) return ans;
      }
   }
   return ans;
}

void insert(int d)
{
    int p=0;
    for(int i=21;i>=0;i--)
    {
       int dd=(d>>i)&1;
       if(!T[p].nxt[dd]) T[p].nxt[dd]=++sz;
       p=T[p].nxt[dd];
       size[p]++;
    }
    if(size[p]>1)
    {
        p=0;
        for(int i=21;i>=0;i--)
        {
            int dd=(d>>i)&1;
            p=T[p].nxt[dd];
            size[p]--;
        }
    }
}

int main(){

      memset(T,0,sizeof(T));
      memset(size,0,sizeof(size));
      int n,m;
      scanf("%d%d",&n,&m);
      sz=0;
      for(int i=1;i<=n;i++)
      {
            int d;
            scanf("%d",&d);
            insert(d);
      } 
      int tt=0;
      while(m--){
         int x;
         scanf("%d",&x);
         tt^=x;
         for(int j=21;j>=0;j--)
         {
            int dd=(tt>>j)&1;
            go[j]=dd;
         }
         printf("%d\n",ask());
      }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值