题目链接:传送门

题目大意:给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. python-__init__.py 与模块对象的关系

    python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的. ...

  2. Python vs Ruby: 谁是最好的 web 开发语言?

    Python 和 Ruby 都是目前用来开发 websites.web-based apps 和 web services 的流行编程语言之一. 这两种语言在许多方面有相似之处.它们都是高级的面向对象 ...

  3. 几种TCP连接中出现RST的情况(转载)

    TCP RST 网络 linux 目录[-] 1 端口未打开 2 请求超时 3 提前关闭 4 在一个已关闭的socket上收到数据 总结 参考文献: 应该没有人会质疑,现在是一个网络时代了.应该不少程 ...

  4. leetcode——Lowest Common Ancestor of a Binary Tree

    题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次 ...

  5. ⽤运营的思路来做无线产品測试-第13届BQConf上的分享

    ⽤运营的思路来做无线产品測试,在2014.10.25.第13届B'QConf(北京软件质量大会)上分享的一个主题.主要是关于京东无线測试的一些实践,包含android和ios的代码覆盖率.无线的接口自 ...

  6. Openresty + nginx-upload-module支持文件上传

    0. 说明 这种方式其实复杂,麻烦!建议通过这个方式搭建Openresty文件上传和下载服务器:http://www.cnblogs.com/lujiango/p/9056680.html 1. 包下 ...

  7. 基于libmemcached,php扩展memcached的安装

    基于libmemcached,php扩展memcached的安装 张映 发表于 -- 分类目录: php 标签:libmemcached, memcached, php, 安装 一,为什么要装memc ...

  8. 5.3日,7:20开始 阮一峰js的早课学习

    Infinity - Infinity // NaN Infinity / Infinity // NaN Infinity + Infinity // Infinity Infinity * Inf ...

  9. 0057 Spring MVC如何获取HTTP请求头信息--URL中的动态参数--@RequestHeader--@CookieValue--@PathVariable

    获取HTTP请求头信息 一个HTTP请求除了有参数和实体内容外还有HTTP请求头信息,Spring MVC也可以获取这部分信息 @RequestHeader解可以将请求头信息映射到处理方法的形参上 @ ...

  10. 跟着百度学PHP[15]-SESSION的应用/网站登陆案例完整案例

    先把几个应该要有的页面建立好.