Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线
3.5 seconds
256 megabytes
standard input
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:
- 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.
3
3 7 8
1
1 3
0
7
1 2 1 3 3 2 3
5
4 7
4 5
1 3
1 7
1 5
0
3
1
3
2
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 树状数组+离线的更多相关文章
- 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 ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...
- 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 ...
- 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 ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤ ...
随机推荐
- monggodb 模糊查询
MongoDB的模糊查询其实很简单: 11.LIKE模糊查询userName包含A字母的数据(%A%) SQL:SELECT * FROM UserInfo WHERE user ...
- delphi xe学习随意记录
学习来源(根据他们的资料整理) 论坛:http://www.coder163.com(有视频) 博客:http://del.cnblogs.com/(万一的博客) 1.1.1 命名规范的概述 1 ...
- iOS核心动画详解(一)
前言 这篇文章主要是针对核心动画(Core Animation)的讲解,不涉及UIView的动画.因为内容较多,这篇文章会分为几个章节来进行介绍.本文主要是介绍核心动画的几个类之间的关系和CAAnim ...
- Storm-源码分析-Topology Submit-Executor
在worker中通过executor/mk-executor worker e, 创建每个executor (defn mk-executor [worker executor-id] (let [e ...
- C#中的另类语法
一..net中return的另类写法: 不知道是从3.5还是从4.0开始C#语法中的return有了新的写法示例如下: public string functionDemo() { str ...
- Redis几个认识误区(转)
add by zhj: 文章很老了,2010年的,注意,下面几点是作者认为的误区 原文:http://timyang.net/data/redis-misunderstanding/ 前几天微博发生了 ...
- 服务器(Ubuntu)远程访问ipython notebook(服务器运行ipython notebook 本地浏览器访问)
准备工作 首先要安装 ipython 推荐直接 Anaconda 搞起(装在服务器). Anaconda 帮你集成N多python相关环境(包),省得你再手动咔咔一顿安装 服务器启动ipython n ...
- Linux(1)- 服务器核心知识、Linux入门、VMware与centeos安装、远程连接linux、linux基本命令使用
一.服务器核心知识 1.电脑和电脑的硬件组成 现在的人们几乎无时无刻不在使用着电脑!不管是桌上型电脑(桌机).笔记型电脑(笔电).平板电脑,还是智慧型手机等等,这些东西都算是电脑.虽然接触这么多,但是 ...
- POJ 3613 Cow Relays (floyd + 矩阵高速幂)
题目大意: 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B B[i][j] 就表示 i-j 刚好走过两条路的方法数 那么同理 我们把 ...
- ATA接口寄存器描写叙述
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mao0514/article/details/32135815 ATA接口寄存器描写叙述 .AT ...