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?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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?
Name |
---|
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.net/contest/665/problem/E.