Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门
题目大意:给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的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- 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) ,问在每个区间里所有 ...
- 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 ...
- Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)
http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...
- Codeforces Round #365 (Div. 2) B - Mishka and trip
http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...
随机推荐
- C++MFC编程笔记day03 MFC工具栏、状态栏、视图窗体
MFC工具栏 相关类: CToolBarCtrl - 父类是 CWnd 封装了工具栏控件相关操作 CToolBar - 父类是CControlBar 封装了工具栏和框架窗体之间的关系 工具栏使用: ...
- Sphinx-实战
配置完成后, 有了数据源.索引文件存储位置等, 便可以使用 indexer 工具创建索引, 收集要被检索的数据 -c 指定配置文件 默认使用 etc/sphinx.conf --all 对所有索引重新 ...
- jq的form验证
jQuery(document).ready(function(){ $('#cform img.contact-loader').hide(); $('#cform').submit(functio ...
- 用Fiddler 发送post请求
在调试web api的时候,若是get 请求,可以直接在浏览器里查看结果,如果是put,或者post请求在浏览器地址栏里就没有办法了. 下面介绍一下,如何利用fiddler模拟post请求. 也可以用 ...
- $("#SpecialAptitude").on("change",function(){CheckType($(this))})$("#SpecialAptitude").on("change",CheckType($(this)))
$("#SpecialAptitude").on("change",function(){CheckType($(this))})$("#Specia ...
- AMQP && MQTT comparision
1. AMQP (Advanced Message Queuing Protocol) 2. MQTT (Message Queuing Telemetry Transport) Introducti ...
- CSS/JQuery元素选择器之&&和||,选择器的逻辑操作
使用CSS或JQuery选中元素的时候.非常多时候我们须要对条件进行&&或者||操作. JQuery或者CSS中的||操作是非经常常使用的.也非常easy,就是通过逗号来分隔的. se ...
- HIVE的transform函数的使用
Hive的TRANSFORM关键字提供了在SQL中调用自写脚本的功能,适合实现Hive中没有的功能又不想写UDF的情况.例如,按日期统计每天出现的uid数,通常用如下的SQL SELECT date, ...
- Web App、Hybrid App、Native APP对比
- 点滴积累【C#】---序列化和反序列化
序列化和反序列化效果图: 序列化和反序列化代码: 需要添加两个命名空间: using System.IO; using System.Runtime.Serialization.Formatters. ...