您的位置:首页 > 其它

CodeForces 703D Mishka and Interesting sum

2016-08-08 10:46 555 查看
[align=center]D. Mishka and Interesting sum[/align]
[align=center]time limit per test[/align]
[align=center]3.5 seconds[/align]
[align=center]memory limit per test[/align]
[align=center]256 megabytes[/align]
[align=center]input[/align]
[align=center]standard input[/align]
[align=center]output[/align]
[align=center]standard output[/align]
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integersa1, a2, ..., an
of n elements!

Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to processm
queries.

Each query is processed in the following way:

Two integers l andr
(1 ≤ l ≤ r ≤ n)
are specified — bounds of query segment.
Integers, presented in array segment [l,  r]
(in sequence of integers al, al + 1, ..., ar)even
number of times, are written down.
XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 arex1, x2, ..., xk,
then Mishka wants to know the value

, where

 —
operator of exclusive bitwise OR.
Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.

Input
The first line of the input contains single integern (1 ≤ n ≤ 1 000 000) —
the number of elements in the array.

The second line of the input contains n integersa1, a2, ..., an
(1 ≤ ai ≤ 109) —
array elements.

The third line of the input contains single integerm (1 ≤ m ≤ 1 000 000) —
the number of queries.

Each of the next m lines describes corresponding query by a pair of integersl
andr (1 ≤ l ≤ r ≤ n) —
the bounds of query segment.

Output
Print m non-negative integers — the answers for the queries in the order they appear in the input.

Examples

Input
3
3 7 8
1
1 3


Output
0


Input
7
1 2 1 3 3 2 3
5
4 7
4 5
1 3
1 7
1 5


Output
03
1
3
2


Note
In the second sample:

There is no integers in the segment of the first query, presented even number of times in the segment — the answer is0.

In the second query there is only integer
3 is presented even number of times — the answer is3.

In the third query only integer 1 is written down — the answer is1.

In the fourth query all array elements are considered. Only1 and2
are presented there even number of times. The answer is

.

In the fifth query 1 and3
are written down. The answer is

.

题意:给你n个数,然后询问q次,每次询问查询区间[l,r]里的出现过偶数次的那些数的亦或值。

分析:假如我们不管区间内每个数出现了多少次,全部亦或起来,然后再亦或上区间内不同数的亦或值,那就是答案了,因为一个数出现奇数次,亦或后的值是本身,然后再亦或一次变为0,不影响答案;而出现偶数次的亦或后值为0,然后再亦或一次变为这个值本身,就成了答案所需。我们离线把每个区间按照右端点从小到大排序,再用树状数组挨着维护,因为查询区间右端点确定,对于几个相同的数,我们只需要最靠后的那个数,前面重复的消掉就行。具体看代码实现,还是挺好懂的~

#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
#define LL __int64
using namespace std;
int n,q;
int a[1000010];
int t[1000010];
int num[1000010];
int C[1000010];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int val)
{
while(x<=n)
{
C[x]^=val;
x+=lowbit(x);
}
}
int sum(int x)
{
int res=0;
while(x>0)
{
res^=C[x];
x-=lowbit(x);
}
return res;
}
struct node
{
int l,r,id;
node() {}
node(int l,int r,int id): l(l),r(r),id(id){}
inline bool operator < (const node &x) const
{
return r<x.r;
}
}E[1000010];
int ans[1000010];
int p[1000010];
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
t[i]=a[i];
}
num[0]=0;
for(int i=1;i<=n;i++) num[i]=num[i-1]^a[i];
sort(t+1,t+n+1);
int m=unique(t+1,t+n+1)-t-1;
for(int i=1;i<=n;i++)
{
int pos=lower_bound(t+1,t+m+1,a[i])-t;
a[i]=pos;
}
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
int l,r;
scanf("%d%d",&l,&r);
E[i]=node(l,r,i);
}
sort(E+1,E+q+1);
for(int i=0;i<=n;i++) C[i]=0,p[i]=0;
int pos=0;
for(int i=1;i<=q;i++)
{
while(pos<E[i].r)
{
pos++;
if(p[a[pos]]) add(p[a[pos]],t[a[pos]]);
add(pos,t[a[pos]]);
p[a[pos]]=pos;
}
ans[E[i].id]=num[E[i].r]^num[E[i].l-1]^sum(E[i].r)^sum(E[i].l-1);
}
for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: