Qualified's blog

By Qualified, history, 3 years ago, In English

I was solving this problem and I got AC. While I was solving the problem, I had some cerr's and I commented them out when submitting. After getting AC, I wanted to experiment if uncommenting those cerr's would have a difference in runtime. Lo and behold! The difference was huge. Take a look at these two submissions. 100116835 is the AC and 100116299 is the TLE. The AC runs in 62 ms and the TLE is over 1000 ms. Now I have questions. Does Codeforces use the standard error stream? Why does this affect my runtime? All the comments are print statements and even if they were outputted to standard output stream, this wouldn't cause TLE. Why, why, why?

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

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

i think it was for this reason

trav(i, hld) { //cerr << i << ' '; //}

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

    100120172. I commented out the trav(i, hld) { cerr << i << ' '; }. But it is still TLE. That's not the problem.

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

I'm not sure what's surprising about this. cerr is a stream that exists during grading but isn't used to determine your verdict. You can just think about it as cout but not looked at by the grader. And when you flush an out stream 3e5 times (you cerr endl every time in your two main loops), I wouldn't be too surprised with a TLE.

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

    But it ain't endl it is "\n".

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

      Oh oops. Maybe I should've read macros more carefully.

      Anyways, cerr is flushed whenever you use the << operator, as unitbuf is set. You can probably google for more information.