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) ,问在每个区间里所有 ... 
随机推荐
- 错误: 找不到或无法加载主类 tomcat-juli.jar
			错误: 找不到或无法加载主类 tomcat-juli.jar 然后就可以用了. 
- 基于WCF的RESTFul WebAPI如何对传输内容实现压缩
			前言 WCF作为通迅框架可以很容易地实现对消息的压缩,且方法不止一种,主要解决方法主要有以下四种: 1.通过自定义MessageEncoder和MessageEncodingBindingElemen ... 
- 700多个PHP版本随时切换,PHPWAMP共生模式与多档位综合教程。
			最近有学生向我咨询如何同时建立多个不同PHP版本站点,并自定义任意版本,软件是否可以多开,PHPWAMP如何设置才能与其他的环境同时使用等问题,本文将一一解决. 简单介绍一下PHPWAMP 你们应该会 ... 
- servlet的跳转
			首先要有RequestDispatcher对象(由request.getRequestDispatcher(根目录)注意不用谢后缀名java RequestDispatcher dispatcher= ... 
- 定位(position)
			position :属性规定元素的定位类型 语法: position : static | absolute | fixed | relative JavaScript语法:object.style. ... 
- python reduce使用实例
			通过一个简单的算法来了解reduce的巧用. 构建函数persistence(n),如果n>9,则返回0.否则继续根据n的权重来分解n,如n=999,则分解为9,9,9.那么将9*9*9=729 ... 
- JTable
			final Table table = new Table(parent, SWT.NONE | SWT.FULL_SELECTION); final GridData gd = new GridDa ... 
- FZU 2238 Daxia & Wzc's problem
			公式. $a×C_{m + i - 1}^m + d×C_{m + i - 1}^{m + 1}$. 推导过程可以看http://blog.csdn.net/queuelovestack/articl ... 
- python向服务器发送邮件事例
			import osimport sysimport re __author__ = 'xiaoming' import requestststr = '<div>\n<ul>\ ... 
- c语言对齐问题
			引言 考虑下面的结构体定义: typedef struct{ char c1; short s; char c2; int i; }T_FOO; 假设这个结构体的成员在内存中是紧凑排列的,且c1的起始 ... 
