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. 日期Date 对象常用的方法

    var mydate = new Date();//通过new方法创建对象 //alert(Date()); // 返回一个完整的日期时间 // alert(mydate.getDay());//返回 ...

  2. Linux 下线程的理解

    2017-04-03 最近深入研究了下Linux线程的问题,发现自己之前一直有些许误解,特记之…… 关于Linux下的线程,各种介绍Linux的书籍都没有深入去解释的,或许真的如书上所述,Linux本 ...

  3. Error-The content of element type "web-app" must match "(icon?,display-

    错误描述 The content of element type "web-app" must match "(icon?,display- name?,descript ...

  4. django 模板语言之 filter 自定义模板

    可以自己写python函数放在模板语言里用 这种方法是django里面的 filter {{ item.event_start|date:"Y-m-d H:i:s"}} {{ bi ...

  5. 21.如何将java类对象转化为json字符串

    使用阿里巴巴的fastJson 下载链接: 链接: https://pan.baidu.com/s/1dHjLOm1 密码: rr3w 用法如下: User user = new User(); us ...

  6. JS生成随机字符串的多种方法

    这篇文章主要介绍了JS生成随机字符串的方法,需要的朋友可以参考下 下面的一段代码,整理电脑时,记录备查. <script language="javascript"> ...

  7. PAT 1146 Topological Order[难]

    1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...

  8. PAT 1078 Hashing[一般][二次探查法]

    1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...

  9. Python(线程进程3)

    四 协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切 ...

  10. 对于近阶段公司代码 review 小结

    来新公司,给公司的SDK review了一下.发现了不少小问题,在此总结一下. (我下面说明问题可能是很简单,但是搞清楚某些问题还是花了些时间的,大家引以为戒吧) 先谈谈处理的问题: 1.某天QA说有 ...