Editorial for DMOPC '18 Contest 5 P6 - An XOR Problem


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: r3mark

For the first subtask, a few initial observations are needed. Note that if xi=xj for some i and j, then nodes i and j may be connected at no cost, even after updates. So multiple nodes with the same value may be discarded and there will be at most 2K important nodes. Another observation is that there are only 2K residues modulo 2K, so we can precompute the answer for each of these residues and then answer each update in O(1). With these in mind, a straightforward implementation of Kruskal's can pass in time.

Time Complexity: O(N+Q+K23K)

For the next subtask, the Kruskal's must be optimized to subquadratic time. Split the nodes into two sets: those with the largest bit, and those without. Note that because the edges are sorted by weight, the edges between nodes in the same set will always be considered before the edges between the two sets. To combine the two sets, only one edge is necessary (since each set is already connected) and this edge can be found by building a trie from one set and checking every node in the other set against the trie.

Time Complexity: O(QNK2)

Note that the Kruskal's in the solution to the second subtask implicitly formed a binary tree. For the third subtask, we will focus on this structure. Consider the subtrees with height J from the bottom of the complete binary tree. Note that if 32 divides c, then for J=5, these subtrees will move but they themselves will not change. Then we can precompute the total weight within each of these subtrees, as well as the minimum weight between every two of these subtrees. Then for each update, we only need to apply the Kruskal's to the first KJ levels and use the precomputed values. Finally, since 32 divides c, there are only 2KJ important residues mod 2K.

Time Complexity: O(J22J+J22KJ+K222(KJ))

For the final subtask, we build on this idea of unchanging subtrees of height J. To force this condition in subtask 3, we can construct a sequence of c's which has most c's divisible by 2J. For example, when K=4, one such sequence of c's would be c=0,22,22,22,1,22,22,22,1,22,22,22,1,22,22,22 which covers all residues mod 24. However, even after constructing this sequence, we cannot simply apply the exact same idea as subtask 3. This is because we need to do the precomputing between every pair of subtrees for each time c is not divisible by 2J, which is 2J times. This would lead to a time complexity of O(J22K) for precomputing alone.

To deal with this, an optimization is necessary for the precomputation. Consider subtrees of height P in these subtrees of height J. There are only 22P possible subtrees. The minimum edge weight between every two of these subtrees can be precomputed and this only needs to be done once. Then, these values are used in precomputing the values of subtrees of height J, and then the solution from subtask 3 can be used.

Time Complexity: O(P222P+1+J222J+J222KP+K222KJ)

In the model solution, we choose P=3,J=7.


Comments

There are no comments at the moment.