Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)
题目链接 Mishka and Interesting sum
题意 给定一个数列和$q$个询问,每次询问区间$[l, r]$中出现次数为偶数的所有数的异或和。
设区间$[l, r]$的异或和为$s(l, r)$, 区间$[l, r]$中所有出现过的数的异或和为$c(l, r)$
那么每个询问的答案为$s(l, r)$ $xor$ $c(l, r)$。
对于$s(l, r)$的求解维护一个前缀和即可。
对于$c(l, r)$, 把所有的询问离线并按照左端点升序排序(右端点无所谓,但是我程序里还是考虑了)
然后把数列中所有第一个出现的数加到树状数组里。
每个询问处理下来,要保证当前询问的$l$之前的$l - 1$个数已经在树状数组中被删除。
所谓被删除就是当前位置$x$上的元素被移去。
同时考虑位置$y$,满足$a_{x}=a_{y}, y > x$且$y$最小。(在$a_{x}$第一个和$a_{x}$值相同的元素的位置)
在位置$y$上把$a_{y}$加入树状数组,这样就可以离线求$c(l, r)$了。
时间复杂度$O(nlogn)$。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 1e6 + 10; struct node{
int l, r, id;
friend bool operator < (const node &a, const node &b){
return a.l == b.l ? a.r < b.r : a.l < b.l;
}
} q[N]; int a[N], c[N], nxt[N];
int n, m;
int ans[N], s[N];
int l;
unordered_map <int, int> mp; int update(int x, int val){
for (; x <= n; x += x & -x) c[x] ^= val;
} int query(int x){
int ret = 0;
for (; x; x -= x & -x) ret ^= c[x];
return ret;
} int undo(int x){
update(x, a[x]);
if (nxt[x]) update(nxt[x], a[nxt[x]]);
} int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%d", a + i);
rep(i, 1, n) s[i] = s[i - 1] ^ a[i]; rep(i, 1, n){
if (mp.count(a[i])) continue;
else{
mp[a[i]] = 1;
update(i, a[i]);
}
} mp.clear(); dec(i, n, 1){
nxt[i] = (mp.count(a[i])) ? mp[a[i]] : 0;
mp[a[i]] = i;
} scanf("%d", &m);
rep(i, 1, m){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
} sort(q + 1, q + m + 1); l = 1;
rep(i, 1, m){
while (l < q[i].l){
undo(l);
++l;
} ans[q[i].id] = query(q[i].r) ^ s[q[i].r] ^ s[q[i].l - 1];
} rep(i, 1, m) printf("%d\n", ans[i]);
return 0;
}
Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)的更多相关文章
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- 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 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 , ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组
题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...
- CodeForces 703D Mishka and Interesting sum
异或运算性质,离线操作,区间求异或和. 直接求区间出现偶数次数的异或和并不好算,需要计算反面. 首先,很容易求解区间异或和,记为$P$. 例如下面这个序列,$P = A[1]xorA[2]xorA[3 ...
随机推荐
- 动态规划:ZOJ1074-最大和子矩阵 DP(最长子序列的升级版)
To the Max Time Limit:1 Second Memory Limit:32768 KB Problem Given a two-dimensional array of po ...
- 如何在Linux下使用Rsync
如何在Linux下使用Rsync 吐槽 昨天对scp进行总结之后看到最后有说到Rsync,俗语有云:好奇心害死猫.抱着学习的态度将Rsync给找了出来,然后进行了一些简单的学习.下面介绍一些个常用的命 ...
- IOS开发---菜鸟学习之路--(七)-自定义UITableViewCell
本篇将介绍如何自定义 UITableViewCell 首先选择新建文件 可以直接使用快捷键 COMMAND+n打开新建页面,然后选Objective-C class 然后选择继承之UITableVie ...
- 【Decode Ways】cpp
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- Python-S9-Day125-Web微信&爬虫框架之scrapy
01 今日内容概要 02 内容回顾:爬虫 03 内容回顾:网络和并发编程 04 Web微信之获取联系人列表 05 Web微信之发送消息 06 为什么request.POST拿不到数据 07 到底使用j ...
- java课堂 笔记
- [状态更新]MSE三个月快速复习计划,成功考上复旦软工
最后更新,6月21日收到录取通知书啦,感谢当初不曾放弃的自己: 更新一下状态: 3.3日 分数出来了,过了复试线. 最初写这篇博客的时候,是希望自己能够每天或者至少每周更新下自己的复习状态,这样能够确 ...
- leetcode NO.7 反转整数 (python实现)
来源 https://leetcode-cn.com/problems/reverse-integer/description/ 题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 ...
- Linux抓包工具tcpdump命令详解
1.简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中 ...
- 快乐的Linux命令行
ls - 列出目录内容 -a 列出所有文件 -d 指定目录信息 -F 为目录增加/标识 -h 增强可读性 -l 列模式显示 -r 反序显示 -S 按照大小排序 -t 按照修改时间排序 file - 确 ...