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 ...
随机推荐
- PHP设计模式之单例模式(数据库访问)
1.什么是单例模式? 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用. 2.单例模式的 ...
- python模块—socket
创建套接字: socket.socket(family,type[,protocal]) 使用给定的地址族.套接字类型.协议编号(默认为0)来创建套接字. 1. socket类型: socket.AF ...
- alois
Background It's not simple to know what happens in a bigger network. There's a multitude of applicat ...
- VIM+qmake编译示例程序HelloQt出错问题的解决(文件名一定要使用.cpp,否则就会默认使用gcc编译,当然通不过)
之前看到很多初学Qt的Linux友们在使用qmake编译第一个HelloQt或者HelloWorld程序时报错,并且始终找不到原因. 前几天我也遇到了同样的问题,我用的是<精通Qt4编程> ...
- firemonkey打开子窗体
procedure TForm1.Button1Click(Sender: TObject);varChildForm: TForm2;beginChildForm := TForm2.Create( ...
- 使用libcurl提交POST请求
最近在学习libcurl,并利用它提交POST请求,可是返回的响应总是无从验证该次POST请求是否成功提交了. 1. 先看下根据firebug提交的一次成功的请求,这里以login我喜欢上的xiami ...
- nyist 303序号互换(数学推理)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=303 思路: 开始看错题了,以为最多只有两个字母. 字母转数字的表达式很容易看出来是:(2 ...
- 2013 多校联合 F Magic Ball Game (hdu 4605)
http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...
- golang 之 defer(统计函数执行时间)
package main import ( "fmt" "time" ) func sum(a ...int) int { defer trace(" ...
- SQL学习之高级联结(自联结、自然联结、外联接)
create table Customers( Id ,), Company ) null, Name ) null ) insert into Customers values('Fun4All', ...