1. 程式人生 > >codeforces703D、Mishka and Interesting sum(區間偶數異或+離線處理)

codeforces703D、Mishka and Interesting sum(區間偶數異或+離線處理)

                                                                                                      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 a

1, 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 x
    1, 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();
}