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

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

首先,很容易求解区间异或和,记为$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. C语言之函数的声明

    函数的声明 1.函数只能定义在函数外,不能定义在函数内 2.函数不允许重名,C语言中函数没有重载 3.函数只要一经定义,就可以在任意函数中调用 注意:如果函数定义在它调用之后,那么必须在调用之前,先声 ...

  2. LaTeX入门教程(二)

    LaTeX(LATEX,音译"拉泰赫")是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用 ...

  3. 数据意识崛起,从企业应用看BI软件的未来发展

    前阵子,和一群企业CIO聊天,希望从甲方角度看看对BI产品的看法.在问及一些成熟企业为何不上BI项目时,大家纷纷表示目前还处于观望状态. 提及BI,大家都觉得有些飘忽,和大数据一样,听着高大上,能真正 ...

  4. 驱动07.USB驱动程序

    1 了解USB识别的过程 eg:在Windows系统下的一个现象:把手机的USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱 ...

  5. hdu1037

    #include <iostream> #include <cstdio> using namespace std; int main() { int a,b,c; while ...

  6. 递归思路分解(C#)

    例子一:求1!+2!+......+X! 思路分解:因为是用递归思想解决问题,也就是方法调用方法.那么肯定的方法是重复利用的.在这道题里,我们要重复利用的也就是求X!和求和 所以我们先把求X!的代码写 ...

  7. 【.NET】Linq几样特点

    LinQ 是 .net framework 3.5的技术,对集合的查询和操作十分方便,结合LinQ Provider ,可以更方便操作XML和SQL(或Entity). LinQ有6样常用的特性: 自 ...

  8. ListView控制消息

    ListView控制消息 ListView控制消息是提供给父窗口或其他窗口通过发消息来控制ListView窗口本身. ListView控件提供给了以下消息来让外部程序控制自身: ListView_Ap ...

  9. google的作恶与不作恶

    Google刚刚出现时,那时互联网还似桃花源,路不拾遗夜不闭户,最多升级升级病毒库.Google的发展,从商业模式上带来了广告对互联网无孔不入的渗透,如今Google.百度.阿里等各大巨头都有自己的广 ...

  10. eclipse 一些快捷键

    快捷键 alt + 上下方向键 向后缩进 shift + tab 整体向左移动 tab 就是向右移动 ctrl + Q 就是构建有参的构造方法 ctrl + E 是get set 方法,要把quick ...