Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
【题目链接】 http://codeforces.com/contest/703/problem/D
【题目大意】
给出一个数列以及m个询问,每个询问要求求出【L,R】区间内出现次数为偶数的数的异或和。
【题解】
显然,我们很容易求出区间内出现次数为奇数的数的异或和,那么如果我们可以求出区间内出现的所有数的异或和,那么将两者异或就可以得到要求的东西。
我们记一个数字上一次出现的位置为pre,对于【L,R】中的数,如果其pre是小于L的,那么它肯定是第一次在这个区间出现,所以现在问题就转化为求【L,R】区间内所有pre小于L的数的异或和。
将【L,R】区间的查询拆分为L-1位置对L-1前缀的查询,R位置对L-1前缀,拆分后的查询仅和数列处理到的位置有关,因此考虑扫描线,以pre为下标,在树状数组中维护异或和,对拆分后的查询进行线扫描,依次处理答案。
【代码】
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
const int N=1000005;
map<int,int> t;
int f[N],m,n,c[N],a[N],pre[N],x[N],y[N],ans[N],tmp=0,sum[N];
int add(int x,int num){while(x<N)c[x]^=num,x+=x&-x;}
int query(int x){int s=0;while(x>0)s^=c[x],x-=x&-x;return s;}
struct data{int q,s,id,ans;}p[N*2];
bool cmp(data a,data b){return a.s<b.s;}
bool cmp0(data a,data b){return a.id<b.id;}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]^a[i];
pre[i]=t[a[i]];
t[a[i]]=i;
}scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&x[i],&y[i]);
p[i*2-1].q=x[i]-1;p[i*2].q=x[i]-1;
p[i*2-1].s=x[i]-1;p[i*2].s=y[i];
p[i*2-1].id=i*2-1;
p[i*2].id=i*2;
}int cnt=1;
sort(p+1,p+2*m+1,cmp);
while(p[cnt].s==0)cnt++;
for(int i=1;i<=n;i++){
add(pre[i]+1,a[i]);
while(p[cnt].s==i){
p[cnt].ans=query(p[cnt].q+1);
cnt++;if(cnt>2*m)break;
}
}sort(p+1,p+2*m+1,cmp0);
for(int i=1;i<=m;i++)printf("%d\n",p[2*i].ans^p[2*i-1].ans^sum[x[i]-1]^sum[y[i]]);
return 0;
}
Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)的更多相关文章
- 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 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- Codeforces 786C Till I Collapse(树状数组+扫描线+倍增)
[题目链接] http://codeforces.com/contest/786/problem/C [题目大意] 给出一个数列,问对于不同的k,将区间划分为几个, 每个区间出现不同元素个数不超过k时 ...
- 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
异或运算性质,离线操作,区间求异或和. 直接求区间出现偶数次数的异或和并不好算,需要计算反面. 首先,很容易求解区间异或和,记为$P$. 例如下面这个序列,$P = A[1]xorA[2]xorA[3 ...
- CF #365 703D. Mishka and Interesting sum
题目描述 D. Mishka and Interesting sum的意思就是给出一个数组,以及若干询问,每次询问某个区间[L, R]之间所有出现过偶数次的数字的异或和. 这个东西乍看很像是经典问题, ...
- Codeforces Round #365 (Div. 2) D 树状数组+离线处理
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
随机推荐
- MYSQL区分大小写
MYSQL区分大小写 1.linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写: 2.用root帐号登录后,在/etc/my.cnf 中的[mysqld]后添加添加lower ...
- wdcp升级php和mysql
安装没什么好说的,按照wdcp官方去搞就行了,这里如果是centos系统建议使用rpm安装方式附件如下(这里包含了本文后面用到的三个文件) http://download.csdn.net/detai ...
- Swift 基本数据类型
Swift 1,Swift支持所有C和Objective-C的基本类型,支持面向过程和面向对象的编程机制. 2,Swift提供了两种功能强劲的集合类型:数组和字典. 3,元组. 4,可选类型. 5,S ...
- textarea 输入框限制字数
在textarea标签中,只需要设置maxlength=”***”即可,但是在textarea标签中,IE9及IE9以下浏览器是不支持的,IE10.IE11则支持,估计后续的版本应该都会支持. 现在来 ...
- Android 更好的Activity生命周期回调
/** * This class allows you to listen to when the user is entering the background (i.e. after a home ...
- C#学习日志 day7 --------------LINQ与Lamda语句的初步尝试以及XML的生成
LINQ是一种集成在计算机语言里的信息查询语句,是c#3.0中最惹人瞩目的功能. 在C#中,LINQ语句有两种写法. 第一种写法与SQL语句类似: IEnumerable<Customer> ...
- vb sqlite 使用 litex
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal ...
- CSS自学笔记(6):CSS的模型
CSS 框模型 (Box Model) 规定了元素框处理元素内容.内边距.边框和外边距的方式. 图片来源:w3school.com.cn 元素(element)是一个html文档的实际内容区域,依次由 ...
- 项目关联不上开源项目(library)
首先是add library的时候重新打开始始终显示关联错误,在reference中出现一个红叉,无论如何都解决不了,remove后重新添加也会有这个问题,Eclipse也没有任何出错提示. 最后在h ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...