D. Mishka and Interesting sum
time limit per test
3.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
Copy
3 3 7 8 1 1 3
output
Copy
0
input
Copy
7 1 2 1 3 3 2 3 5 4 7 4 5 1 3 1 7 1 5
output
Copy
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 .
一、原题地址
二、大致题意
给出n个数,有q个询问,每次询问查询在[ l , r ]区间内出现次数为偶数次的数,求这些数的异或值。
三、思路
不考虑题意,将一个区间异或起来,得到的是区间内奇数次数的异或值。
如[1、2、3、3、5]区间异或, 等于1^2^3^3^5 等于1^2^5.
求偶数的异或很容易想到先将区间内所有出现过的数提取出来异或一次,然后用得到的异或值再去异或一次区间内奇数次异或的值,这样得到的就是偶数次次数的数的异或值值了。
再如上面的例子,所有数的异或就是1^2^3^5 ,用这个值去异或1^2^5 得到的就是我们需要的偶数次数的数的异或 3。
用一个线段树维护区间内奇数次数的异或值,然后离线操作区间内所拥有的数的异或值,离线时用另外一颗线段树维护区间内数的种类。
四、代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<unordered_set>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long LL;
int n, q;
const int maxn = 1000005 * 4; //线段树范围要开4倍
struct Tree
{
int l, r;
LL sum;
};
Tree node[maxn],nodeall[maxn]; //node[maxn]为线段树处理数组
LL a[maxn],temp[maxn]; //a[maxn]为原数组
bool vis[1000005];
int pre[1000005];
void update(int i)
{
node[i].sum = node[i << 1].sum ^ node[(i << 1) | 1].sum;
}
void build(int i, int l, int r)
{
node[i].l = l; node[i].r = r;
if (l == r)
{
node[i].sum = a[l];
return;
}
int mid = (l + r) / 2;
build(i << 1, l, mid);
build((i << 1) | 1, mid + 1, r);
update(i);
}
LL getsum(int i, int l, int r)
{
if (node[i].l == l&&node[i].r == r)
return node[i].sum;
int mid = (node[i].l + node[i].r) >> 1;
if (r <= mid) return getsum(i << 1, l, r);
else if (l > mid) return getsum((i << 1) | 1, l, r);
else return getsum(i << 1, l, mid) ^ getsum((i << 1) | 1, mid + 1, r);
}
void add(int i, int k, LL v) //当前计算到的点为i,把数列中的第k个元素加v
{
if (node[i].l == k&&node[i].r == k)//因为更改的单点,所以左右端点均和k相等
{
node[i].sum = v;
return;
}
int mid = (node[i].l + node[i].r) / 2;
if (k <= mid) add(i << 1, k, v);
else add((i << 1) | 1, k, v);
update(i);
}
void updateall(int i)
{
nodeall[i].sum = nodeall[i << 1].sum ^ nodeall[(i << 1) | 1].sum;
}
void buildall(int i, int l, int r)
{
nodeall[i].l = l; nodeall[i].r = r;
if (l == r)
{
nodeall[i].sum = 0;
return;
}
int mid = (l + r) / 2;
buildall(i << 1, l, mid);
buildall((i << 1) | 1, mid + 1, r);
updateall(i);
}
LL getsumall(int i, int l, int r)
{
if (nodeall[i].l == l&&nodeall[i].r == r)
return nodeall[i].sum;
int mid = (nodeall[i].l + nodeall[i].r) >> 1;
if (r <= mid) return getsumall(i << 1, l, r);
else if (l > mid) return getsumall((i << 1) | 1, l, r);
else return getsumall(i << 1, l, mid) ^ getsumall((i << 1) | 1, mid + 1, r);
}
void addall(int i, int k, LL v) //当前计算到的点为i,把数列中的第k个元素加v
{
if (nodeall[i].l == k&&nodeall[i].r == k)//因为更改的单点,所以左右端点均和k相等
{
nodeall[i].sum = v;
return;
}
int mid = (nodeall[i].l + nodeall[i].r) / 2;
if (k <= mid) addall(i << 1, k, v);
else addall((i << 1) | 1, k, v);
updateall(i);
}
struct Que
{
int ql, qr, id;
}que[1000005];
LL ans[1000005];
bool cmp(Que qq, Que qqq){return qq.qr < qqq.qr;}
void prework()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)scanf("%lld", &a[i]), temp[i] = a[i];
scanf("%d", &q);
for (int i = 1; i <= q; i++)
{
scanf("%d %d", &que[i].ql, &que[i].qr);
if (que[i].ql > que[i].qr)swap(que[i].ql, que[i].qr);
que[i].id = i;
}
sort(temp + 1, temp + 1 + n);
sort(que + 1, que + 1 + q, cmp);
build(1, 1, n);
buildall(1, 1, n);
}
void Mainwork()
{
memset(vis, false, sizeof(vis));
int ask = 1;
for (int i = 1; i <= n; i++)
{
int pos = lower_bound(temp + 1, temp + 1 + n, a[i]) - temp ;
if (vis[pos])
{
addall(1, pre[pos], 0);
addall(1, i, a[i]);
pre[pos] = i;
}
else
{
addall(1, i, a[i]);
vis[pos] = true;
pre[pos] = i;
}
for (; ask <= q; ask++)
{
if (que[ask].qr != i)break;
else
{
LL t = getsum(1, que[ask].ql, que[ask].qr);
LL allt=getsumall(1, que[ask].ql, que[ask].qr);
ans[que[ask].id] = allt^t;
}
}
}
}
int main()
{
prework();
Mainwork();
for (int i = 1; i <= q; i++)printf("%lld\n", ans[i]);
getchar();
getchar();
}