Блог пользователя SYCu

Автор SYCu, история, 19 месяцев назад, По-английски

Maintains a data structure that supports the following operations:

  • inserts an element $$$x$$$ into the set $$$S$$$
  • deletes an element $$$x$$$ from the set $$$S$$$
  • queries the maximum value of xor for each pair of elements in the set ( $$$\max\limits_{i,j\in S}i\oplus j$$$ )

Can this problem be solved within $$$O(n^2)$$$ better time complexity?

  • Проголосовать: нравится
  • +30
  • Проголосовать: не нравится

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

Trie again.

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Here is a problem the same as yours: 706D - Vasiliy's Multiset

It's solvable by Trie (I'm new to Trie as well xD)

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

I think it's possible just maintain the multiset of max xor by trie for each number and handling insert delete carefull

»
19 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +38 Проголосовать: не нравится

I think you could solve it offline. for each element in the set mark the time it goes in the set and out of the set now for each element you have interval, you could do same as dynamic connectivity, using a segment tree you could traverse the tree and know what elements where added in this segment and add them to the trie, every time you add an element you take max value of this element and another element in the trie, and each leaf in the tree is a query you can know the max then, and when you leave the node, make the value back to what it was before entering the node. this solution should be $$$O(Q*log(maxVal)*log(Q))$$$

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

If it's offline, then trie + segtree over versions in $$$n \space log n \space log X$$$

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If you necessarily want an online approach, you can do sqrt decomposition and rebuild tries after every query, though the complexity is quite atrocious, even if subquadratic.

»
19 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

You can try to speed up this heuretically like this: If there is non-zero numbers with the most significant bit (MSB) both 0 and 1, the solution will obviously have MSB = 1, and you can assume that i's MSB is 0, and j's MSB is 1. (If all the numbers have the same MSB, just ignore this bit and continue to the 2nd MSB.)

Now you can imagine, that you are building your solution with i and j being in different subtrees of your trie. Now you try to optimize 2nd MSB of the solution: you check whether in the subtrees of i and j, continuation of type (0, 1) or (1, 0) is possible. Note that if both types of continuation are present, you must explore all the possibilities (that's why this cannot simply reduce the complexity to $$$O(\log n)$$$). If all continuations of i and j have the same bit, just continue to next MSB.

Note that you most likely want to do this in BFS order and not DFS order (ie. if at some continuation you see i = 0110..., j = 1001..., and the next recursion starts from i = 0001... and j = 1111..., you don't have to continue but exit early).

I don't know how to compute the complexity of that but for random data you should roughly halve the size of search size for each bit. And for very dense data it would seem that there are a lot of pairs of the same type (i.e. (0, 0) or (1, 1) continuations) while of different type (i.e. (0, 1) and (1, 0)) are available, so also skipping a lot of entries.

If you implemented this, let me know how it performs :)

Good luck!

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

You can do it in $$$O(n \cdot \log{n})$$$ offline. you only need rollbacks (in this case you just save maximums along with a trie), and you can add offline deletion.