异或运算性质,离线操作,区间求异或和。

直接求区间出现偶数次数的异或和并不好算,需要计算反面。

首先,很容易求解区间异或和,记为$P$。

例如下面这个序列,$P = A[1]xorA[2]xorA[3]......xorA[15]$

$1$,$1$,$1$,$2$,$2$,$3$,$3$,$3$,$4$,$4$,$5$,$5$,$6$,$7$,$7$。

出现偶数次数的异或和记为$Q$,那么$Q = 2xor4xor5xor7$。

我们记$F=PxorQ$,如果知道$F$,那么就能计算出$Q$。所以接下来我们来看一下$F$是什么东西。

$F = 1xor1xor1xor2xor3xor3xor3xor4xor5xor6xor7 = 1xor2xor3xor4xor5xor6xor7$。

也就是说,$F$为区间去重之后的那些数的异或和。

那么,接下来问题变成了如何求解区间去重之后的数字的异或和。这个问题和$HDU3333$一模一样,只不过从$+$变成了$xor$。

$HDU3333$:$http://www.cnblogs.com/zufezzt/p/5805327.html$

求得了$F$之后,我们只需将$PxorF$,即可得到$Q$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const int maxn=;
int T,n;
int a[maxn],c[maxn],sum[maxn],Ans[maxn];
struct X { int id,L,R;LL ans; }q[maxn]; bool cmp(X a,X b) { return a.R<b.R; }
bool cmp1(X a,X b) { return a.id<b.id; } int lowbit(int x) { return x&(-x); }
void update(int p,LL v)
{
while(p<=n) c[p]=c[p]^v,p=p+lowbit(p);
}
int XOR(int p)
{
int res=;
while(p>) res=res^c[p],p=p-lowbit(p);
return res;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),sum[i]=sum[i-]^a[i];
map<int,int>m;
int Q; scanf("%d",&Q);
for(int i=;i<=Q;i++) scanf("%d%d",&q[i].L,&q[i].R),q[i].id=i;
sort(q+,q++Q,cmp); int p=;
for(int i=;i<=n;i++)
{
int h=m[a[i]];
if(h!=) update(h,a[i]); update(i,a[i]); m[a[i]]=i;
while(p<=Q&&q[p].R==i) q[p].ans=XOR(q[p].R)^XOR(q[p].L-), p++;
}
for(int i=;i<=Q;i++) Ans[q[i].id]=sum[q[i].R]^sum[q[i].L-]^q[i].ans;
for(int i=;i<=Q;i++) printf("%d\n",Ans[i]);
return ;
}

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 偶数亦或 离线+前缀树状数组

    题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...

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

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

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

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

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

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

  6. codeforces 703D D. Mishka and Interesting sum(树状数组)

    题目链接: D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megaby ...

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

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

  9. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

随机推荐

  1. [ios2]tableView去除空行的singleLine

    http://www.winddisk.com/2013/03/29/tableview%E5%8E%BB%E9%99%A4%E7%A9%BA%E8%A1%8C%E7%9A%84singleline/ ...

  2. 【Linux学习三】Linux系统目录架构

    主要包括: ●bin:保存的是可执行文件,二进制,就是命令 ●boot:引导目录,操作系统的启动加载,包含版本内核文件.greb引导程序- ●dev:硬件设备文件,如硬盘.网卡.声卡.终端.显卡,每一 ...

  3. PWA - 渐进式网络应用初认识

    Progressive Web Apps 简称PWA,是一种接近原生用户体验的渐进增强的web-app.从浏览器演进而来,沉浸式的体验,改进web的性能低下等.是Google 在2015年提出,今年才 ...

  4. 【JS学习笔记】第一个JS效果——鼠标提示框

    分析效果实现原理--鼠标提示框 样式:div的display 事件:onmouseover,onmouseout 编写JS的流程 布局:HTML+CSS 属性:确定需要修改哪些属性 事件:确定用户做哪 ...

  5. sublime & atom 插件

    1. autofilename(sublime) autocomplete-paths (atom): 自动路径 2. autoprefixer: 自动添加前缀  : https://github.c ...

  6. socket 心跳包机制

    心跳包的发送,通常有两种技术 方法1:应用层自己实现的心跳包  由应用程序自己发送心跳包来检测连接是否正常,大致的方法是:服务器在一个 Timer事件中定时 向客户端发送一个短小精悍的数据包,然后启动 ...

  7. js中substr,substring,indexOf,lastIndexOf等的用法

    1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert( ...

  8. Logstash利用GeoIP库显示地图以及通过useragent显示浏览器(四)

    我们通过Logstash收集的Nginx Access log中已经包含了客户端IP的数据(remote_addr),但是只有这个IP还不够,要在Kibana的显示请求来源的地理位置还需要借助GeoI ...

  9. Ubuntu 14.04—无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系 解决办法

    在Ubuntu中使用sudo apt-get install安装是有时候会出现: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系   解决办法 这样的错误,这是因为更新源 ...

  10. BIEE基本函数

    一,TRIM ,去除空字符 TRIM(EXPR) SUBSTRING("UT TIME"."月份" FROM 6 FOR 2) 1.AGGREGATE AT 此 ...