Xor
xor_1
Computes the sum of all consecutive subsequence sums in a given sequence.
Solution
First all prefix sums are calculated, then for all k (the number of bits in binary), the contribution at the kth bit of the binary sum of all consecutive subsequences is calculated separately, and finally the contribution of all bits is summed together to give the desired answer.
For calculating the contribution at one bit, a tree array is used. There are two tree arrays which are used to record the number of occurrences of the prefix sum for the current bit 0 or 1 and the number of occurrences of the prefix sum for the current bit 1.
Specifically, for the current position i processed to
- If the kth bit of the binary for that position is 1, it calculates the contribution of the iso-or sum of the prefix sum to the current position by querying the tree array, while adding one to the number of occurrences of that prefix sum in bit1.
- If the binary $k$th bit at that position is 0, it calculates the contribution of the iso-or sum of the prefix and to the current position by querying the tree array, while adding one to the number of occurrences of that prefix and in bit0.
Finally, it multiplies the contribution of each bit by the corresponding weight value 2^k and heterosyncs the contributions of all the bits together as the final answer.
Code
1 |
|
xor_2
Problem
There is a sequence of integers of length n.
We can divide this sequence into m consecutive intervals arbitrarily, and let the xor sum of the numbers in each interval be ,
Find the smallest value of or or or
Solution
For each bit i, count the number of intervals whose XOR sum has a 0 bit in position i using an array vis, which is initialized to 0.
If there are at least m such intervals, mark the positions of the array a that have a 1 bit in position i as visited by setting the corresponding values in vis to 1.
Otherwise, set the i-th bit in the answer variable ans to 1.
1 |
|