# SCALSUM - Editorial

Hi,
I have a doubt regd the above problem. As mentioned in the third paragraph in the explanation section:
“In each block choose the layer with the least number of vertices. Select every possible pair from this layer to pre-process. Let’s observe that if the block has K vertices then in selected layer there could be at most K/sqrt(N) vertices”.

If I consider a binary tree with N = 31 vertices (please find the image link below), then
block size = sqrt(N) = sqrt(31) = 5.56 ~ 5
maximum number of vertices in a selected layer in a block = K/sqrt(N) = 31/5.56 = 5.568 ~ 5

This means that, all the levels come into a single block which has all 5 layers.
Maximum number of vertices in any selected layer in a block should not exceed 5.
But maximum number of vertices as per the diagram is 16 (in the last level)

Strangely map does work…
Anybody can explain why manually computing upto depths less than = 1000
and memoizing for greater depths passes the 60 pts subtask?

submission - https://www.codechef.com/viewsolution/39670764

I implemented the same logic in Python and I am getting AC in SubTask-1 but in SubTask 2 and 3 I am getting RE and TLE.

Yeah, turns out unordered map doesn’t work well for many insertions and lookups (high constant factor) . I used gp_hash_table as @new_coder_12 did in his submission.

I tried the exact same approach described in the video editorial. Still, it is giving TLE.Kindly check which part of the code is causing the problem?

https://www.codechef.com/viewsolution/39671745

Which library should I include in my code to use this??

why modulo 2**32 is not computed in the editorials.

I think this one

``````#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
``````

look at the reply by @new_coder_12 for the ac code

``````#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
int operator()(int x) const { return x ^ RANDOM; }
};

gp_hash_table<ll, ui, chash> record;
``````

Implement your own hash function for AC…read the codeforces blog given by @l_returns

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??
submission>> https://www.codechef.com/viewsolution/39677764

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