题目链接:

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 题意: 问一个区间里面出现偶数次的数字的异或和是多少; 思路: 一个区间的异或和得到的是出现奇数次的数字的异或和,现在再异或个所有的数字的异或和就是答案了;离线处理,按有区间排序,
结合map存与每个数相同的最近的前边的前驱位置,然后用树状数组每次删去不用的前驱位置; AC代码:
/************************************************
┆ ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓   ┏━┛ ┆
┆  ┃   ┃  ┆      
┆  ┃   ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃    ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+10;
const int maxn=2e3+14;
const double eps=1e-12; int n,a[N],sum[N],ha[N];
int lowbit(int x){return x&(-x);} void update(int x,int temp)
{
while(x<=n)
{
sum[x]^=temp;
x+=lowbit(x);
}
} int query(int x)
{
int s=0;
while(x)
{
s^=sum[x];
x-=lowbit(x);
}
return s;
}
struct node
{
int l,r,id;
}po[N];
int cmp(node a,node b)
{
//if(a.r==b.r)return a.l<b.l;
return a.r<b.r;
}
map<int,int>mp;
int pre[N],ans[N];
int main()
{
read(n);
For(i,1,n)
{
read(a[i]);
ha[i]=ha[i-1]^a[i];
pre[i]=mp[a[i]];
mp[a[i]]=i;
}
int q;
read(q);
For(i,1,q)
{
read(po[i].l);
read(po[i].r);
po[i].id=i;
}
sort(po+1,po+q+1,cmp);//cout<<"##"<<endl;
int p=1;
For(i,1,q)
{
while(p<=po[i].r)
{
if(pre[p])update(pre[p],a[pre[p]]);
update(p,a[p]);
p++;
}
ans[po[i].id]=(ha[po[i].r]^ha[po[i].l-1]^query(po[i].r)^query(po[i].l-1));
//cout<<po[i].id<<" "<<sum[po[i].l-1]<<" "<<sum[3]<<" "<<sum[po[i].r]<<endl;
}
For(i,1,q)printf("%d\n",ans[i]);
return 0;
}

  

codeforces 703D D. Mishka and Interesting sum(树状数组)的更多相关文章

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

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  2. 【29.82%】【codeforces 703D】Mishka and Interesting sum

    [题解] 题意: 给n个数字组成有序数列; 给m个询问. 对于每个询问区间.输出这个区间里面出现次数为偶数次的所有数的异或值; 做法: 我们可以先求出这段区间里面所有(包括重复的数字)数字的异或值p1 ...

  3. [bzoj3155]Preprefix sum(树状数组)

    3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1183  Solved: 546[Submit][Status] ...

  4. CodeForces 602E【概率DP】【树状数组优化】

    题意:有n个人进行m次比赛,每次比赛有一个排名,最后的排名是把所有排名都加起来然后找到比自己的分数绝对小的人数加一就是最终排名. 给了其中一个人的所有比赛的名次.求这个人最终排名的期望. 思路: 渣渣 ...

  5. CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

    题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...

  6. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  7. codeforces 985 E. Pencils and Boxes (dp 树状数组)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. CodeForces 380C Sereja and Brackets(扫描线+树状数组)

    [题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...

  9. CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)

    树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且代码量和常数较小 首先定义一个数组 int c[N]; 并清空 memset(c, 0, sizeof c) ...

随机推荐

  1. win2012R2无法打开匿名级安全令牌

    解决办法:  1.输入“dcomcnfg.exe”,打开组件服务管理. 2.展开组件服务,计算机,右击我的电脑,选择属性. 3.在默认属性选项卡中,      选择:- 勾选“在此计算机中启用分布式C ...

  2. Win8 恢复传统启动菜单 for 多系统

    如果你安装了Win7和Win8,启动的时候得先启动到Win8然后再启动到Win7,很怀念原来的启动选择器,这里给你方法了~ 当前系统是Win8的输入 bcdedit /set {current} bo ...

  3. layui-概念-入门-总结

    layui教程:http://www.dosrun.com/layui/ 获得 Layui你可以在官网首页下载到 Layui 的最新版,也可以通过 GitHub得到Layui的开源包.目前只同步维护这 ...

  4. JavaScript中的ajax(二)

    一.Ajax概念Ajax是(Asynchronous JavaScript And XML)是异步的JavaScript和xml.也就是异步请求更新技术.Ajax是一种对现有技术的一种新的应用,不是一 ...

  5. jquery 获取 outerHtml

    在开发过程中,jQuery.html() 是获取当前节点下的html代码,并不包括当前节点本身的代码,然后我们有时候确须要.找遍jQuery api文档也没有不论什么方法能够拿到. 看到有的人通过pa ...

  6. 1-3:CSS3课程入门之伪类和伪元素

    E:target 表示当前的URL片段的元素类型,这个元素必须是E E:disabled 表示不可点击的表单控件 E:enabled 表示可点击的表单控件 E:checked 表示已选中的checkb ...

  7. adaptive heuristic critic 自适应启发评价 强化学习

    https://www.cs.cmu.edu/afs/cs/project/jair/pub/volume4/kaelbling96a-html/node24.html [旧知-新知   强化学习:对 ...

  8. 【题解】POJ1934 Trip (DP+记录方案)

    [题解]POJ1934 Trip (DP+记录方案) 题意: 传送门 刚开始我是这么设状态的(谁叫我DP没学好) \(dp(i,j)\)表示钦定选择\(i\)和\(j\)的LCS,然而你会发现这样钦定 ...

  9. JavaScript 四种显示数据方式

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 我的Java开发学习之旅------>Java经典排序算法之选择排序

    一.算法原理 对比数组中前一个元素跟后一个元素的大小,如果后面的元素比前面的元素小则用一个变量k来记住他的位置, 接着第二次比较,前面"后一个元素"现变成了"前一个元素& ...