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

时间:2022-08-27 23:52:19
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 Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum  树状数组+离线, where Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum  树状数组+离线 — 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 Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum  树状数组+离线.

In the fifth query 1 and 3 are written down. The answer is Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum  树状数组+离线.

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

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

#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 &num;365 &lpar;Div&period; 2&rpar; D&period; Mishka and Interesting sum 离线&plus;线段树

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

  2. Codeforces Round &num;365 &lpar;Div&period; 2&rpar; D&period; Mishka and Interesting sum &lpar;离线树状数组&plus;前缀xor&rpar;

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

  3. Codeforces Round &num;365 &lpar;Div&period; 2&rpar; D - Mishka and Interesting sum(离线树状数组)

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

  4. Codeforces Round &num;365 &lpar;Div&period; 2&rpar;-D Mishka and Interesting sum(树状数组)

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

  5. Codeforces Round &num;365 &lpar;Div&period; 2&rpar; D&period;Mishka and Interesting sum

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

  6. Codeforces Round &num;225 &lpar;Div&period; 1&rpar; C&period; Propagating tree dfs序&plus;树状数组

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

  7. Codeforces Round &num;333 &lpar;Div&period; 1&rpar; C&period; Kleof&&num;225&semi;š 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 &num;510 &lpar;Div&period; 2&rpar; D&period; Petya and Array(树状数组)

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

  9. Codeforces Round &num;248 &lpar;Div&period; 2&rpar; B称号 【数据结构:树状数组】

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

随机推荐

  1. apache 日志轮询 linux cronolog

    Linux下运行的Web服务器Apache,默认日志文件是不分割的,一个整文件既不易于管理,也不易于分析统计.安装cronolog后,可以将日志文件按时间分割,易于管理和分析. cronolog安装配 ...

  2. SQL复制一个表的数据到另一个表

    最近做一个项目,由于客户数据量大,为了不将数据彻底删除,于是将数据移动到历史表,原始表的数据删除.由于技术有限,想不到好的方法,于是写个存储过程 执行,为了防止执行过程中出现异常,执行不完整.用到hI ...

  3. asp&period;net实现GZip压缩和GZip解压

    最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...

  4. servlet的生命周期与工作原理、使用!

    概念: Servlet是一个java程序运行在服务器上,处理客户端请求并做粗响应的程序!Servlet是和平台无关的服务器组件,它运行在Servlet容器中,Servlet容器 负责servlet和客 ...

  5. jquery 弹出层

    <!DOCTYPE html> <html>     <head>         <meta charset="utf-8">   ...

  6. poj 2425 A Chess Game&lowbar;sg函数

    题意:给你一个有向无环图,再给你图上的棋子,每人每次只能移动一个棋子,当轮到你不能移动棋子是就输了,棋子可以同时在一个点 比赛时就差这题没ak,做了几天博弈终于搞懂了. #include <io ...

  7. accp8&period;0转换教材第6章连接MySQL理解与练习

    JDBC_ODBC,纯java方式连接mysql 1.单词部分 ①JDBCjava连接数据库②driver manager驱动③connection连接④statement声明 ⑤execute执行⑥ ...

  8. 从零一起学Spring Boot之LayIM项目长成记(五)websocket

    前言 距离上一篇已经比较久的时间了,项目也是开了个头.并且,由于网上的关于Spring Boot的websocket讲解也比较多.于是我采用了另外的一个通讯框架 t-io 来实现LayIM中的通讯功能 ...

  9. 解决Configuration &&num;39&semi;compile&&num;39&semi; is obsolete and has been replaced with implementation

    项目中Gradle版本升级到4.4后,项目构建时,每次出现红色的警告信息: WARNING: Configuration 'compile' is obsolete and has been repl ...

  10. tex中把参考文献标题删除

    如果是book类<br>\renewcommand\bibname{}<br> 如果是article类<br>\renewcommand\refname{}