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内所有数 ...
随机推荐
- POJ Girls and Boys (最大独立点集)
Girls and Boys Time Limit: 5000MS Memo ...
- 黑马程序员——JAVA基础之System,Runtime,Date,Calendar,Math
------- android培训.java培训.期待与您交流! ---------- System: 类中的方法和属性都是静态的. out: 标准输出,默认是控制台. in:标准输入,默认是键盘 ...
- Debugging a Parallel Application
Walkthrough: Debugging a Parallel Application https://msdn.microsoft.com/en-us/library/dd554943.aspx ...
- unity3d中切换武器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- orzdba的安装与使用
参考:http://code.taobao.org/p/orzdba/src/ http://code.taobao.org/p/myrelay/wiki/index/ http://www.taob ...
- sql server 如何在一个数据库中操作另一个数据库中的数据
INSERT INTO T1 SELECT * FROM OPENDATASOURCE( 'SQLOLEDB', 'Data Source=Serve ...
- linux服务之udevd
http://www.ibm.com/developerworks/cn/linux/l-cn-udev/[root@localhost ~]# uname -r2.6.32-431.el6.x86_ ...
- wikioi 1203 判断浮点数是否相等
/*======================================================================== 1203 判断浮点数是否相等 题目描述 Descr ...
- samba 服务
samba-client-3.6.9-167.el6_5.i686 samba-3.6.9-167.el6_5.i686 samba-common-3.6.9-167.el6_5.i686 samba ...
- Webproject 每次运行都停到workerDone(this); tomcat调试
那是因为你是Debug调试,你要将Dubug的时间设置长一些; 设置步骤: window-> preferences -> java -> debug -> commu ...