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 ...
随机推荐
- URL地址中的字符串转换
url出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,怎么解决?解决办法将这些字符转化成服务器可以识别的字符,对应关系如下:URL字符转义 用其它 ...
- Linux(5)- MariaDB、mysql主从复制、初识redis
一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...
- 009-Hadoop Hive sql语法详解4-DQL 操作:数据查询SQL-select、join、union、udtf
一.基本的Select 操作 语法SELECT [ALL | DISTINCT] select_expr, select_expr, ...FROM table_reference[WHERE whe ...
- 我的第一次NGS分析操作
部分参考自:生物信息学100个基础问题 —— 番外2: 用Anaconda快速搭建生物信息学分析平台 - 孟浩巍的文章 - 知乎 https://zhuanlan.zhihu.com/p/357114 ...
- 记录:python读取excel文件
由于最近老是用到python读取excel文件,所以特意记录一下python读取excel文件的大体框架. 库:xlrd(读),直接pip安装即可.想要写excel文件的话,安装xlwd库即可,也是直 ...
- Selenium 方法封装 一
Selenium 封装 Selenium 封装 WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐: WebElement element =driver. ...
- vertical-align和line-height的深入应用
vertical-align和line-height的深入应用 本文的重点是了解vertical-align和line-height的使用 涉及到的名词:基线,底端,行内框,行框,行间距,替换元素及非 ...
- js自动类型转换
<script> testeq0 = null == undefined; testeq1 = "0" == 0; //比较前字符串转换为数字 testeq2 = 0 ...
- 04 linux用户群组和权限
作业一: 1)新建用户natasha,uid为1000,gid为555,备注信息为“master” 2)修改natasha用户的家目录为/Natasha 3)查看用户信息配置文件的最后一行 4)为na ...
- 软工网络15团队作业1——团队组队&展示
一.团队展示 1.队名:想不出队名 2.队员学号(标记组长) 201521123064 郭炜埕 201521123066 郑晓丽 201521123067 廖怡洁 201521123068 包梦榕 2 ...