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 ...
随机推荐
- 笔记-python-standard library-12.1 pickle
笔记-python-standard library-12.1 pickle 1. pickle简介 source code: Lib/pickle.py pickle模块实质上是一个实现p ...
- sql中一个服务器建立另一个服务器的连接
EXEC sp_addlinkedserver 'TonyLink','','SQLOLEDB','111.111.1.111(服务器名)' EXEC sp_addlinkedsrvlogin 'To ...
- 【正则】对RegExp执行typeof运算的结果
对RegExp执行typeof运算的结果并不统一,在有些浏览器中返回“function”,在有些中返回“object”. 谷歌: 火狐 IE: **
- Python+Selenium练习篇之8-利用css定位元素
前面介绍了,XPath, id , class , link text, partial link text, tag name, name 七大元素定位方法,本文介绍webdriver支持的最后一个 ...
- SQL SERVER存储引擎——04.数据
4. SQL SERVER存储引擎之数据篇 (4.1)文件 (0)主数据文件.mdf初始文件大小至少为3MB,次要数据文件.ndf初始大小,同日志文件一样至少为512KB: (1)SQL SERVER ...
- OgnlValueStack 源码
/* * Copyright 2002-2006,2009 The Apache Software Foundation. * * Licensed under the Apache License, ...
- .net学习笔记--设计模式
设计模式都有哪些? 按照GOF提出,23种,按照目的分为:创建型(creational).结构性(structural).行为型(behavioral). 一.创建型: 1.Singleton 单例模 ...
- 我的第一个python程序——猜数字
#Author:xiaoxiao age = 22 #标准正确答案 counter = 0 #计数器 for i in range(10): #循环10次 if counter < 3: gue ...
- 手机安装app总是显示未安装
手机安装软件总是显示未安装 查看是否开启了护眼模式或者护眼工具等干扰屏幕的软件.关掉,再安装即可
- php 不重新编译增加openssl扩展
安装openssl和开发包 yum install openssl openssl-devel 跳转到PHP源码下的openssl cd /usr/local/src/php-5.5.27/ext/o ...