Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, 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 process m queries.
Each query is processed in the following way:
- Two integers l and r (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 are
x1, 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.
The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array.
The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements.
The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries.
Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
Print m non-negative integers — the answers for the queries in the order they appear in the input.
3
3 7 8
1
1 3
0
7
1 2 1 3 3 2 3
5
4 7
4 5
1 3
1 7
1 5
0
3
1
3
2
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 is 0.
In the second query there is only integer 3 is presented even number of times — the answer is 3.
In the third query only integer 1 is written down — the answer is 1.
In the fourth query all array elements are considered. Only
1 and 2 are presented there even number of times. The answer is
.
In the fifth query 1 and
3 are written down. The answer is .
题意:给你一个长度为n的区间,M次提问,问[Li,Ri]内出现次数为偶次的数的异或和。
题解:这道题和HDU3333那道题解法非常类似,思路想法也相同,建议大家先去看看那道题(我blog有),回来再看这个,
首先分析次数为偶次的数的异或和等于什么,它等价于次数为奇次数的异或和 异或 区间不同数的异或和(区间不同数,不是奇次就是偶次的)。区间次数为奇次数的异或和等价于区间所有数的异或和(区间内的数,如果是偶次的异或后为0,所以只剩奇次的了)
所以这道题转化成求区间不同数的异或和(HDU那道题求的是区间不同数的和,很类似吧)。至于区间所有数的异或和(我们用一个前缀和维护就好了)。剩下操作和那道题类似。注意。把树状数组两个基本操作需要变一下。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define N 1000010
#define ll long long
using namespace std;
map<int,int> mp;
int t,n,m;
ll rre[N];
ll f[N];
int a[N];
int x[N];
struct node
{
int l,r,id;
bool operator<(const node &n)const{
return r<n.r||(r==n.r&&l<n.l);
}
}qu[N];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y)
{
while(x<=n)
{
f[x]^=y;//记得改这个操作,不是加了,是异或。
x+=lowbit(x);
}
}
ll sum(int x)
{
ll ans=0;
while(x>0)
{
ans^=f[x];//异或和。
x-=lowbit(x);
}
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
x[i]=x[i-1]^a[i];//所有数的异或前缀和。
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
qu[i]=(node){x,y,i};
}
sort(qu+1,qu+m+1);
int cur=1;
for(int i=1;i<=n;i++)
{
if(mp.find(a[i])==mp.end())
{
add(i,a[i]);
mp.insert(make_pair(a[i],i));
}
else
{
add(i,a[i]);
add(mp[a[i]],a[i]);
mp[a[i]]=i;
}
while(qu[cur].r==i)
{
rre[qu[cur].id]=x[qu[cur].l-1]^x[qu[cur].r]^sum(qu[cur].r)^sum(qu[cur].l-1);
cur++;
}
}
for(int i=1;i<=m;i++)
printf("%I64d\n",rre[i]);
return 0;
}