Codeforces703D-Mishka and Interesting sum-离线树状数组
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
题意:传送门
原题目描述在最下面。
询问一个区间内出现次数为偶数次的数字的异或和。
思路:
先求出区间异或前缀和,其实就是出现次数为奇数次的数字的异或前缀和和。
然后用离线树状数组树状维护区间内区间内每种数字的前缀和。
最后的答案就是上面两个前缀和 差分一下 的异或和。
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
#include<bitset>
#include<unordered_map>
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x)&(-(x))
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = (int)1e6 +107;
int n, m;
LL pre[N], bit[N], ar[N], ans[N];
unordered_map<LL,int> vis;
void add(int x,LL c){
while(x<=n){
bit[x] ^= c;
x += lowbit(x);
}
}
LL query(int x){
LL sum = 0;
while(x){
sum ^= bit[x];
x -= lowbit(x);
}
return sum;
}
struct lp{
int l, r, id;
}cw[N];
bool cmp(lp &a, lp &b){
if(a.r!=b.r)return a.r<b.r;
return a.l<b.l;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
//freopen("E://ADpan//out.out", "w", stdout);
#endif
while(~scanf("%d",&n)){
memset(pre,0,sizeof(pre));
memset(bit,0,sizeof(bit));
for(int i=1;i<=n;++i){
scanf("%lld",&ar[i]);
pre[i] = pre[i-1]^ar[i];
}
scanf("%d",&m);
for(int i=0;i<m;++i){
scanf("%d%d", &cw[i].l, &cw[i].r);
cw[i].id = i;
}
sort(cw,cw+m,cmp);
vis.clear();
int r = 0;
for(int i=0;i<m;++i){
while(r<cw[i].r){
++r;
add(r, ar[r]);
if(vis[ar[r]]==0) vis[ar[r]] = r;
else {
add(vis[ar[r]], ar[r]);
vis[ar[r]] = r;
}
}
ans[cw[i].id]=pre[cw[i].r]^pre[cw[i].l-1]^query(cw[i].r)^query(cw[i].l-1);
}
for(int i=0;i<m;++i){
printf("%lld%c", ans[i], " \n"[i==m-1]);
}
}
return 0;
}
####原题目描述:

Codeforces703D-Mishka and Interesting sum-离线树状数组的更多相关文章
- 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 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- 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 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- 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(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- HDU 2852 KiKi's K-Number(离线+树状数组)
题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...
- 离线树状数组 hihocoder 1391 Countries
官方题解: // 离线树状数组 hihocoder 1391 Countries #include <iostream> #include <cstdio> #include ...
- 区间的关系的计数 HDU 4638 离线+树状数组
题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...
随机推荐
- luoguP1290 欧几里德的游戏 [博弈论]
题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的数的正整数倍,当然,得到的数 ...
- Dubbo 如何成为连接异构微服务体系的最佳服务开发框架
从编程开发的角度来说,Apache Dubbo (以下简称 Dubbo)首先是一款 RPC 服务框架,它最大的优势在于提供了面向接口代理的服务编程模型,对开发者屏蔽了底层的远程通信细节.同时 Dubb ...
- NX二次开发-Block UI C++界面关于 在Block UI中UF_initialize();和UF_terminate();的使用
关于 在Block UI中UF_initialize();和UF_terminate();的使用 用Block UI作NX二次开发的时候,不需要在使用UFUN函数的时候加UF_initialize() ...
- 理解CommonJS ,AMD ,CMD, 模块规范
参考 : https://blog.csdn.net/xcymorningsun/article/details/52709608 1.CommonJS 模块规范 (同步加载模块): var math ...
- (7)centos7 用户管理
1.创建用户 useradd meng 如果创建用户时没有指定属于哪个组,则默认会创建一个名字与用户相同的组并归属于此组 对应的home目录下回创建一个meng的文件夹 2.设置密码 passwd m ...
- linux进阶之路(一):linux入门
Linux:开源.免费得开源系统.具有高效性.稳定性.安全性.处理多并发. Linux的发行版本:基于Linux,不同的安装软件 CentOS(RedHat开源版本) RedHat Ubuntu Su ...
- Flink 编程模型
抽象层次 levels_of_abstraction 最低级的抽象接口是状态化的数据流接口(stateful streaming).这个接口是通过 ProcessFunction 集成到 Data ...
- 1067 Sort with Swap(0, i) (25 分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- 剑指offer——48把数字翻译成字符串
题目要求: 给定一个数字,按照如下规则翻译成字符串:0翻译成“a”,1翻译成“b”...25翻译成“z”.一个数字有多种翻译可能,例如12258一共有5种,分别是bccfi,bwfi,bczi,mcf ...
- kafka单机版的安装、集群部署 及使用
1.安装kafka(单机版) 1.1上传 kafka_2.11-2.0.0.tgz 到 /root/Downloads 1.2解压 tar 包 tar -zxvf kafka_2.11-2.0.0.t ...