题目链接:
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:
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.
输入
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.
输出
Print m non-negative integers — the answers for the queries in the order they appear in the input.
样例
sample input
7 1 2 1 3 3 2 3 5 4 7 4 5 1 3 1 7 1 5sample output
0 3 1 3 2
题意
求一个区间内所有出现次数为偶数次的数的异或和。
题解
如果题目叫我们求区间内所有出现次数为奇数次的数的异或和,那就好办了,直接把区间所有的数都异或起来就可以了。
现在我们把问题转换一下:我们先求出所有的数的异或和sum1,然后再求出区间内所有不同的数的异或和sum2,那么ans=sum1^sum2. 对于sum1可以O(n)跑前缀异或和,也可以跑线段树。而对于sum2我们可以用离线的线段树来处理()。
代码
#include#include #include