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 3.5 secondsmemory limit per test 256 megabytes
#### 问题描述
> Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!
>
> Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.
>
> Each query is processed in the following way:
>
> Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment.
> Integers, presented in array segment [l, r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down.
> XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where — operator of exclusive bitwise OR.
> Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.
#### 输入
> The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array.
>
> The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements.
>
> The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries.
>
> Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.
#### 输出
> Print m non-negative integers — the answers for the queries in the order they appear in the input.
#### 样例
> **sample input**
> 7
> 1 2 1 3 3 2 3
> 5
> 4 7
> 4 5
> 1 3
> 1 7
> 1 5
>
> **sample output**
> 0
> 3
> 1
> 3
> 2
题意
求一个区间内所有出现次数为偶数次的数的异或和。
题解
如果题目叫我们求区间内所有出现次数为奇数次的数的异或和,那就好办了,直接把区间所有的数都异或起来就可以了。
现在我们把问题转换一下:我们先求出所有的数的异或和sum1,然后再求出区间内所有不同的数的异或和sum2,那么ans=sum1^sum2.
对于sum1可以O(n)跑前缀异或和,也可以跑线段树。而对于sum2我们可以用离线的线段树来处理(搓这里)。
代码
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cstring>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M l+(r-l)/2
#define X first
#define Y second
#define mkp make_pair
using namespace std;
const int maxn = 1e6 + 10;
const int maxq = 1e6 + 10;
typedef int LL;
LL sumv[maxn << 2],sumv2[maxn<<2];
map<int, pair<int,int> > mp;
int arr[maxn];
LL ans[maxq];
struct Node {
int l, r, id;
bool operator <(const Node& tmp) const {
return r < tmp.r;
}
} nds[maxq];
int ql, qr;
LL _sumv,_sumv2;
void query(int o, int l, int r) {
if (ql <= l&&r <= qr) {
_sumv ^= sumv[o];
_sumv2 ^= sumv2[o];
}
else {
if (ql <= M) query(lson, l, M);
if (qr>M) query(rson, M + 1, r);
}
}
int _p, _v;
void update(int o, int l, int r,int type) {
if (l == r) {
if(type==1) sumv[o] = _v;
else sumv2[o] = _v;
}
else {
if (_p <= M) update(lson, l, M,type);
else update(rson, M + 1, r,type);
if(type==1) sumv[o] = sumv[lson] ^ sumv[rson];
else sumv2[o] = sumv2[lson] ^ sumv2[rson];
}
}
int n;
void init() {
memset(sumv, 0, sizeof(sumv));
mp.clear();
}
int main() {
init();
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
_p = i; _v = arr[i]; update(1, 1, n, -1);
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%d%d", &nds[i].l, &nds[i].r);
nds[i].id = i;
}
sort(nds, nds + q);
int pos = 1;
for (int i = 0; i < q; i++) {
int l = nds[i].l, r = nds[i].r, id = nds[i].id;
while (pos <= r) {
if (mp.count(arr[pos])) {
_p = mp[arr[pos]].X, _v = 0;
update(1, 1, n,1);
}
mp[arr[pos]].X = pos;
_p = pos, _v = arr[pos];
update(1, 1, n,1);
pos++;
}
ql = l, qr = r;
_sumv = 0,_sumv2=0;
query(1, 1, n);
ans[id] = _sumv^_sumv2;
}
for (int i = 0; i < q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树的更多相关文章
- 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 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 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 #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树
B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...
随机推荐
- Ninject在mvc中的简单配置
前言 Ninject是一款开源的轻量级的依赖注入插件.从接触ioc以来,一直都是使用这个,感觉用起来还是不错的,配置起来也很方便简单.在mvc中更是基本傻瓜式的配置. 开发前的准备 新建一个mvc3项 ...
- PHP和AJAX笔记汇总
AJAX简介AJAX = Asynchronous JavaScript And XML(异步 JavaScript 及 XML)AJAX 是 Asynchronous JavaScript And ...
- [leetcode]_Climbing Stairs
我敢保证这道题是在今早蹲厕所的时候突然冒出的解法.第一次接触DP题,我好伟大啊啊啊~ 题目:一个N阶的梯子,一次能够走1步或者2步,问有多少种走法. 解法:原始DP问题. 思路: 1.if N == ...
- 5.21_启程日本二面_1 vs 1
昨天上午刚群面完,晚上7点左右就接到了电话.面试官就两位菇凉,看来她们也是很辛苦.今天下午3点 1 vs 1,在一家咖啡店里,主要是询问下去日本的意愿是否足够强烈.太老实,这里实话实说,也没有表现出非 ...
- 重拾C,一天一点点_12
连续两天没写了,今天继续! sizeof 对象 或 sizeof (类型名) 返回一个整型值,等于指定对象或类型占用的存储空间字节数.(返回值是无符号整型值,其类型为size_t,在头文件<st ...
- php的swoole扩展中onclose和onconnect接口不被调用的问题
在用swoole扩展写在线聊天例子的时候遇到一个问题,查了不少资料,现在记录于此. 通过看swoole_server的接口文档,回调注册接口on中倒是有明确的注释: * swoole_server-& ...
- apache2反向代理node.js应用
在之前记录的随笔中,只是介绍了怎么在apache2中使用proxy模块,后来查到了一些资料,可以通过下面网址查看配置块的详细参数信息 http://man.ddvip.com/soft/apache2 ...
- WPF 超链接方式
<TextBlock> <Hyperlink Name="hc" Click="hc_Click" Navi ...
- 关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录)
关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录) 作者:王可利(Star·星星) HTML中 相对定位:position:relative; 绝对定位:position ...
- Linux ls -l内容详解
ls -l是列出当前目录下所有文件信息 以下是实例: 具体的文字描述如下: 第1字段: 文件属性字段文件属性字段总共有10个字母组成,第一个字母表示文件类型,如果这个字母是一个减号”-”,则说明该文 ...