Reminder: in case of any technical issues, you can use the lightweight website m1.codeforces.com, m2.codeforces.com, m3.codeforces.com. ×

deepak_097's blog

By deepak_097, history, 13 months ago, In English,

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.

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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).

  • »
    »
    13 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I am still getting TLE :(

    link

    Can you please optimise my code ?

    • »
      »
      »
      13 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      you dont need separate lca, it's just taking extra time. You may look at my code it's just of 150 lines

      • »
        »
        »
        »
        13 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I think there is some problems in my code :( One my friend submitted with the same logic and got accepted.

»
13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    13 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Uhm.. I'm sorry, what? HLD on a line tree is just a segment tree. The complexity of query on that tree is . On random trees HLD query complexity is about

    What about TLE on problem. Did you try to debug some edge cases? It seems like your solution is not working when n = 1

    • »
      »
      »
      13 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Some HLD codes can be RE for special cases

  • »
    »
    13 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    HLD assures O(log^2 n) per query (in this problem).

    Especially when tree is a line,( there is only one chain) that is same as querying on sequence. Which is O(log n).

»
13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    13 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

    • »
      »
      »
      7 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.

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

        I AM CONSTANTLY GETING TLE IN QTREE 2 PROBLEM ON SPOJ CAN ANYONE SPECIFY WHY MY CODE IS SLOW implementation detail: i used hld as preprocess then i used segment tree to find lca now to answer query of type 1 DIS U V i used traversing tree using hld method in logn^2 time(mot probably here it is giving tle) to answer query of type 2 again used hld traversal and tried to reach kth node in logn^2 time by finding the segment in which kth node lies… please check it i also checked with spojtoolkit taking an input as big but here its working absolutely fine but i dont know why it is not working on final submission…please look into it; link to problem: https://www.spoj.com/problems/QTREE2/ 1 link to my solution: https://ideone.com/1HCs4A 1