Codeforces 842D Vitya and Strange Lesson【逆向思维+字典树查询亦或最小值】
2017-08-30 16:45
260 查看
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 — 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
output
input
output
input
output
题目大意:
给出一个数组,每次操作将整个数组亦或一个数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);
}
}
}
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 — 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
题目大意:
给出一个数组,每次操作将整个数组亦或一个数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 706D Vasiliy's Multiset (字典树查询+贪心)
- CodeForces - 877C Slava and tanks (在这个位置的炸一下,这个位置的坦克就跑到相邻的其中一个位置,一个坦克炸两下才能坏 ,求最小的轰炸数)
- Codeforces 706D Vasiliy's Multiset(可持久化字典树)
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
- Codeforces 741C Sonya and Queries(字典树)
- Codeforces 387E George and Cards 树状数组 + 集合set查询
- codeforces 632f 最小瓶颈树
- codeforces 632F. Magic Matrix (最小生成树)
- Codeforces 706D Vasiliy's Multiset(异或字典树)
- 字典树在车站查询功能中的应用
- 最小字典树输出字符串
- NYOJ 1185 最大最小值 (线段树 & 区间最值查询)
- Codeforces 282E Sausage Maximization(字典树)
- Codeforces 876F High Cry【逆向思维】
- SQL查询某字段最小值
- Codeforces 706 D. Vasiliy's Multiset (字典树贪心)
- Codeforces Round #430 (Div. 2) D.Vitya and Strange Lesson 异或 01字典树补集最小
- Codeforces 125E MST Company (单度限制最小生成树)
- Codeforces 723C暴力(最小值最大)
- CodeForces - 240E Road Repairs(最小树形图+输出路径)