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(离线树状数组)的更多相关文章

  1. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  2. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  3. 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) ,问在每个区间里所有 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  8. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

    题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...

  9. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

随机推荐

  1. python基础-第七篇-7.2面向对象(进阶篇)

    进入到今天的探索前,我先对上节内容进行一下回顾: 面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个函数可供使用 对象是基于类创建的,实例用于调用被包装在类 ...

  2. The Highest Mark---hdu5501(问题转化 01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5501 二维数组: #include<stdio.h> #include<iostre ...

  3. Linux的概念与体系(转)

    学linux就用它了 http://www.cnblogs.com/vamei/archive/2012/10/10/2718229.html

  4. 需求-shidebing

    # 原始数据 list1 = [ {"c_id": "101", "e_code": "201"}, {"c_ ...

  5. Linux系统性能调优之性能分析

    1.Linux性能分析的目的1)找出系统性能瓶颈(包括硬件瓶颈和软件瓶颈):2)提供性能优化的方案(升级硬件?改进系统系统结构?):3)达到合理的硬件和软件配置:4)使系统资源使用达到最大的平衡.(一 ...

  6. LoadJS

    LoadJS是一个微小的异步加载器为现代浏览器(711字节). https://github.com/muicss/loadjs 介绍 LoadJS是一个微小的异步加载库的现代浏览器(IE9 +). ...

  7. [golang note] 变量常量

    变量 • 变量声明 √ golang变量声明的关键字为var. √ golang变量声明时类型信息放置在变量名之后. ▶ 单个变量声明 ▪ 语法如下 var name type ▪ 示例如下 var ...

  8. SCADA 必备函数之 :关于消息的函数

    Message Functions BroadcastSystemMessage//是将一条系统消息广播给系统中所有的顶级窗口. BroadcastSystemMessageEx//将消息发送到指定的 ...

  9. SQLAlchemy-Utils,提供choice功能

    SQLAlchemy操作数据库建表时,无法像Django一样提供choice方法,我们开头导入SQLAlchemy-Utils来为我们提供这个功能 pip3 install sqlalchemy-ut ...

  10. javascript 理解对象--- 属性类型

    ECMA-262 把对象定义为:无序属性的集合,其属性可以包含基本值.对象或者函数: var Person = { name:"wsc", age :"25", ...