题目传送门

题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和。

思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或,得到的其实就是出现次数为奇数的数字的亦或和,所以我们希望这段区间内的所有数字出现次数都+1,使奇偶性互换。

我们先处理出前缀的亦或和,这样可以得到次数为奇数的亦或和。

  接下来的问题就是要改变一段区间的奇偶性了,也就是说,这个问题其实就转化成了如何求一段区间出现的所有数字(无重复)。

这里我学到的是用树状数组离线处理的方式。核心代码如下。

  

    for(int i=;i<=m;i++){
while(p<=ask[i].r)
{
if(pos[a[p]]==)//对于每一个第一次出现的数字,加入树状数组,并且记录。
{
pos[a[p]]=p;
update(p,a[p]);
}else
{
update(pos[a[p]],a[p]);//如果曾经出现过,则将之前的位置清空,更新树状数组和记录。
update(p,a[p]);
pos[a[p]]=p;
}
p++;
}
ask[i].ans=pre[ask[i].r]^pre[ask[i].l-]^getxor(ask[i].r)^getxor(ask[i].l-);
}

如此操作后,比如我们询问的是1- R 的区间,此时肯定能得到我要的东西,但是如果我们询问的是1- r 这个区间的话(r<R),这个信息可能就不对了!!但是精彩的地方来了,由于我们事先对询问的区间排过序,也就是说,如果是纯粹的询问1-r区间,这个操作肯定在询问1-R之前,所以不会有影响,而如果我们计算的是r-R这个区间,此时1-r这个东西是出现在 减数 的位置的,也就是说,如果有一个数字在1-r和r-R中间都出现过,则此时1-r的树状数组为0,而1-R的树状数组为1!!

所以根据这个思路把他转化成亦或的求法就可以了。

接下来上代码。

#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<map>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=;
ll pre[maxn],tree[maxn<<],a[maxn];
int n,m;
struct node{
int l,r,id;
ll ans;
}ask[maxn];
bool cmp(const node a,const node b)
{
return a.r<b.r;
}
bool cmpid(const node a,const node b)
{
return a.id<b.id;
}
inline int lowbit(int k){
return (-k)&k;
}
inline void update(int x,ll val){
while(x<=n){
tree[x]^=val;
x+=lowbit(x);
}
}
inline ll getxor(int x){
ll ans=;
while(x>){
ans^=tree[x];
x-=lowbit(x);
}
return ans;
}
map<ll,int >pos;
int main(){
cin>>n;
scanf("%lld",&a[]);
pre[]=a[];
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
pre[i]=pre[i-]^a[i];
}
cin>>m;
for(int i=;i<=m;i++)
{
scanf("%d%d",&ask[i].l,&ask[i].r);
ask[i].id=i;
}
sort(ask+,ask++m,cmp);
int p=;
for(int i=;i<=m;i++){
while(p<=ask[i].r)
{
if(pos[a[p]]==)
{
pos[a[p]]=p;
update(p,a[p]);
}else
{
update(pos[a[p]],a[p]);
update(p,a[p]);
pos[a[p]]=p;
}
p++;
}
ask[i].ans=pre[ask[i].r]^pre[ask[i].l-]^getxor(ask[i].r)^getxor(ask[i].l-);
}
sort(ask+,ask++m,cmpid);
for(int i=;i<=m;i++)
{
printf("%lld\n",ask[i].ans);
}
}
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

Copy
3
3 7 8
1
1 3
output

Copy
0
input

Copy
7
1 2 1 3 3 2 3
5
4 7
4 5
1 3
1 7
1 5
output

Copy
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 .

codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组的更多相关文章

  1. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  2. Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)

    题目链接  Mishka and Interesting sum 题意  给定一个数列和$q$个询问,每次询问区间$[l, r]$中出现次数为偶数的所有数的异或和. 设区间$[l, r]$的异或和为$ ...

  3. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

  4. CodeForces 703D Mishka and Interesting sum

    异或运算性质,离线操作,区间求异或和. 直接求区间出现偶数次数的异或和并不好算,需要计算反面. 首先,很容易求解区间异或和,记为$P$. 例如下面这个序列,$P = A[1]xorA[2]xorA[3 ...

  5. CF #365 703D. Mishka and Interesting sum

    题目描述 D. Mishka and Interesting sum的意思就是给出一个数组,以及若干询问,每次询问某个区间[L, R]之间所有出现过偶数次的数字的异或和. 这个东西乍看很像是经典问题, ...

  6. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  7. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  8. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】

    题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...

  9. Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

随机推荐

  1. Use SFTP in Linux (转)

    From http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个交互式文件传输程式.它类似于 ftp, ...

  2. ESP8266-iot-2

    1.SDK概述 复制相关的工程文件到HelloWorld里面 要在版本esp8266_nonos_sdk_v2.0.0_16_07_19上面开发,那么就要复制相应文件 然后打开IDE 导入HelloW ...

  3. 解析json的方法

    解析json的两种方法:JS中的eval().JSON.parse eval不仅解析内容还会解析其中的方法,JSON.parse解析更安全.JSONLint可校验json的错误.

  4. 编写高质量代码改善C#程序的157个建议——建议24:迭代器应该是只读的

    建议24:迭代器应该是只读的 如果注意观察会发现,FCL中的迭代器只有GetEnumerator方法,没有SetEnumerator方法,所有的集合类也没有一个可以写的迭代器属性.原因有二: 一:这违 ...

  5. android获取USB设备的名称

    1.注释内 .是三星设备可能不支持,需要更换的代码. 2.mUsbManager.是getSystemService(Context.USB_SERVICE)获的. 3. 从stackoverflow ...

  6. 【单例模式】Singleton pattern

    前言:有很多时候,在一个生命周期中我们只要一个对象就可以了,比如:线程池,缓存,对话框,日志,显卡驱动等等.如果造出多个实例,就会导致许多问题产生,例如:程序的行为异常.资源使用过量,或者说不一致的结 ...

  7. angular 引入第三方库

    第一步 --save:自动写入package.json 第二步: 第三部: 为了让typescript识别$ 第四步:

  8. 自定义类型转换器 及 使用 ServletAPI 对象作为方法参数

    自定义类型转换器使用场景: jsp 代码:  <!-- 特殊情况之:类型转换问题 --> <a href="account/deleteAccount?date=2018- ...

  9. kali linux之手动漏洞挖掘二

    漏洞挖掘原则/宗旨----所有变量,所有头(cookie中的变量),逐个变量删除 身份认证 常用若口令/基于字典的密码爆破/锁定帐号 信息收集(手机号,身份证,住址信息等等) 密码嗅探 会话sessi ...

  10. cf555e

    cf555e(缩点) 给一个 n 个点 m 条边的图,以及 q 对点 (s,t),让你给 m 条边定向.问是否存在一种方案,使每对点的 s 能走到 t. \(n,m,q≤ 2×10^5\). 首先,在 ...