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

Each query is processed in the following way:

  1. Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment.
  2. Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., *a**r*) even number of times, are written down.
  3. 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, ..., *x**k*, then Mishka wants to know the value img, where img — 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 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;
}

猜你喜欢

转载自www.cnblogs.com/smallocean/p/10262622.html