# | User | Rating |
---|---|---|
1 | tourist | 3856 |
2 | jiangly | 3747 |
3 | orzdevinwang | 3706 |
4 | jqdai0815 | 3682 |
5 | ksun48 | 3591 |
6 | gamegame | 3477 |
7 | Benq | 3468 |
8 | Radewoosh | 3462 |
9 | ecnerwala | 3451 |
10 | heuristica | 3431 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | -is-this-fft- | 162 |
3 | Dominater069 | 160 |
4 | Um_nik | 158 |
5 | atcoder_official | 157 |
6 | Qingyu | 156 |
7 | djm03178 | 152 |
7 | adamant | 152 |
9 | luogu_official | 151 |
10 | awoo | 147 |
Name |
---|
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).
I am still getting TLE :(
link
Can you please optimise my code ?
you dont need separate lca, it's just taking extra time. You may look at my code it's just of 150 lines
I didn't read your code but when the tree is a line, HLD maybe get TLE
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
Some HLD codes can be RE for special cases
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).
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
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.
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.