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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.