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. java如何计算两个经纬度之间的距离?

    /*计算两个经纬度之间的距离 结果单位:米 */public static double getDistance(String lat1Str, String lng1Str, String lat2 ...

  2. 160704、commons-beanutils.jar常用方法

    package com.test.beanutils; import java.lang.reflect.InvocationTargetException;import java.text.Pars ...

  3. mysql_系统数据库认识

    show databases:查看mysql自带数据库有information_schema,mysql, performance_schema, test information_schema数据库 ...

  4. 码云平台, 生成并部署SSH key

    参考链接: http://git.mydoc.io/?t=154712 步骤如下: 1. 生成 sshkey: ssh-keygen -t rsa -C "xxxxx@xxxxx.com&q ...

  5. PHP的语言结构和函数的区别

    相信大家经常看到对比一些PHP应用中,说用isset() 替换 strlen(),isset比strlen执行速度快等. 例子: if ( isset($user) ) { //do some thi ...

  6. DKLang Translation Editor

    https://yktoo.com/en/software/dklang-traned Features Translation using a dictionary (so-called Trans ...

  7. 0501-Hystrix保护应用-超时机制、断路器模式简介

    一.概述 hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystri ...

  8. Deeplearning——动态图 vs. 静态图

    动态图 vs. 静态图 在 fast.ai,我们在选择框架时优先考虑程序员编程的便捷性(能更方便地进行调试和更直观地设计),而不是框架所能带来的模型加速能力.这也正是我们选择 PyTorch 的理由, ...

  9. Tomcat 安装、配置与部署

    Tomcat的官方网站:http://tomcat.apache.org/,目前最新版本为7.0. Tomcat下载地址: 1.32位:http://mirrors.tuna.tsinghua.edu ...

  10. 前端基础之JavaScript(Day53)

    阅读目录 一.JavaScript基础 二.JavaScript对象 三.BOM对象 一.JavaScript基础 http://www.cnblogs.com/yuanchenqi/articles ...