Tutis's blog

By Tutis, history, 5 years ago, In English

I was solving this problem and after I solved it by myself I decided to implement editorial's solution. Strangely my first submission was really slow 55982450, but after changing the order of dimensions it got 5x faster 55999256.

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

»
5 years ago, # |
  Vote: I like it +32 Vote: I do not like it

There was a blog before where that happened before, and the reason was because of cache. When storing a multi-dimensional array in a more cache-friendly order, you will get better performance.

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

    I've noticed this specifically for sparse tables. Having st[j][i] store the 2^j-th ancestor of i is an order of magnitude faster than st[i][j].

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

      Nice one, I've never thought about this before. It makes sense. I will consider this next time I implement sparse table.