转载自: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 离线树状数组(转)的更多相关文章

  1. 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) ,问在每个区间里所有 ...

  2. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  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 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  5. 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 ...

  6. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

  7. 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 ...

  8. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  9. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

    题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...

随机推荐

  1. Codeforces Round #147 (Div. 2)

    A. Free Cash 判断值相同的最长长度. B. Young Table 按从上到下,从左到右排序,每个位置最多交换一次. C. Primes on Interval \(p_i\)表示位置\( ...

  2. URAL 1160 Network(最小生成树)

    Network Time limit: 1.0 secondMemory limit: 64 MB Andrew is working as system administrator and is p ...

  3. Linux 安装rar解压工具

    下载RAR安装包: http://www.rarsoft.com/download.htm 我的是CentOS 64位: wget http://www.rarsoft.com/rar/rarlinu ...

  4. HTTP 状态消息

    1xx: 信息 消息: 描述: 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. 101 Switching Protocols 服务器 ...

  5. [poj 3691]DNA repair

    好久没刷 poj 了,今天练习 AC 自动机时去水了一发喵~ 在 poj 上 A 题的感觉并没有 BZOJ 上那么愉悦,准确的说是痛不欲生 真是应了那句老话,你再慢也有比你慢的,你再快也有比你快的…… ...

  6. caffe: compile error: Could not open or find file your path~~/resized_data/0 and a total of 2 images .

    I0219 14:48:40.965386 31108 net.cpp:76] Memory required for data: 0I0219 14:48:40.965517 31108 layer ...

  7. FreeSWITCH在呼叫失败的情况下播放语音提示

    看到好多网友问到这个问题.一般我们在打电话时会听到“您拨的电话正在通话中,请稍后再拨....”,或“电话无应答...”之类的提示,我们在 freeswitch 里也可以这样做. 其实很简单,默认的配置 ...

  8. java 动态代码生成。

    http://stackoverflow.com/questions/2320404/creating-classes-dynamically-with-java https://zeroturnar ...

  9. Xcode 7 ImageNamed 方法加载jpg图片失败

    更新XCode7后 原来的Image.xcassets文件夹变成了Assets.xcassets 把01.jpg,02.jpg,03.png拖入这个文件夹中 UIImage* test1=[UIIma ...

  10. ABBYY 识别结果的文档怎么导出

    使用ABBYY FineReader Pro for Mac OCR文字识别软件识别文档时,识别结果可以保存至一个文件.复制到剪贴板或通过电子邮件发送.可以执行下列操作:导出整个文档.仅导出所选页面. ...