CF703D Mishka and Interesting sum
题意:给定一个1e6长度的值域1e9的数组。每次给定询问,询问区间内出现偶数次的数的异或和。
题解:首先很显然,每一次询问的答案,等于这个区间所有不同元素异或和异或上区间异或和。(因为出现偶数次的对区间异或和贡献为0,此时剩下的是出现奇数次的数,在取个补集即为答案)
区间异或和前缀和就好了,那问题转化为求区间不同元素异或和。由于这个东西区间合并很困难,所以在线算法是比较不优雅的。那我们考虑离线算法。我们按询问的右端点为第一关键字排序,然后处理到目前这个右端点位置的last数组,last数组定义为每个数最后出现的位置,然后给每个值对应的last附上它的值,这样我们一个区间求异或和可以得到区间不同元素异或和。树状数组一发就好了。
#include<bits/stdc++.h>
using namespace std;
#define N 1000000
#define LL long long
#define lowbit(x) ((x) & -(x)) inline LL read() {
LL x = , f = ; char a = getchar();
while(a < '' || a > '') { if(a == '-') f = -; a = getchar(); }
while(a >= '' && a <= '') x = x * + a - '', a = getchar();
return x * f;
} int n, a[N + ], ans[N + ], sum[N + ], Q;
int fen[N + ];
map<int, int> last; struct query {
int id, l, r;
bool operator < (const query & w) const {
return r < w.r;
}
} q[N + ]; inline void add(int pos, int val) { if(!pos) return; for(int i = pos; i <= n; i += lowbit(i)) fen[i] ^= val; } inline int querysum(int l, int r) {
int ret = ;
for(int i = r; i; i -= lowbit(i)) ret ^= fen[i];
for(int i = l - ; i; i -= lowbit(i)) ret ^= fen[i];
return ret;
} int main() {
n = read();
for(int i = ; i <= n; i++) a[i] = read();
for(int i = ; i <= n; i++) sum[i] = sum[i - ] ^ a[i];
Q = read();
for(int i = ; i <= Q; i++) q[i].id = i, q[i].l = read(), q[i].r = read();
sort(q + , q + + Q);
for(int pos = , i = ; i <= Q; i++) {
for(; pos <= q[i].r; pos++) {
add(last[a[pos]], a[pos]);
last[a[pos]] = pos;
add(pos, a[pos]);
}
ans[q[i].id] = querysum(q[i].l, q[i].r) ^ sum[q[i].l-] ^ sum[q[i].r];
}
for(int i = ; i <= Q; i++) printf("%d\n", ans[i]);
return ;
}
CF703D Mishka and Interesting sum的更多相关文章
- [CF703D]Mishka and Interesting sum/[BZOJ5476]位运算
[CF703D]Mishka and Interesting sum/[BZOJ5476]位运算 题目大意: 一个长度为\(n(n\le10^6)\)的序列\(A\).\(m(m\le10^6)\)次 ...
- 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 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- Mishka and Interesting sum
Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input ...
- CF #365 703D. Mishka and Interesting sum
题目描述 D. Mishka and Interesting sum的意思就是给出一个数组,以及若干询问,每次询问某个区间[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 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组
题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...
- Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)
题目链接 Mishka and Interesting sum 题意 给定一个数列和$q$个询问,每次询问区间$[l, r]$中出现次数为偶数的所有数的异或和. 设区间$[l, r]$的异或和为$ ...
随机推荐
- jmeter 参数值写入到指定的文件中[如获取token写入某文件为免登录备用]
1.编写java 类AppendFile ,代码如下 import java.io.File;import java.io.FileWriter;import java.io.IOException; ...
- Mac下通过shell脚本修改properties文件
通过shell脚本替换属性文件中的某行记录 假设有如下属性文件 demo.properties user.name=test user.password=123456 ................ ...
- 【BZOJ3325】[Scoi2013]密码 Manacher
[BZOJ3325][Scoi2013]密码 Description Fish是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进. ...
- pycharm中格式标准化代码
点击之后,可以使代码标准化
- <2013 08 12> Andrew:C语言的一点心得
C语言的特点在于,这是少见的中级语言(介于机器汇编和高级语言之间),因此它极其紧密地与特定机器架构.编译器.操作系统.库等基本概念相连.在底层,人们可以少量的甚至不使用汇编,但是不能不使用C.它以一种 ...
- Date、Calendar、DateFormat、SimpleDateFormat、Timer、TimerTask类
类 Date 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值. 它也允许格式化和解析日期字符串.不过,这些函数的 API 不易于实现国际化.从 ...
- Delphi Pdf的使用方法
此方法安装了llPDFLib.v3.6 控件.对pdf左侧.右侧正文进行了操作. procedure TForm1.Button1Click(Sender: TObject); var node,nd ...
- 转载:阮一峰 理解RESTful架构
转载 http://www.ruanyifeng.com/blog/2011/09/restful.html 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件&q ...
- LeetCode-11-6
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- python基础23 -----进程和线程
一.进程 1.什么是进程? 1.1 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成. 1.2 程序是指进程需要完成那些功能以及如何完成. 1.3 数据集 ...