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

Автор rofi93, история, 6 лет назад, По-английски

I was trying to solve this problem using AVL tree, but getting RE. Can't find out what I'm doing wrong. Can someone help me ??

Solution — https://ideone.com/l7a0jg

UPDATE

Got AC. The bug was I was accessing directly root->subtree_size without checking if the root is NOT NULL.

Here's my AVL tree implementation, pastebin and ideone

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

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

think about treaps in this problem

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by rofi93 (previous revision, new revision, compare).

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

This problem has a really nice and easy offline solution using BIT.

You can solve it online (as you did using your own AVL, congrats!) or using an Ordered Set from C++ (not the set from STL)

»
6 лет назад, # |
Rev. 12   Проголосовать: нравится +1 Проголосовать: не нравится

The following is a slight update to your AVL tree solution. All the functions dealing with the structure node have been updated and included as member functions and friend member functions of the class AVL_tree. Algorithms in all functions are essentially the same, except for the function find_k_smallest which has been rewritten as a recursive function.

The 209 lines of code between lines 14 and 222 in this update should be equivalent to your 311 lines of code between lines 102 and 412 in ideone. The only difference is 102 lines reduction (about 33%) in the code size, and probably a slightly more readable code.

Finally, note that it is simple to add to this problem a command that prints the k-th largest key in the set using the function find_k_smallest. The answer is

root->find_k_smallest( root->size + 1 - k )
  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    Thanks. :D

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      With pleasure.

      Hope that more object-oriented programming features are utilized in your C++ code to enhance its performance as well as its readability, maintainability, and re-usability.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        How does OOP improve performance?

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        For CP, I don't prefer OOP actually. :P

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          But if I were you I should use a simpler binary search tree in Competitive Programming. I think treaps are easier to code correctly than AVLs. In treaps, rotations are not necessary

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
            Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

            Yes. Treaps are easier and AVL actually can't be implemented in an onsite contest, only online may be if you already have the code.