CodeForces 703D Mishka and Interesting sum
异或运算性质,离线操作,区间求异或和。
直接求区间出现偶数次数的异或和并不好算,需要计算反面。
首先,很容易求解区间异或和,记为$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的更多相关文章
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组
题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...
- Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)
题目链接 Mishka and Interesting sum 题意 给定一个数列和$q$个询问,每次询问区间$[l, r]$中出现次数为偶数的所有数的异或和. 设区间$[l, r]$的异或和为$ ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- CF #365 703D. Mishka and Interesting sum
题目描述 D. Mishka and Interesting sum的意思就是给出一个数组,以及若干询问,每次询问某个区间[L, R]之间所有出现过偶数次的数字的异或和. 这个东西乍看很像是经典问题, ...
- 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 ...
- 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 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- 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) ,问在每个区间里所有 ...
随机推荐
- 使用highcharts显示mongodb中的数据
1.mongodb数据表相关 # 显示数据库 show dbs # 数据库 use ceshi # 显示表 show tables # 创建集合 db.createCollection('infoB' ...
- LocalDateTime返回的是Local时间
LocalDateTime返回的是本地时间,比如 LocalDateTime startDateTime = LocalDateTime.of(2016, 9, 18,00, 00); 返回的时间格式 ...
- Docker集群实验环境布署--swarm【6 配置上层Nginx代理,让任意Docker client访问得到高可用的管理API】
10.40.42.10上,也就是对应的VRRP中的10.40.42.1和2上,配置nginx tcp代理 # cat 4000_manager.venic.com_10.40.100.141-14 ...
- 未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad的真正解决办法
未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3 ...
- package com.sun.image.codec.jpeg does not exist
rt.jar 是sun公司内部使用的包,不建议外部使用,rt.jar是被用了的,但是里面的com.sun下面的包不被默认加载了, JAVA7之前是默认加载的,所有用JAVA7以前的JDK编译是通过的. ...
- jquery新版本不支持toggle()的解决方法
toggle() 方法用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的 click 事件. 在1.9以后官方废除了这个方法: 解决如下 在需要调用的js文件下引用 $.fn.toggle = ...
- >> 关于计算机有符号数的符号拓展(sign extension)问题
这里首先阐述相关规律, 情况为将位数较少的有符号存储空间中取出数据并放入更大有符号存储空间, 如: char → short . 规律: 将原空间符号位重复填充至新空间剩余位. eg.(负数情况, ...
- hdu 5996 dingyeye loves stone(博弈)
题目链接:hdu 5996 dingyeye loves stone 题意: 给你一棵树,树的每一个节点有a[i]个石子,每个人可以将这个节点的石子移向它的父亲,如果没有合法操作,那么就算输,现在给你 ...
- 动态网站加速,cdn义不容辞
"双十一"大战已经落下帷幕,各大电商纷纷拿出了亮眼的成绩单,但在这些成绩单的背后,CDN加速技术是功不可没的.随着互联网的发展,电商.视频直播等网站的火热,以及各个云加速平台的流行 ...
- StringBuilder跟StringBuffer
一直以来只知道StringBuffer是线程安全的,StringBuilder是线程不安全的, 所以通常情况下使用StringBuilder,这样可以提升效率!!! 今天由于想起StringBuild ...