PassingBy's blog

By PassingBy, history, 10 years ago, In Russian

24455084.

It works on my computer and on cpp.sh.

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

»
10 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

It's not recommended to use something like left->get_size() when you know that left can be equal to nullptr. It's undefined behaviour, so crashes on Codeforces.

It works on my computer and on cpp.sh.

cpp.sh uses an old version of GCC (4.9.2). In newer versions, compiler makes some optimizations with this, so the program crashes.

Consider using something like this:

        static size_t get_size(Node* node) {
            return (node == nullptr) ? 0 : (node->size);
        }

        void update() {
            size = get_size(left) + get_size(right) + 1;
        }

I didn't submit it, but in Codeforces' "Custom invocation" it works and gives correct answer :)

  • »
    »
    10 years ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    Thanks. Your solution did the trick

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

      You're welcome! :)

      • »
        »
        »
        »
        10 years ago, hide # ^ |
        ← Rev. 2  
        Vote: I like it 0 Vote: I do not like it

        That's weird but I've got another problem of that kind. Now I get RE2 with C++14 24456589 and WA1 with C++11 24456640. I double-checked my code for any type of calling nullptr's and I'm sure there is nothing about that. And it still works fine on my machine and cpp.sh. Won't you please guess what's wrong this time?

        • »
          »
          »
          »
          »
          10 years ago, hide # ^ |
          ← Rev. 2  
          Vote: I like it 0 Vote: I do not like it

          Strange... I removed volatile and it worked! Doesn't have ideas why.

          Also consider initializing left and right in Treap::Node's contructor.

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

            Well, it didn't fix WA1 with c++11 but now I have RE4 with c++14

            • »
              »
              »
              »
              »
              »
              »
              10 years ago, hide # ^ |
               
              Vote: I like it +1 Vote: I do not like it

              Also consider initializing left and right in Treap::Node's contructor.

              • »
                »
                »
                »
                »
                »
                »
                »
                10 years ago, hide # ^ |
                 
                Vote: I like it +8 Vote: I do not like it

                Thanks a lot, it helped. It is so confusing that UB sometimes works right. I guess I have a lot of learning to do