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

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

Please help me to optimise my code i am getting TLE since 6 hours.

problem link- link solution link- link

I tried every possible way to get rid over TLE but nothing works.

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

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

I too got suck on this one when I first tried it due to strict TL.

Btw, just don't use anything for max (like function, macro or stl) just write if() else (), and you will be fine(probably).

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

I didn't read your code but when the tree is a line, HLD maybe get TLE

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

I solved it in another online judge, using HLD. I guess there might be mistake in your code but i couldn't find it.. sorry

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

    Yes i found out the mistake but unable to understand why it's happening :(

    My Accepted Soln- link in 0.33sec

    I just changed the behaviour of my ancestor 2-d array from anc[i][j] to anc[j][i] and it got accepted but the problem is even in both cases it has samecomplexity of O(15*sz) but anc[i][j] is giving TLE and anc[j][i] is giving the answer within time limit.

    Please help me to find out the mistake that i was doing.

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

      Well, IMO, this is just cache localisation. While finding the ancestors, in case of lca we are using anc[j][i] and then immediately anc[j][i+1] which helps cache localisation.

      On the other hand, if you are using anc[i][j] and then anc[i+1][j] there is a gap of around 15 between these and it is unable to use the cache efficiently.

      Generally this doesn't matter much but this question seems to have a very strict TL.