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. cpp中文乱码

    中文乱码 [root@test mediaStudio]# g++ testCgi.cpp [root@test mediaStudio]# ./a.out Content-type:text/htm ...

  2. python基础之类的特性(property)

    一 什么是特性propertyproperty是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值. import math class Circle: def __init__(self,ra ...

  3. paper reading:gaze tracking

    https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Krafka_Eye_Tracking_for_CVPR_2016_ ...

  4. 序列化组件之生成hypermedialink

    一  生成hypermedialink(极少数) 组件 class BooksSerializer(serializers.ModelSerializer): name = serializers.C ...

  5. 【我的Android进阶之旅】 Google Developers中国网站发布啦!

    今天,Google Developers 中国网站 (https://developers.google.cn) 正式发布! Google Developers 中国网站是特别为中国开发者而建立的,它 ...

  6. Selenium 安装与卸载

    安装: 在cmd中键入pip install selenium==3.6.0(等号后面的为版本号),并点击回车,当出现Successfully installed selenium-3.6.0即表示已 ...

  7. Linux学习笔记(8)文件搜索与帮助

    帮助: (1) man ls (2) info  ls  (3) whatis ls  (4) help 搜索: (1) which  ls :查看ls命令所在绝对路径 (2) locate user ...

  8. Android Studio设置行宽、格式化断行

    设置基于Android studio 1.2,其它版本可能位置不大一样,可以直接搜索 1.设置行宽 就是那条右标准线的位置:Setting-->Editor-->Code Style,右侧 ...

  9. 关于 tf.nn.softmax_cross_entropy_with_logits 及 tf.clip_by_value

    In order to train our model, we need to define what it means for the model to be good. Well, actuall ...

  10. C#__ 模拟鼠标单击事件

    首先要用到的引用有 [DllImport("User32")] public extern static void mouse_event(int dwFlags, int dx, ...