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 Interesting sum
题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有出现偶数次的数异或的值。
思路:容易想到,把区间内的所有的数都异或得到的是出现奇数次的数的值,然后再异或该区间内的所有出现过的数(每个数只统计一次),得到的ans了。
第一个问题:得到询问区间的所有数的异或值,由 a[1~r] ^ a[0~(l-1)] = a[l~r] 可以用数组all_xor[i]保存a[1~i]区间的所有数的异或值,每次对询问区间的左右断点的all_xor值异或即可。
第二个问题:得到该区间内所有出现过的数的异或值,离线树状数组。具体操作如下:
先按照询问的右区间保存所有的询问g[r].(l, id),当前询问的结果保存在ans[id]里。
用树状数组one_xor[i]保存a[1~i]区间所有出现过的数(只统计一次)的异或值。遍历a[i],如果a[i]出现过了,对于后面的询问,右区间一定是>=i的,即i之前的区间都已经不关心这个值是否出现过了,
add_xor(mp[a[i]], a[i]),使前面的区间不再有这个数。然后mp[a[i]] = i , add_xor(mp[a[i]], a[i]) ; 这样保证了后面的每次询问都只在该最近一次出现过a[i]值的地方找到a[i]。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <map>
#define maxn 1000010
using namespace std; int a[maxn], one_xor[maxn], ans[maxn], all_xor[maxn]; //分别对应 原数组 (0~i)所有出现过的数的异或 保存ans (0~i)所有的数的异或 struct Query {
int l, id;
}; map<int, int>mp;
vector<Query> query[maxn];
int n, m; void init() {
mp.clear();
for (int i=; i<=maxn; ++i) {
query[i].clear();
}
memset(one_xor, , sizeof(one_xor));
} void add_xor(int x, int val) {
for (; x<=n; x+=(x&(-x)))
one_xor[x] ^= val;
} int sum_xor(int l, int r) {
int ans = ;
for (; r>; r-=(r&(-r))) ans ^= one_xor[r];
for (; l>; l-=(l&(-l))) ans ^= one_xor[l];
return ans;
} int main(){
while(~scanf("%d", &n)) {
init();
all_xor[] = ;
for (int i=; i<=n; ++i) {
scanf("%d", &a[i]);
all_xor[i] = all_xor[i-]^a[i];
}
scanf("%d", &m);
for (int i=; i<=m; ++i) {
int l, r;
scanf("%d%d", &l, &r);
query[r].push_back({l, i});
} for (int i=; i<=n; ++i) {
if (mp.count(a[i])) {
add_xor(mp[a[i]], a[i]);
}
mp[a[i]] = i;
add_xor(mp[a[i]], a[i]); for (int j=; j<query[i].size(); ++j) {
Query now = query[i][j];
ans[now.id] = (all_xor[i] ^ all_xor[now.l-]);
ans[now.id] ^= sum_xor(i, now.l-);
}
} for (int i=; i<=m; ++i) {
printf("%d\n", ans[i]);
}
}
return ;
}
这个题也算是让我理解了一些异或的操作吧,原来异或这么有意思~~
CF #365 (Div. 2) D - 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) ,问在每个区间里所有 ...
- 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 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D 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 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 树状数组+离线
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 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...
随机推荐
- HDU-4533 威威猫系列故事——晒被子(区间更新)
题目大意:在平面直角坐标系的第一象限中,给出n个矩形(可能重叠).有m次询问,每次询问点(t,t)的左下方的正方形区域中矩形的总面积(重叠部分重叠几次就得统计几次). 题目分析:线段树的叶子节点x维护 ...
- hdu2647 拓扑序
题意:年终要给 n 个员工发奖金,每个人的起始金额是888,有些人觉得自己做的比另一个人好所以应该多得一些钱,问最少需要花多少钱,如果不能满足所有员工的要求,输出 -1 拓扑排序,从奖金少的向奖金多的 ...
- spring之bean的作用域scope的值的详解
今天研究了一下scope的作用域.默认是单例模式,即 scope="singleton".另外scope还有prototype.request.session.global ses ...
- 如何给caffe添加新的layer ?
如何给caffe添加新的layer ? 初学caffe难免会遇到这个问题,网上搜来一段看似经典的话, 但是问题来了,貌似新版的caffe并没有上面提到的vision_layer:
- elasticsearch介绍集群,模拟横向扩展节点、节点宕机、改变分片
出处:[http://www.cnblogs.com/dennisit/p/4133131.html] ,防楼主删博,故保留一份! elasticsearch用于构建高可用和可扩展的系统.扩展 ...
- 常用MIME类型
后缀名 MIME名称*.3gpp audio/3gpp, video/3gpp*.ac3 audio/ac3*.asf allpication/vnd.ms-asf ...
- SQLServer 执行计划
http://www.cnblogs.com/fish-li/archive/2011/06/06/2073626.html#_label0 http://www.jb51.net/article ...
- (object sender,EventArgs e)是什么?
object sender:发出事件的对象 EventArgs e:对象中的数据
- redis主从配置及主从切换 转
redis主从配置及主从切换 转自 http://blog.sina.com.cn/s/blog_67196ddc0101h8v0.html (2014-04-28 17:48:47) 转载▼ 分 ...
- paypal接口对接注意事项
追加:新的设定画面 在paypal对接过程中,会存在return_url和notify两种 分别用pdt和ipn实现 但是对于paypal,大家请注意,真实环境和沙盒测试环境的区别 你可以到www.p ...