SYCu's blog

By SYCu, history, 13 months ago, In English

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?

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

| Write comment?
»
13 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

Trie again.

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can you provide a detailed description of your solution? I don't know how to solve it really

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +31 Vote: I do not like it

    How will you use trie for supporting deletion other than probably a potentially working method to do it with an extra $$$\mathcal{O}(\log n)$$$ factor? Inserting is well known enough; deletion sadly not

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it -22 Vote: I do not like it

      Can you not just do node.count = node.left.count + node.right.count. When you remove an element, you go down and subtract 1 from the counts.

      • »
        »
        »
        »
        13 months ago, hide # ^ |
         
        Vote: I like it -8 Vote: I do not like it

        did you even read the post

        • »
          »
          »
          »
          »
          13 months ago, hide # ^ |
           
          Vote: I like it -21 Vote: I do not like it

          Deletion on a trie is well known

          • »
            »
            »
            »
            »
            »
            13 months ago, hide # ^ |
             
            Vote: I like it +3 Vote: I do not like it

            but not maintaining the $$$\max(a_i \oplus a_j)$$$ alongside deletion.

            You absolutely did not read the post did you.

            • »
              »
              »
              »
              »
              »
              »
              13 months ago, hide # ^ |
               
              Vote: I like it -11 Vote: I do not like it

              I read the post. I was just pointing out that you were wrong.

              If it's n^2, just maintain a set and then brute force all pairs for every query.

              • »
                »
                »
                »
                »
                »
                »
                »
                13 months ago, hide # ^ |
                 
                Vote: I like it 0 Vote: I do not like it

                What do you mean I am wrong, your comment hints absolutely nothing towards how to solve the problem. The actually hard part of the problem is maintaining the $$$\max(a_i \oplus a_j)$$$ during deletion, which I implied in "How will you use trie for supporting deletion".

                Fuck off, you aren't really understanding the problem even.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  13 months ago, hide # ^ |
                   
                  Vote: I like it +5 Vote: I do not like it

                  What do you think about dealing with the time intervals [tl,tr] in which each number occurs, and then Divide and conquer a segment tree on the timeline? Like some dynamic graph connectivity problem that can be taken offline

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  13 months ago, hide # ^ |
                   
                  Vote: I like it -11 Vote: I do not like it

                  I believe that is the method mentioned in the article I linked

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  13 months ago, hide # ^ |
                   
                  Vote: I like it +49 Vote: I do not like it

                  you two have great chemistry

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  13 months ago, hide # ^ |
                   
                  Vote: I like it -39 Vote: I do not like it

                  I'm saying if its O(n^2) per query, you can brute force all pairs.

                  If its O(n) per query, it's still possible by maintaining counts and second maxes, sort of like a segment tree beats-like structure on a trie in lognlog(A) per query with n^2 preprocessing.

                  You need to quit being so hostile when someone corrects you.

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it -68 Vote: I do not like it

    try*. learn to spell before commenting next time.

»
13 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

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)

»
13 months ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

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

»
13 months ago, hide # |
Rev. 2  
Vote: I like it +38 Vote: I do not like it

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))$$$

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +9 Vote: I do not like it

    Thank you 111 I never thought of this clever solution but can it be solved online and within a time complexity of $$$O (\log^k n) $$$ ?

»
13 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

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

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Thank you 111 but can it be solved online and within a time complexity of $$$O (\log^k n) $$$ ?

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Thank you 111 but can it be solved online and within a time complexity of $$$O (\log^k n) $$$

»
13 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

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!

  • »
    »
    13 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +8 Vote: I do not like it

    Thank you! Much faster than I expected, when $$$Q=2\times 10^5,V=2^{30}-1$$$ it only cost 1 second(but my data isn't strong enough)

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.