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

思路:利用离线求每个区间不同数的异或和,再求区间的异或和,区间的异或和相当与区间的奇数个数的异或和;

   利用区间异或和   异或   区间不同数的异或和  == 区间偶数个数的异或和;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 1e-10
const int N=1e6+,M=1e6+,mod=1e9+,inf=1e9+;
struct is
{
int l,r;
int pos;
}a[N];
int b[N];
int ans[N];
map<int,int>last;
int cmp(is x,is y)
{
if(x.r!=y.r)
return x.r<y.r;
return x.l<y.l;
}
int treeunq[N];
int tree[N];
int lowbit(int x)
{
return x&-x;
}
int update(int x,int change,int n,int *tree)
{
while(x<=n)
{
tree[x]^=change;
x+=lowbit(x);
}
}
int query(int x,int *tree)
{ int sum=;
while(x)
{
sum^=tree[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
int x,y,z,i,t;
while(~scanf("%d",&x))
{
memset(tree,,sizeof(tree));
memset(treeunq,,sizeof(treeunq));
for(i=;i<=x;i++)
{
scanf("%d",&b[i]);
tree[i]=b[i];
y=lowbit(i);
for(t=;t<y;t++)
tree[i]^=b[i-y+t];
}
scanf("%d",&y);
for(i=;i<=y;i++)
scanf("%d%d",&a[i].l,&a[i].r),a[i].pos=i;
sort(a+,a+y+,cmp);
int st=;
for(i=;i<=y;i++)
{
while(st<=a[i].r)
{
if(last[b[st]]!=)
update(last[b[st]],b[st],x,treeunq);
last[b[st]]=st;
update(st,b[st],x,treeunq);
st++;
}
ans[a[i].pos]=query(a[i].r,tree)^query(a[i].l-,tree)^query(a[i].r,treeunq)^query(a[i].l-,treeunq);
}
for(i=;i<=y;i++)
printf("%d\n",ans[i]);
}
return ;
}

Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线的更多相关文章

  1. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  2. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  3. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  4. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  5. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

    题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...

  6. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  7. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  8. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  9. Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】

    主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤  ...

随机推荐

  1. 【IDEA】单元测试:项目中引入JUnit测试框架+Mock简单了解

    一.Junit 使用和说明: 参考:单元测试第三弹--使用JUnit进行单元测试-HollisChuang's Blog http://www.hollischuang.com/archives/17 ...

  2. CodeForces 157A Game Outcome

    A. Game Outcome time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. MySQL中的注释(有三种)

    MysQL支持三种注释: .#... (推荐这种,具有通性) ."-- ..." (注意--后面有一个空格) ./*...*/

  4. Centos6.5下Samba服务器的安装和配置

    1.安装samba服务 # yum install samba samba-client samba-swat 2.安装包说明 samba-3.6.23-43.el6_9.x86_64----> ...

  5. 微信支付 超时 mysql.event

    $wtime 使用具体timestamp //rand 防推测 $wev = 'ev_gbuy_create_' . trim($winsert_id) . rand(100, 999); $sql ...

  6. [荐][转]为何应该使用 MacOS X(论GUI环境下开发人员对软件的配置与重用)

    一周前我和 Tinyfool 闲聊苹果操作系统,都认为对于开发人员来说,苹果操作系统(MacOS)是上佳的选择.Tinyfool 笔头很快,当即就写了一篇长文章,我则笔头很慢,今天才全部码好.他的文章 ...

  7. Java基础语法 - 面向对象 - this 关键字

    在Java语言中规定使用this关键字来代表本类对象的引用,this关键字被隐式地用于引用对象的成员变量和方法. this关键字引用的就是本类的一个对象,在局部变量或方法参数覆盖了成员变量时,就要添加 ...

  8. IO 流之字符流的缓冲区

    缓冲区的出现提高了对数据的读写效率 对应类: BufferedWriter BufferedReader 缓冲区需要结合流才可以使用, 对流的功能进行了增强, 即对流的操作起到装饰作用 使用缓冲区实现 ...

  9. DateTimeTypeHandler

    mysql-timestimp-------model-DateTime(jodatime) public class DataTimeTypeHandler extends BaseTypeHand ...

  10. oracle external密码验证

    什么是external密码验证 当OS user 中存在和DB user 同名的用户时   直接使用和DB user 同名的OS user 可以不输入密码直接登录数据库, [oracle@zxrac1 ...