OneClickAC's blog

By OneClickAC, history, 8 years ago, In English

Can anyone tell me , why I am getting runtime error on test 1 itself ?? Problem link->http://codeforces.net/problemset/problem/387/B Code link->http://pastebin.com/N3JKSVPj

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Run it through valgrind and you'll see a bunch of invalid read errors. You need to advance the iterator before you erase the element from the set. You can use the return value from set.erase() like this.

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

    http://codeforces.net/submissions/rakcode1998.

    First of all , Thanks for helping .. Secondly, Above is the link of my recent submission as per your advice . I have corrected this as you said yuizy but i am not getting anything apart from runtime error.. And added to this IS , that my code is perfectly running on my PC..

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

      You still have the same issue — you're advancing the iterator after erasing the corresponding element from the set. The reason it works on your PC is that it's undefined behavior, and would most likely cause a runtime error on your PC as well with bigger inputs. It seems you didn't perhaps understand my code, so here's a link with a few alternative solutions to the issue as one of them might be easier to understand: http://stackoverflow.com/questions/2874441/deleting-elements-from-stl-set-while-iterating.