I was solving this problem. As no editorial is provided for this solution. Can anybody help me to figure out how to calculate XOR between each pairs of integers (i, j) such that 1 ≤ i ≤ j ≤ N?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
I was solving this problem. As no editorial is provided for this solution. Can anybody help me to figure out how to calculate XOR between each pairs of integers (i, j) such that 1 ≤ i ≤ j ≤ N?
Название |
---|
This can be done using segment tree.
You can build segment tree just like we do in case of minimun or maximum function. We can do this because the XOR operation is associative. This can be done in O(n) time.
Querying is same as the standard case with change in binary operation as XOR. This will take O(logn) time.
In case you haven't read about segment tree, you can refer this awesome post.
It's not a good idea. You don't need a segment tree if you have no modification queries and function that you want to calculate is invertible. In case of calcing sum, multiplication by prime modulo, xor, etc. you should just calculate prefix sums.
UPD. Because of the previous comment I thought that problem is about calculating xor in range, but it is not so.
But this problem is not asking to calculate xor in range(Did I misunderstood something?)
We can use prefix xors + trie + dp to solve this.
we can break l to r as less than x .And calculate l to r = (less than r) minus (less than l).
Similar to problem 3 here.
PS: I would be happy to know how to solve by segment trees or anyother way
Also a problem similar to the problem that are you speaking about was on CF recently: http://codeforces.me/contest/665/problem/E.