SCALSUM - Editorial

they have used unsigned int which has the range 0 to 2^32…so acts like mod

What if tree structure is like, the first root and then its (3*10^5 - 1) child? Then in that case complexity will shoot to O(N^2)!

1 Like

I have to include this line only?? In my code??

i have included this line but still it giving tle for last two cases can you tell why??

can you please explain it in detail by giving a small example? I haven’t understand it clearly.

Range of unsigned int is 0 to 4,294,967,295 and 2^32 is 4,294,967,296. If at any point your ans becomes 4,294,967,296 the expected output is ans%4,294,967,296 which would be 0 and since unsigned int cant store 4,294,967,296 it would overflow and store it as 0 and your code would continue to add to this value …which is as good as using mod in this sum.

1 Like

I don’t know this kind of approach before. Thanks for explaining.

In that case, the first node will be selected for preprocessing. So, every query in the second level can be answered in 4 steps.

1 Like

Sorry cannot find the issue…i usually code in java and tried c++ only for this sum.
Maybe he can help @bennyjoseph.

Can you share some good resource for sqrt decompositions on tree??

It’s for minimum amount of vertexes in any blocks dont exceed k/sqrt(n)
Like realize it in this way you have 5 spaces and you have to put 20 packets in these spaces then then maximum value of minimum packets in any spaces don’t exceed 20/5 as puuting 4 in each essentially if you remove it from one next one become greater and minimum decrease more
can you tell why my code is giving tle in last cases i have done exactly like editoriol changed hash function can you check the issue??

@codemaster7s. You have added the submit link, not your solution link.
this is the link

@codemaster7s I checked your code. You did according to the editorial. Try the Iterative method for precomputation and answering queries.

Same code in C++ giving TLE but accepted in JAVA…
Is it because of the extra time we get in java or is it because of bad hashing of unordered_map in C++ ??
C++ code:
JAVA code:

1 Like

I have done precomputation iteratively …In which part you are saying to change?? Can you specify… It will be helpfull

Anyone who is getting tle (try using n maps and in those , use only sqrtn maps to store the pair values , which decreases time complexity a little bit) , Although it gave me (60 pts)…
This is my code , Anyone help me where i can optimise this code a bit more

Thanks, mate. Was struggling for the past few days. Things got clearer now. I’m very thankful to you. :smile: :smile:

Try using vectors instead of map.