D. Mishka and Interesting sum
D. Mishka and Interesting sum
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., *a**n* 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
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, ..., *a**r*) 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 x
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 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, ..., *a**n* (1 ≤ *a**i* ≤ 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.
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
0
3
1
3
2
題意是求一個區間內出現次數為偶數的數的異或和.
首先想到由字首異或和求區間的異或和.
然後相同的數取異或等於0
ans=區間異或和與區間所有不同的數的異或和
問題關鍵在於如何求區間所有不同的數的異或和
離線查詢,然後記錄每個數的上一個相同的數的位置.遍歷過程中每次就加上這個數的影響並去掉上一個相同的數的影響.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+5;
int pre_sum[maxn],unq_sum[maxn],n,q;
inline int lowbit(int x){
return x&(-x);
}
ll getsum(int pos){
ll res=0;
for(ll i=pos;i>0;i-=lowbit(i)){
res^=unq_sum[i];
}
return res;
}
void add(int pos,int val){
for(int i=pos;i<=n;i+=lowbit(i)){
unq_sum[i]^=val;
}
}
struct node{
int l,r,id;
bool operator<(const node &c)const{
return r<c.r;
}
}s[maxn];
struct find_last{
int val,id;
bool operator<(const find_last &c)const{
if(val==c.val){
return id<c.id;
}
else return val<c.val;
}
}copy_num[maxn];
int num[maxn];
int last[maxn];
int ans[maxn];
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i){
scanf("%d",&num[i]);
copy_num[i].val=num[i];
copy_num[i].id=i;
pre_sum[i]=pre_sum[i-1]^num[i];
}
for(int i=1;i<=q;++i){
scanf("%d%d",&s[i].l,&s[i].r);
s[i].id=i;
}
sort(s+1,s+1+q);int pos=1;
sort(copy_num+1,copy_num+n+1);
for(int i=2;i<=n;++i){
if(copy_num[i].val==copy_num[i-1].val){
last[copy_num[i].id]=copy_num[i-1].id;
}
}
for(int z=1;z<=q;++z){
while(pos<=s[z].r){
if(last[pos]) add(last[pos],num[pos]);
add(pos,num[pos]);
pos++;
}
ans[s[z].id]=getsum(s[z].r)^getsum(s[z].l-1)^pre_sum[s[z].r]^pre_sum[s[z].l-1];
}
for(int i=1;i<=q;++i){
printf("%d\n",ans[i]);
}
return 0;
}