CC's

Back

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.

分析#

被大哥们秒切了…都没来得及想💔

利用了异或的性质。

想找到一个区间内出现奇数次的数的异或和非常简单,只要维护一个前缀和就可以了。那么知道了奇数次的数字,就能从所有数字的异或中求到补——出现偶数次的数的异或。

值得一写的是求某区间内不同元素相关信息的一个思路。之前有好几道题都是这么干的(一个钥匙的题,一个书的题)。即维护元素的下一次出现位置,将左区间排序后,使用数据结构查询右端点。当左端点越过了某数后,就将其下一次出现的位置加入到结构中。整体为O(nlgn)O(n\lg n)

如果题目的范围小一点,可以莫队。

代码#

没上快读T了发。

[CF703D] Mishka and Interesting Sum
https://astro-pure.js.org/blog/cf703d-mishka-and-interesting-sum
Author Cheng Chen
Published at 2020年4月10日
Comment seems to stuck. Try to refresh?✨