Sanat_HUIHUIHUI's blog

By Sanat_HUIHUIHUI, history, 18 months ago, In English

Hey, I recently participated in ABC308, I thought my approach for solving G was different compared to one in the editorial and what most people used, So I decided to share it (Although I was only able to solve it few mins after the contest ended)

If you want to refer to my code https://atcoder.jp/contests/abc308/submissions/43162433

I basically maintained a segment tree, where the topmost node maintained the answer for the whole array. and to support it I had two nodes who took care of values whose 29th bit is off and 29 th bit is on respectively.

and similarly, these nodes were supported by two nodes, who took care of the numbers that were the same from i to 30th bit and that differed in (i-1)th bit.

I continued it down all the way to the 0th bit.

first clarification, such Segment tree could have O(10^9) nodes and to avoid it I used a dynamic segment tree (aka sparse segment tree to achieve a Space complexity of Q*30)

Key observation for a node and its two child nodes we do not need to take care of the values that occur due to xor of values that are in different nodes since it would have i^th bit on, which would be worse compared to xor any two values within one of the child node because it would have i^th bit off and that is better obviously.

But we sometimes have to consider the xor value due to cross terms, precisely when a node has 2 elements and they differ in (i-1)th bit but are same all the up. since there is only one element in each child node, so answer for each child node is infinity but the answer for the parent node would be xor of 2 values.

Also when a node has >= 3 elements it is guaranteed that one the child node has less than infinity as the answer, due to the pigeonhole hole principle.

then adding and removing any element was easy just regular segment tree stuff.

basically, that's it.

Full text and comments »

  • Vote: I like it
  • +15
  • Vote: I do not like it