hackerbhaiya's blog

By hackerbhaiya, history, 4 years ago, In English

Yesterday, I was trying a problem. I got two different verdict on the apporx same code.
Solution 1 (Accepted) : https://codeforces.net/contest/271/submission/115150455
Solution 2 (Runtime Error) : https://codeforces.net/contest/271/submission/115150580

See line 83 of solution 1. There i have assigned pre[1][2] to some value which is out of bound. But still the code was accepted, it should give Runtime Error as given in the solution 2. In solution 2, I changed long long to int. I submitted another code in which i removed pre[1][2] assignment and then it was accepted.
Solution 3 : https://codeforces.net/contest/271/submission/115150618

Someone please tell why didn't I get RTE in my solution 1 as pre[1][2] is clearly out of bound?
Thanks in advance!

  • Vote: I like it
  • -2
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +13 Vote: I do not like it

You very lucky. You overwrote some random value in memory, some random variable that is unused or was overwritten later or maybe you wrote a value to padding. Either way you didn't corrupt any data by coincidence.

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

    But why it gave RTE in solution 2? Everything is same as in solution 1, i just changed long long to int in solution 2.

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

      Well, int takes 4 bytes in memory, long long takes 8 bytes in memory. So vector<int>(2) allocates (at least in theory) 8 bytes and vector<long long>(2) allocates 16 bytes, so maybe they used different allocation buckets. Or maybe the size was padded to 16 bytes so there were 8 bytes of unused padding in int case and no padding in long long case.