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]$的异或和为$ ...
随机推荐
- Spring MVC的路径匹配
Spring MVC中的路径匹配比起标准web.xml的servlet映射要灵活得多.路径匹配的默认策略是由org.springframework.util.AntPathMatcher实现的.顾名思 ...
- activity通过流程实例id动态获取流程图并展示在jsp页面上
提供的Service方法如下: Java /** * 获取当前任务流程图 * * @param processInstanceId * @return */ @Override public Inpu ...
- c# 如何利用异或运算进行简单加密解密
利用“^”异或运算对字符串进行加密 原理:按位做“异或”运算是->位值相同得1,不同得0,如下计算 1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0 例如: < ...
- CSV导出
CSV 导入导出工具类 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; impor ...
- python系列六:Python3元组tuple
'''元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号.''''''uple元素不可变有一种特殊情况,当元素是可变对象时.对象内部属性是可以修改的!tuple的不可变限制 ...
- 【转】PowerVM 的主要组成部分及概念
PowerVM 是在基于 IBM POWER 处理器的硬件平台上提供的具有行业领先水平的虚拟化技术家族.它是 IBM Power System 虚拟化技术全新和统一的品牌(逻辑分区,微分区,Hyper ...
- Js slice()方法和splice()方法
1.slice(start,end) 从已有的数组中返回选定元素,参数start必填,end选填 <script> delArray(); function delArray(){ var ...
- PHP判断是手机端访问还是PC端访问网站
Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class, 它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境. 这个设备检测 ...
- 006-MySQL中使用SHOW PROFILE命令分析性能
一.概述 1.版本支持 Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后. 查看数据库版本: Select version(); 2.查看开启关闭和默认 ...
- 基于CentOS的SSHD服务的Docker镜像
原文地址 1.Dockerfile文件 FROM registry.aliyuncs.com/acs-sample/centos:6 MAINTAINER xuqh "xqh_163@163 ...