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]$的异或和为$ ...
随机推荐
- git更新代码出现错误
git pull代码时,出现如下的错误: SSL certificate problem: unable to get local issuer certificate 主要的原因是:没有ssl证书 ...
- Python学习笔记(三)windows下安装theano
2016.6.28补充: 不论是实验室的电脑还是我的笔记本,只要是windows下,theano.test()都是不通过的.虽然能使用一些theano中的函数,但是我感觉很不好. 所以还是转Ubunt ...
- EasyGBS国标流媒体服务器GB28181国标方案安装使用文档
EasyGBS - GB28181 国标方案安装使用文档 下载 安装包下载,正式使用需商业授权, 功能一致 在线演示 在线API 架构图 EasySIPCMS SIP 中心信令服务, 单节点, 自带一 ...
- 内网网络摄像机(RTSP/IPC/NVR)如何能在公网进行RTMP/HLS/HTTP-FLV直播
一.背景需求 传统监控行业里不管是设备端.服务器端亦或是客户端都在一个内网里面.而且现在的大部分监控方案都是这样的格局,小到一个公司范围内的监控,再到一个园区.一个仓库监控.一个农业园林监控.一个养殖 ...
- EasyNVR摄像机无插件直播进行摄像机云台控制的接入及调用详解
EasyNVR云台接入及控制详解 摄像机云台控制在摄像机当中很常见摄像机能将当前状态下云台的水平角度.倾斜角度和摄像机镜头焦距等位置参数存储到设备中,需要时可以迅速调用这些参数并将云台和摄像头调整至该 ...
- ZOJ 2059 The Twin Towers(双塔DP)
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...
- js验证表单大全3
2 >表单提交验证类 2.1 表单项不能为空 <scriptlanguage="javascript"> <!-- function CheckForm( ...
- .net全部版本的官方下载地址
https://technet.microsoft.com/zh-cn/5a4x27ek
- seaborn(matplotlib)画图,linux系统中文乱码等问题解决
data = pd.read_json(json.dumps(issue_dpl)) # set pic size plt.figure(figsize=(13, 5)) sns.set_style( ...
- Python Variable Scope
Python中的变量的作用域有时会让像我这样的初学者很头疼. 其实只需要掌握以下两点: 1. Python能够改变变量作用域的代码段是def.class.lamda; 而if/elif/else ...