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, ..., 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:

  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, ..., ar) 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, ..., 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 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.

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

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

 

题意:求区间内 为偶数的个数的异或和。

那该如何解决呢?我们可以得到的是什么呢?

(1)所有数的异或和

(2)可以通过维护树状数组前缀异或和实现。来得到各个数的异或和

二者的异或和则为我们所求

离线处理:将所有询问按照右端点从小到大排序,左端点无所谓。

记录每个数字最后出现的位置。对于每个询问[l,r],将前面r个数字全部更新到树状数组中。如果该数字未出现过,则直接更新,如果该数字出现过,则将上次出现的数字置为0(异或该数字自己变为0),并更新这次的数字及其位置。

如:区间[2,5]={3,4,5,3} (以下指a数组,非树状数组) 
①直接更新3  –>3 0 0 0  
②直接更新4 –>3 4 0 0 
③直接更新5 –>3 4 5 0 
④3在之前已经出现过,将之前的3置为0 –> 0 4 5 3

因为已经按照右端点从小到大排序,所以如果有用到3,必定是用到最后出现的那个3,左边的那个3被置为0不会对答案有任何影响,并且解决了重复的问题。

参考:https://blog.csdn.net/JiaFenGuNiang/article/details/52218299 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 1000000 + 100;

int n, m, a[maxn], bit[maxn], re[maxn], pre[maxn];
long long ans[maxn];

map<int, int>mp;

struct nod{
    int lef;
    int rig;
    int pos;
} N[maxn];
bool cmp(nod a, nod b)
{

    return a.rig < b.rig;
}
int lowbit(int n)
{
    return n & (-n);
}

int sum(int i)
{
    int s = 0;
    while(i > 0)
    {
        s ^= bit[i];
        i -= lowbit(i);
    }
    return s;
}
void add(int i, int x)
{
    while(i <= n)
    {
        bit[i] ^= x;
        i += lowbit(i);
    }
}
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        mp.clear();
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            re[i] = re[i - 1] ^ a[i];
            pre[i] = mp[a[i]]; //记录a[i]前一次出现的位置
            mp[a[i]] = i; // 更新
        }
        scanf("%d", &m);

        for(int i = 1; i <= m; i++)
        {
//            int a, b;
            scanf("%d%d", &N[i].lef, &N[i].rig);
            N[i].pos = i;
        }

        sort(N + 1, N + m + 1, cmp);

        memset(bit, 0, sizeof(bit));
        for(int i = 1, r = 1; i <= m; i++)
        {
            while(r <= N[i].rig)
            {
                if(pre[r]) // 出现过,删去
                {
                    add(pre[r], a[r]);  // 出现过的情况,把他删掉
                }
                add(r, a[r]); // 添加第r个数
                r++;
            }
           ans[N[i].pos] = re[N[i].rig]^re[N[i].lef - 1]^sum(N[i].rig)^sum(N[i].lef - 1);
            //  得到的两种情况的异或和。
        }

        for(int i = 1; i <= m; i++)
        {
            printf("%lld\n", ans[i]);
        }
    }
    return 0;
}


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值