题目链接:传送门

题目大意:给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. zabbix触发的多条件判断表达式

    2 Trigger expression   Overview The expressions used in triggers are very flexible. You can use them ...

  2. 转:SiteMesh简介

    OS(OpenSymphony)的SiteMesh是一个用来在JSP中实现页面布局和装饰(layout and decoration)的框架组件,能够帮助网站开发人员较容易实现页面中动态内容和静态装饰 ...

  3. Python 的__name__属性

    Python 的__name__属性: 一个模块被另一个程序第一次引入时,其主程序将运行. 如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身 ...

  4. 【转】火星坐标、百度坐标、WGS-84坐标相互转换及墨卡托投影坐标转经纬度JavaScript版

    原文地址:https://www.cnblogs.com/fwc1994/p/5884115.html 火星坐标 火星坐标是国家测绘局为了国家安全在原始坐标的基础上进行偏移得到的坐标,基本国内的电子地 ...

  5. word2vector 理解入门

    1.什么是word2vector? 我们先来看一个问题,假如有一个句子 " the dog bark at the mailman". 假如用向量来表示每个单词,我们最先想到的是用 ...

  6. js 时间戳

    https://www.cnblogs.com/crf-Aaron/archive/2017/11/16/7844462.html var time = '2018-03-22 00:00:00'.r ...

  7. git 清除历史

    http://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git- ...

  8. atitit.基于  Commons CLI 的命令行原理与 开发

    atitit.基于  Commons CLI 的命令行原理与 开发 1. 命令行支持的格式有以下几种: 1 2. json化,map化的命令行参数内部表示 1 3. Ati cli 2 4. CLI库 ...

  9. [sh]清理memcached缓存

    #!/bin/bash ###author xxx ###date xxx ###清理内存缓存 used=`free -m | awk 'NR==2' | awk '{print $3}'` free ...

  10. iOS9新特性

    本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...