【Codeforces Round 365 (Div 2)D】【离线询问 树状数组 前驱思想】Mishka and Interesting sum 区间内出现次数偶数的数的异或和

来源:互联网 发布:语音转文本软件 编辑:程序博客网 时间:2024/05/17 17:18

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:

  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
33 7 811 3
output
0
input
71 2 1 3 3 2 354 74 51 31 71 5
output
03132
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 .


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e6 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int n, m;int a[N];struct Q{int l, r, o;bool operator < (const Q&b)const{return r < b.r;}}q[N];int ans[N];map<int, int>pre;int r;int v[N];void add(int x, int val){for (; x; x -= x&-x)v[x] ^= val;}int check(int x){int ret = 0;for (; x < r; x += x&-x)ret ^= v[x];return ret;}int main(){while (~scanf("%d", &n)){for (int i = 1; i <= n; ++i)scanf("%d", &a[i]);scanf("%d", &m);for (int i = 1; i <= m; ++i){scanf("%d%d", &q[i].l, &q[i].r);q[i].o = i;}sort(q + 1, q + m + 1);pre.clear();MS(v, 0);r = 0;for (int i = 1; i <= m; ++i){while (r <= q[i].r){add(pre[a[r]], a[r]);pre[a[r]] = r;++r;}ans[q[i].o] = check(q[i].l);}for (int i = 1; i <= m; ++i)printf("%d\n", ans[i]);}return 0;}/*【题意】有一个长度为n(1e6)的数组a[],权值范围为[1,1e9]有m(1e6)个询问,对于每个询问[l,r]我们要使得——在区间[l,r]范围内的,出现次数为偶数次的权值做异或和【类型】离线处理询问 前置作用 树状数组 【分析】这道题如果用莫队,是比较easy的,出现次数超过1次(>=2)之后,把权值做异或处理。但是莫队的复杂度为O(n^1.5),直冲1e9,虽然时间给了3.5s,但是依然会TLE。我们不考虑莫队了,要换其他做法,最多只能为O(nlogn)——其实不管是莫队,还是其他的做法,都可能需要抓住一个性质,就是我们在区间统计的时候,我们只要使得第一次出现某个数的时候,我们不计数即可。或者是说,当我们每次出现一个数的时候,我们在其前一个数的位置上权值增加。具体怎么实现呢?首先,对于这道题,询问的区间是有左右界的。我们如果想作用"使其前一个数的位置上权值增加"的效果。就必须要使得当前这个数的作用,对我们所有的询问,都是生效的。也就是说,我们还是要按照边界排序,进行离线处理。即,我们使得右界单增化。然后,对于每个节点a[i],在其进入到右界范围内时(i<=r),使得其前一个点的权值获得a[i]的权值。(每个权值的初始前置位置都为0)那对于一个询问,我们只要做区间权值异或和即可。即我们整体上需要树状数组嵌套实现,详见代码。【时间复杂度&&优化】O(nlogn)*/


0 0
原创粉丝点击