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

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

首先,很容易求解区间异或和,记为$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. MySQL索引类型

    一.简介 MySQL目前主要有以下几种索引类型:1.普通索引2.唯一索引3.主键索引4.组合索引5.全文索引 二.语句 CREATE TABLE table_name[col_name data ty ...

  2. ASP.NET中的C#基础知识

    ASP.NET中的C#基础知识 说明:asp.net作为一种开发框架现在已经广为应用,其开发的基础除了前端的html.css.JavaScript等后端最重要的语言支持还是C#,下面将主要用到的基础知 ...

  3. 斯坦福机器学习视频笔记 Week1 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  4. Error creating bean with name 'contentCategoryController': Injection of autowired dependencies failed;

    在springmvc.spring.mybatis搭建的工程中出现了一大串错误 查了代码后发现,原来是在ContentCategoryServiceImpl中忘了写一句话@Service(),以至于因 ...

  5. Apache server for win解压版的安装与配置

    下载地址: Apache 2.4.16 Win64位:VC14:http://www.apachelounge.com/download/VC14/binaries/httpd-2.4.16-win6 ...

  6. 面向对象---final关键字

    final 在java中表示的意思是最终,也称为完结器,可以用final关键字声明类.属性.方法,在声明时需要注意以下几点: 1.使用final声明的类不能有子类. 2.使用final声明的方法不能被 ...

  7. cuda编程(一)

    环境安装和例程运行 显卡主要有两家,ATI.NVIDIA,简称A卡和N卡.随着GPU计算能力的上升,采用GPU并行计算来加速的应用越来越多. Nvidia创立人之一,黄仁勋(Jen-Hsun Huan ...

  8. Unity启动事件-监听:InitializeOnLoad

    [InitializeOnLoad] :在启动Unity的时候运行编辑器脚本  官方案例: using UnityEngine; using UnityEditor; [InitializeOnLoa ...

  9. PHP不使用递归的无限级分类

    不用递归实现无限级分类,简单测试了下性能比递归稍好一点点点,但写得太复杂了,还是递归简单方便点 代码: <?php $list = array( array('id'=>1, 'pid'= ...

  10. iOS根据网络图片的size大小设置UIImageView的大小

    有时候在设置UIImageView的大小时候需要根据UIimage的长宽比来自动设置,不让图片原比例失真. 如果是从本地获取到的图片,[UIImage imageNamed:@"" ...