博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
阅读量:5043 次
发布时间:2019-06-12

本文共 3724 字,大约阅读时间需要 12 分钟。

题目链接:

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
#include
#include
#include
#include
#define lson (o<<1)#define rson ((o<<1)|1)#define M l+(r-l)/2#define X first#define Y second#define mkp make_pairusing namespace std;const int maxn = 1e6 + 10;const int maxq = 1e6 + 10;typedef int LL;LL sumv[maxn << 2],sumv2[maxn<<2];map
> 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;}

转载于:https://www.cnblogs.com/fenice/p/5743145.html

你可能感兴趣的文章
美国专利
查看>>
【JavaScript】Write和Writeln的区别
查看>>
百度编辑器图片在线流量返回url改动
查看>>
我对你的期望有点过了
查看>>
微信小程序wx:key以及wx:key=" *this"详解:
查看>>
下拉框比较符
查看>>
2.2.5 因子的使用
查看>>
css选择器
查看>>
photoplus
查看>>
Python 拓展之推导式
查看>>
[Leetcode] DP-- 474. Ones and Zeroes
查看>>
80X86寄存器详解<转载>
查看>>
c# aop讲解
查看>>
iterable与iterator
查看>>
返回顶部(动画)
查看>>
webpack+react+antd 单页面应用实例
查看>>
Confluence 6 SQL Server 数据库驱动修改
查看>>
Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决
查看>>
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>