CodeForces - 703D

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.

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.

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

这个题首先想到类似于前缀和的性质
因为自己异或自己等于0
所以用类似于前缀和的方式存异或就可以找到区间内出现次数为奇数的数的异或值
要求偶数只要区间内把出现过的所有不同数的异或与之前的奇数异或就可以得到答案

离线的原因是因为每次求区间在固定右端点的情况下,所得需要维护的值的位置一定是尽可能的最右边,这个最右边被固定的右端点限制住了,最优是最右 但是在先求右后求左的右端点的情况下…
某些数据就会被覆盖,同一个数出现的位置被覆盖到了右边,你会认为他没出现过…所以是错误的
因此需要离线查询

#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdio>
#include<set>
using namespace std;
int tu[1000001];
int qz[1000001],sg[1000001];
map<int, int>mp;
int n;
struct p
{
    int z, y, ml;
    bool operator < (const p&a)const {
        if (y == a.y)return z < a.z;
        return y < a.y;
    }
};
p wen[1000001];
int shu[1000001],jg[1000001];
int lowb(int q) { return q&(-q); }
void ad(int q, int w)
{
    while (q <= n)
    {
        shu[q] ^= w;
        q += lowb(q);
    }
}
int ww(int q)
{
    int fs = 0;
    while (q >= 1)
    {
        fs ^= shu[q];
        q -= lowb(q);
    }
    return fs;
}
int main()
{
    cin >> n;
    for (int a = 1; a <= n; a++)scanf("%d", &tu[a]);
    for (int a = 1; a <= n; a++)
    {
        sg[a] = mp[tu[a]];
        mp[tu[a]] = a;
    }
    qz[1] = tu[1];
    for (int a = 2; a <= n; a++)qz[a] = qz[a - 1] ^ tu[a];
    int m;
    cin >> m;
    for (int a = 1; a <= m; a++)scanf("%d%d", &wen[a].z, &wen[a].y);
    for (int a = 1; a <= m; a++)wen[a].ml = a;
    sort(wen + 1, wen + m + 1);
    for (int a = 1,b=1; a <= m; a++)
    {
        while (b <= wen[a].y)
        {
            if (sg[b])ad(sg[b], tu[b]);
            ad(b, tu[b]);
            b++;
        }
        jg[wen[a].ml] = qz[wen[a].z - 1] ^ qz[wen[a].y] ^ ww(wen[a].z - 1) ^ ww(wen[a].y);
    }
    for (int a = 1; a <= m; a++)printf("%d\n", jg[a]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值