Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D
题意:
给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和。
思路:
这儿利用一下异或和的性质,在一个区间中,我们如果把所有数字都异或的话,可以发现最后偶数次的数字异或后都变成了0,只剩下了奇数次的数字异或。
举个例子,{1,2,3,2,3,5}
异或和是1^2^3^2^3^5=1^5
因为最后要计算偶数次数字的异或和,那么最后我们只需要再异或上该区间内所有不同数字即可。
那么我们可以先计算出前缀异或和,之后就只要求区间上的不同数字的异或和即可。
离线树状数组和在线树状数组的不同点是前者是先把所有询问存储下来,排序后再处理。
拿这道题目来说,我们将询问按照右端点从小到大排序,然后依次计算询问,如果当前数字在之前已经出现过,那么就先删去它,然后再插入该数字,这样就保证了这个区间内不同的数字只出现一次,具体可见代码。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL; const int maxn=1e6+; struct node
{
int l,r;
int id;
}q[maxn]; map<int,int> pos; int n,m;
int a[maxn];
int c[maxn];
int sum[maxn];
int pre[maxn];
LL ans[maxn]; bool cmp(node a,node b)
{
return a.r<b.r||(a.r==b.r && a.l<b.l);
} int lowbit(int x)
{
return x&-x;
} int XOR_sum(int x)
{
int ret=;
while(x>)
{
ret^=c[x];
x-=lowbit(x);
}
return ret;
} void add(int x,int d)
{
while(x<=maxn)
{
c[x]^=d;
x+=lowbit(x);
}
} int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d",&n))
{
pos.clear();
sum[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]^a[i];
pre[i]=pos[a[i]]; //记录a[i]这个数前面出现的位置
pos[a[i]]=i; //更新a[i]最晚的出现位置
}
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q,q+m,cmp);
memset(c,,sizeof(c));
for(int i=,r=;i<m;i++)
{
while(r<=q[i].r)
{
if(pre[r]) //如果第r个位置的数之前已经出现过,就删去这个数
add(pre[r],a[r]);
add(r,a[r]); //添加第r个数
r++;
}
ans[q[i].id]=XOR_sum(q[i].r)^XOR_sum(q[i].l-)^sum[q[i].r]^sum[q[i].l-];
}
for(int i=;i<m;i++)
printf("%I64d\n",ans[i]);
}
return ;
}
Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)的更多相关文章
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- 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) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- 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 ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
随机推荐
- 1.新建项目出现包名有一道红线The SDK platform-tools version ((23)) is too old to check APIs compiled with API 20
原因分析: 就是platform-tools的版本太低导致的 解决方法: 1.点开SDK Manager,打开SDK Tools面板,将Platform-tools更新 2.更新完之后重启as即可
- Shiro起步
1.测试环境 IntelliJ Idea 2.pom配置 <?xml version="1.0" encoding="UTF-8"?> <p ...
- linux内核介绍
linux系统可以分为:包括用户空间和内核空间两个部分. 现代cpu通常实现了不同的工作模式,以ARM为例,实现了7种工作模式: 用户模式.快速中断.外部中断.管理模式.数据访问中止.系统模式.未定义 ...
- PAT 1084 Broken Keyboard[比较]
1084 Broken Keyboard (20 分) On a broken keyboard, some of the keys are worn out. So when you type so ...
- nginx php fastcgi安装
CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一种语言编 ...
- 工作笔记——区块链POC
1.基础配置 安装SecureCRT 8.0链接到虚拟服务器,并配置docker 安装文件上传到服务器工具FileZilla
- Java游戏服务器成长之路——弱联网游戏篇(源码分析)
前言 前段时间由于公司的一款弱联网游戏急着上线,没能及时分享,现在基本做的差不多,剩下的就是测试阶段了(本来说元旦来分享一下服务器技术的).公司的这款游戏已经上线一年多了,在我来之前一直都是单机版本, ...
- Html.DropDownListFor的用法总结
在ASP.NET MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值. 注:重点是要将DropDownList的数据源转换成IEnumerable<SelectL ...
- java poi解析excel日期为数字的问题
这个数字是什么呢?是以1900年为原点,到2015年8月21日,之间经过的天数. 知道这个后,就很好处理了,我们拿到1900年的日期,在这个日期上加上42237天即可.如下: Calendar cal ...
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in Va ...