Codeforces 842D Vitya and Strange Lesson【逆向思维+字典树查询亦或最小值】

本篇介绍了一个关于数组操作的问题,通过一系列查询来找出数组中未出现过的最小非负整数。利用位运算与数据结构技巧实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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

题目大意:


给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数。


思路:

①首先我们知道,亦或x之后得到的数组将其再亦或y值得的数组,其实就是原数组亦或(x^y)的结果。所以这里我们没有必要将原数组进行变化。


②我们还知道,如果有:z^x=y,其中若x,y已知,那么z是固定的。那么我们可以根据这个特性,我们将问题翻转一下。

我们将原数组中不存在的数入树。那么对于每一个查询temp,其实就相当于我们在树上找一个值,使得这个值亦或temp最小。

引入了这个逆向思维之后,我们就变成了贪心find。


③过程维护一下即可。注意入树的数值范围。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define ll long long int
#define maxn 2
typedef struct tree
{
    tree *nex[maxn];
    ll v;
    ll val;
} tree;
tree root;
void init()
{
    for(ll i=0; i<maxn; i++)
    {
        root.nex[i]=NULL;
        root.v=0;
        root.val=0;
    }
}
void creat(char *str,int va)
{
    int len=strlen(str);
    tree *p=&root,*q;
    for(int i=0; i<len; i++)
    {
        int id=str[i]-'0';
        if(p->nex[id]==NULL)
        {
            q=(tree *)malloc(sizeof(root));
            q->v=1;
            for(int j=0; j<2; j++)
            {
                q->nex[j]=NULL;
            }
            p->nex[id]=q;
        }
        else
        {
            p->nex[id]->v++;
        }
        p=p->nex[id];
        if(i==len-1)
        {
            p->val=va;
        }
    }
}
void find(char *str,ll query)
{
    ll len=strlen(str);
    tree *p=&root;
    for(ll i=0; i<len; i++)
    {
        ll id=str[i]-'0';
        if(p->nex[id]!=NULL)
        {
            p=p->nex[id];
        }
        else p=p->nex[1-id];
        if(p==NULL)return ;
    }
    printf("%lld\n",p->val^query);
}
int main()
{
    ll n,q;
    while(~scanf("%lld%lld",&n,&q))
    {
        init();
        map<ll,ll>s;
        for(ll i=1; i<=n; i++)
        {
            ll x;
            scanf("%lld",&x);
            if(s[x]==0)
            {
                s[x]=1;
            }
        }
        for(ll i=0; i<=600000; i++)
        {
            if(s[i]==0)
            {
                char ss[25];
                ll x=i; 
                ss[21]='\0';
                for(int j=20; j>=0; j--)
                {
                    if(x)
                    {
                        ss[j]=x%2+'0';
                        x/=2;
                    }
                    else
                    {
                        ss[j]='0';
                    }
                }
                creat(ss,i);
            }
        }
        ll temp=0;
        while(q--)
        {
            ll x;
            scanf("%lld",&x);
            temp^=x;
            x=temp;
            char ss[25];
            ss[21]='\0';
            for(int j=20; j>=0; j--)
            {
                if(x)
                {
                    ss[j]=x%2+'0';
                    x/=2;
                }
                else
                {
                    ss[j]='0';
                }
            }
            find(ss,temp);
        }
    }
}










### 解题思路 #### 问题描述 Codeforces 1678C - Tokitsukaze and Strange Inequality 是一道关于排列组合与前缀和的应用问题。给定一个长度为 \( n \) 的排列数组 \( p \),需要统计满足条件 \( a < b < c < d \) 并且 \( p_a < p_c \) 同时 \( p_b > p_d \) 的四元组数量。 --- #### 核心思想 由于数据规模较小 (\( n \leq 5000 \)),可以直接通过枚举的方式解决问题。为了降低时间复杂度,引入 **前缀和** 技术来加速计算过程[^3]。 具体来说: - 枚举变量 \( a \) 和 \( c \),固定它们之后,目标是快速找到符合条件的 \( b \) 和 \( d \)。 - 使用预处理好的前缀和数组 `num` 来高效查询某个范围内满足特定关系的数量。 - 定义辅助数组 `sum` 表示对于固定的区间范围内的某些约束条件下的累积计数结果。 --- #### 实现细节 ##### 步骤一:构建前缀和数组 `num` 定义二维数组 `num[i][j]`,其中 `num[i][j]` 表示在序列的前 \( i \) 项中,有多少个元素大于 \( j \)。 该数组可以通过如下方式初始化: ```python n = len(p) max_val = max(p) # 初始化 num 数组 num = [[0] * (max_val + 2) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(max_val + 1, -1, -1): # 反向遍历以保持正确性 if p[i - 1] > j: num[i][j] = num[i - 1][j] + 1 else: num[i][j] = num[i - 1][j] ``` 上述代码的时间复杂度为 \( O(n \cdot m) \),其中 \( m \) 是数组中的最大值。 --- ##### 步骤二:定义并填充辅助数组 `sum` 定义另一个二维数组 `sum[i][j]`,它表示当 \( a=i \), \( c=j \) 时,在区间 \([a+1, c-1]\) 中满足 \( p[b] > p[d] \) 的总贡献次数。 利用动态规划的思想逐步更新此数组: ```python sum_ = [[0] * (n + 1) for _ in range(n + 1)] bucket = [0] * (max_val + 1) for l in range(n - 1, 0, -1): bucket[p[l]] += 1 for r in range(l + 2, n + 1): sum_[l][r] = sum_[l][r - 1] + (num[r - 1][p[r - 1]] - num[l][p[r - 1]]) ``` 这里的关键在于如何有效累加当前区间的合法贡献,并借助之前已经计算的结果减少重复运算。 --- ##### 步骤三:枚举所有可能的 \( a \) 和 \( c \) 最后一步是对所有的 \( a \) 和 \( c \) 进行双重循环,并将对应位置上的 `sum[a][c]` 加入最终答案中: ```python result = 0 for a in range(1, n - 2): for c in range(a + 2, n): result += sum_[a][c] print(result) ``` 整个算法的核心部分即完成以上三个阶段的操作即可实现高效的解决方案。 --- ### 总结 本题主要考察的是对多重嵌套结构的有效简化以及合理运用前缀和技巧的能力。通过巧妙设计的数据结构能够显著提升程序运行效率至可接受水平。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值