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

Автор ajit, история, 4 часа назад, По-английски

Hi,

While solving problem H of the recent Codeforces Div3 round (link), I came across the following issue (after hours of debugging):

In my solution, I had to clear up the segment tree after every test case, so I kept track of all the elements I interacted with in an STL container. The issue is that if I use std::set for keeping track of changed elements in the segment tree, I have no issues, but if I use std::vector, I get a weird runtime error on test 4. Here are my two solutions:

  • Accepted solution (using std::set variable called changed_elements in segtree structure) link
  • Runtime error solution (using std::vector variable called changed_elements in segtree structure) link

Can anyone tell me what's causing this runtime error?

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

»
4 часа назад, # |
Rev. 2   Проголосовать: нравится +9 Проголосовать: не нравится

You are iterating over the changed_elements vector and calling set function which pushes into this vector. push_back for vector invalidates iterators, so it's no wonder you are getting a runtime error. std::set insert doesn't invalidate iterators, so that's why there is no runtime error.

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

    Oops, I got your point. Thanks !