题目链接:传送门

题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和

题目思路:前缀和+离线处理+树状数组

     首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数异或起来就是答案,那么出现偶数次的数就可以

     先求出区间 l,r 内有多少不同的数,将这些数异或起来,再异或上区间内出现奇数次的数的异或和就是答案。(出现偶数次的数异或后为0,奇数次的数异或后是本身

       然后离线处理询问,对询问按右端点 sort,因为树状数组保存的是数出现的最后位置。离线处理询问后便于数组的更新以及答案的更新。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 1000005
#define maxn 100005
typedef pair<int,int> PII;
typedef long long LL; int n,m,flag;
int sum[N],tree[N],a[N];
struct Node{
int l,r;
int v,id;
bool operator<(const Node&a)const{
return r<a.r;
}
}node[N];
map<int,int>M;
bool cmp(const Node&a,const Node&b){return a.id<b.id;}
void add(int i,int v){for(;i<=n;i+=(i&-i))tree[i]^=v;}
int getsum(int i){int res=;for(;i;i-=(i&-i))res^=tree[i];return res;}
int main(){
int i,j,group,temp;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(i=;i<=n;++i){
scanf("%d",&a[i]);
sum[i]=sum[i-]^a[i];
}
scanf("%d",&m);
for(i=;i<=m;++i){
scanf("%d%d",&node[i].l,&node[i].r);
node[i].id=i;
}
sort(node+,node+m+);
j=;
for(i=;i<=n;++i){
int &t=M[a[i]];
if(t){
add(t,a[i]);
}
t=i;
add(t,a[i]);
while(j<=m&&node[j].r==i){
int l=node[j].l-;
int r=node[j].r;
node[j].v=getsum(r)^getsum(l)^sum[r]^sum[l];
++j;
}
}
sort(node+,node+m+,cmp);
for(i=;i<=m;++i)printf("%d\n",node[i].v);
return ;
}

Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum的更多相关文章

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

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

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

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

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

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

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

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

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

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

  8. Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)

    http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...

  9. Codeforces Round #365 (Div. 2) B - Mishka and trip

    http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...

随机推荐

  1. Vue学习小结

    ES6 let完全可以取代var const声明一个只读的常量 箭头函数:可以绑定this对象,大大减少了显式绑定this对象的写法(call.apply.bind) 函数绑定(function bi ...

  2. jumpserver v0.5.0 创建用户和管理机器

    用户管理-创建用户    data 用户详情 如下 创建用户组 data 资产列表添加资产 jumpserver 的 root 公钥需保持到 后端服务器的 authorized_keys 里, 然后测 ...

  3. CSS3图片折角效果

    本篇文章由:http://xinpure.com/css3-picture-angle-effect/ 图片折角效果主要是通过设置 border 属性实现的效果 效果预览 效果解析 假设我们将一个元素 ...

  4. IE6—在链接click事件的响应函数中发送jsonp请求不生效

    $("#link").click(function(){     $.ajax({         type: 'GET',         dataType: 'jsonp', ...

  5. 安装Windows Service总是发生异常!

    打开VS2010 创建个windows服务应用程序!没有添加删除任何一行代码!然后按照下面的步骤 1. 将这个服务程序切换到设计视图2. 右击设计视图选择“添加安装程序”3. 切换到刚被添加的Proj ...

  6. Web App、Hybrid App、Native APP对比

  7. [svc][op]如何查看当前Ubuntu系统的版本

    如何查看当前Ubuntu系统的版本 说来也惭愧,用Ubuntu差不多快1个月了,双系统是让朋友安的,只知道自己使用的是什么12版本的,具体怎么看还不知道,下面写一下查看当前Linux系统的版本的方法 ...

  8. C#实现的自定义IIS认证模块 转载

    IIS7以后application pool都支持两种模式:经典模式和集成模式. 所谓经典模式就是与IIS6的application pool运行模式相同,对于asp.net的页面请求处理由单独的as ...

  9. 使用thrift进行跨语言调用(php c# java)

    使用thrift进行跨语言调用(php c# java)   1:前言 实际上本文说的是跨进程的异构语言调用,举个简单的例子就是利用PHP写的代码去调C#或是java写的服务端.其实除了本文提供的办法 ...

  10. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...