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) ,问在每个区间里所有出现偶数次的数异或的值。
思路:容易想到,把区间内的所有的数都异或得到的是出现奇数次的数的值,然后再异或该区间内的所有出现过的数(每个数只统计一次),得到的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=1; i<=maxn; ++i) {
query[i].clear();
}
memset(one_xor, 0, 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 = 0;
for (; r>0; r-=(r&(-r))) ans ^= one_xor[r];
for (; l>0; l-=(l&(-l))) ans ^= one_xor[l];
return ans;
} int main(){
//freopen("in.cpp", "r", stdin);
while(~scanf("%d", &n)) {
init();
all_xor[0] = 0;
for (int i=1; i<=n; ++i) {
scanf("%d", &a[i]);
all_xor[i] = all_xor[i-1]^a[i];
}
scanf("%d", &m);
for (int i=1; i<=m; ++i) {
int l, r;
scanf("%d%d", &l, &r);
query[r].push_back({l, i});
} for (int i=1; 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=0; j<query[i].size(); ++j) {
Query now = query[i][j];
ans[now.id] = (all_xor[i] ^ all_xor[now.l-1]);
ans[now.id] ^= sum_xor(i, now.l-1);
}
} for (int i=1; i<=m; ++i) {
printf("%d\n", ans[i]);
}
}
return 0;
}
CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组的更多相关文章
- 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) 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内所有数 ...
随机推荐
- 如何在本机上将localhost改为www.dev.com
windows上安装好服务器后,打开本地目录 C:\Windows\System32\drivers\etc\ ,会看到有个hosts文件,打开后里面的代码为: # Copyright (c) - M ...
- iOS - Swift Dictionary 字典
前言 public struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConverti ...
- Myeclipse 主题下载
Myeclipse 主题下载 下载网址: 1. 编辑区背景主题:http://www.eclipsecolorthemes.org 2. 框架主题:https://marketplace.eclips ...
- [转载] 高流量大并发Linux TCP 性能调优
原文: http://cenwj.com/2015/2/25/19 本文参考文章为: 优化Linux下的内核TCP参数来提高服务器负载能力 Linux Tuning 本文所面对的情况为: 高并发数 高 ...
- Python学习(19)正则表达式
目录 Python 正则表达式 re.match 函数 re.search 方法 re.match 函数与 re.search 方法区别 检索和替换 正则表达式修饰符 - 可选标志 正则表达式模式 正 ...
- MyISAM与InnoDB的索引实现
1.MyISAM 使用B+Tree 作为索引结构,叶子节点的data存放指针,也就是记录的地址.对于主键索引和辅助索引都是一样的.2.InnoDB 也使用B+Tree作为索引结构,也别需要注意的是,对 ...
- 20160815_Redis安装
OS: CentOS6.4(x64) 参考网址: http://www.cnblogs.com/haoxinyue/p/3620648.html http://www.codeceo.com/arti ...
- 【转载】PHP运行模式的深入理解
PHP运行模式的深入理解 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-03我要评论 本篇文章是对PHP运行模式进行了详细的分析介绍,需要的朋友参考下 PHP运行模式有4钟:1) ...
- LinuxShell脚本攻略--第三章 以文件之名
生成任意大小的文件文件权限.所有权和粘滞位创建不可修改文件生成空白文件查找符号链接及其指向目标head 与 tail只列出目录的其他方法在命令行中用 pushd 和 popd 快速定位(cd -)统计 ...
- javascript之with的使用 弊端
妹的,昨天都快写完了,一不小心点了个关闭,然后...就没有然后了 wordpress的自动保存功能咋就这么不靠谱呢 记得还在懵懂学习JavaScript基础之时,坊间便有传言“with语句是低效率语句 ...