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

Автор Laggy, история, 7 лет назад, По-английски

 Problem: 467C - George and Job So as you can see this is so cool because the bug doesn't actually have anything to do with the solving function. It happens somewhere when building the prefix sum. The current code doesn't pass TC#5 while after removing the green part it goes up to TC#55. How? I wish I knew. Maybe you guys have some thoughts ^^

Kudos to Bassel for playing with it.

Теги bug, op
  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

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

The quantity m[i - 1] is undefined when i=0. The ternary operator ensures that this undefined value is not used by checking if i is nonzero.

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

    that's not what he's saying, he's saying that without the ternary operator it passes more cases.

    The current code doesn't pass TC#5 while after removing the green part it goes up to TC#55

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

I think the TLE is occurring because you are using memset(mem, -1, sizeof(mem)). Using memset will set the byte of each one to -1, so you will not end up with -1 in each cell, but something like 10000000100000001000000010000000 instead. That is why you are getting TLE, because it's recalculating each time.

UPD: that, and I think that your algorithm is N^3 = TLE.

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

This is pretty neat. The code without the green parts computes m[-1] = nonsense, m[0] = nonsense + a[0], m[1] = nonsense + a[0] + a[1], etc., and then solve uses this for prefix sums -- a[i] + ... + a[j] = m[j] - m[i-1] (the random value coming from m[-1] cancels out). Essentially it implements prefix sums without branching and with an array of only size n.

It's wildly undefined behavior, of course, and it would not be surprising if a compiler noticed that and e.g. optimized away certain loop iterations. (It would also crash if you compiled your program with ASan and UBSan, which I highly recommend.) Simplest fix would be to make m be of size n+1 and move each entry up one step, presumably setting m[0] = 0.