Answers to: LCH15JGH - Editorialhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial<h1>PROBLEM LINK:</h1>
<p><a href="http://www.codechef.com/problems/LCH15JGH">Practice</a>
<a href="http://www.codechef.com/LTIME20/problems/LCH15JGH">Contest</a></p>
<p><strong>Author:</strong> <a href="http://www.codechef.com/users/pavel1996">Pavel Sheftelevich</a> <br>
<strong>Tester:</strong> <a href="http://www.codechef.com/users/rubanenko">Roman Rubanenko</a> <br>
<strong>Editorialist:</strong> <a href="http://www.codechef.com/users/pkacprzak">PaweÅ‚ Kacprzak</a> </p>
<h1>DIFFICULTY:</h1>
<p>Medium Hard</p>
<h1>PREREQUISITES:</h1>
<p>Math, [Fenwick tree][11111], Segment tree, Ad-hoc</p>
<h1>PROBLEM:</h1>
<p>There are initially N families of people of a different given size. You have to be able to handle 3 types of queries on the set of these families:</p>
<ol>
<li>Add one family of size X to the set</li>
<li>Remove one family of size X from the set</li>
<li>You will give Y bananas to every family in the set in such a way that for any particular family, all member of it will receive the same number of bananas and this number will be maximum possible i.e if a family has M members then every member will receive Y / M bananas and there will be Y % M bananas left. Your tast is to count the number of all bananas left in this process.</li>
</ol>
<h1>QUICK EXPLANATION:</h1>
<p>Use the fact that x % y = x - (x / y) * y, where x / y is an integer division, so in order to calculate the number of bananas left you can avoid calculating modulo. To handle queries of the first and the second type you can use a [Fenwick tree][11111] or a Segment tree, which can answer two queries: how many total people are currently in families for which Y / M is equal and how many families are there for which Y / M is equal, where M denotes the number of people in one such family. If you are able to answer this question, in order to answer a query of the third type, you can iterate over all possible values of Y / M and accumulate the results.</p>
<h1>EXPLANATION:</h1>
<p>There are multiple very good approaches for this problem and almost all of them depends on the facts given above. I will sketch 3 of them:</p>
<h1>SOLUTIONS BASED ON SEGMENT-TYPE-LIKE DATA STRUCTURE</h1>
<p>As mentioned in quick explanation, you can use a data structure which can handle queries about segments, where a segment [L, R] corrensponds to all families with sizes in a range [L, R]. If you are able to keep track of these ranges, you can easily handle queries of first two types. In order to handle the third query, notice that there are many families of different sizes for which Y / M is equal, where M denotes a size of a family. Based on this fact, and the fact that Y % M = Y - (Y / M) * M, you can avoid calculating modulo on every such family and you can calculate a result for all such familis at once by querying the data structure about the number of people in families in a range [L, R] in which Y / M is equal for every family and querying it also about the number of families in the same range. Based on this two subresults, you can compute the number of bananas left in the process for each of family in [L, R]. If you sum these results over all possible values of Y / M you will get the result for the third query.</p>
<p>The complexity of these solutions are about O(log N * M * sqrt(max(Y)))</p>
<h1>SOLUTIONS BASED ON BLOCKS</h1>
<p>Rather than using a segment-based data structure, you can divide the range of possible family sizes in contant size blocks. You can use similar facts as in the above solution to get the results, but rather than querying for a particular range [L, R] you will divide it into two calculations of result for a range [1, L - 1] and [1, R] and you will subtract first from the second. In order to calculate the result for a given range [1, R] you will sum results for all full blocks which fit into the range and possibly one chunk at the end of the range for which you can calculate the result iterating over all its elements.</p>
<p>Let B be the number of used blocks and S be the size of one such block.</p>
<p>The complexity of these type solutions are about O(M * sqrt(max(Y)) + N * (B + S)) which can be an upgrade over the segment-tree-like data structure.</p>
<h1>AD HOC SOLUTIONS</h1>
<p>You can also keep it really simple and get away with using advanced data structures or blocks. A quick overview is that you can compute the results for families given initially in some way, then handle a portion of up to Q queries (Q is choosen arbitrary to fit the time limits, we saw contestants using Q = 200) by using previously computed results and bruteforcing over previous updates in current portion of queries. After you handle Q queries, you can rebuilt your current results and start with another Q queries.</p>
<p>The complexity of these type solutions depends on the implementation, but if done right with appropriate constants, they can be really fast for this problem.</p>
<h1>AUTHOR'S AND TESTER'S SOLUTIONS:</h1>
<p>Author's solution can be found <a href="http://www.codechef.com/download/Solutions/LTIME20/Setter/LCH15JGH.cpp">here</a>.
Tester's solution can be found <a href="http://www.codechef.com/download/Solutions/LTIME20/Tester/LCH15JGH.java">here</a>. </p>
<h1>RELATED PROBLEMS:</h1>enFri, 30 Jan 2015 17:54:35 +0530Answer by out_of_the_boxhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial/63354<p>My segment tree solution is also giving TLE for the second division. Here is the link- <a href="http://ideone.com/JxDfUG">http://ideone.com/JxDfUG</a> . What can I improve to make it faster?</p>out_of_the_boxFri, 30 Jan 2015 17:54:35 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial/63354Answer by Sandeeprippinghttps://discuss.codechef.com/questions/62667/lch15jgh-editorial/63125<p>I have a doubt regarding the time limit... it is too strict :( .. my segment tree solution did not work and i had to change to B.I.T and even in my solution i had to change some of my variables from long long int to int :\ .. isnt this too much... altough i m quiet new to the programming environment yet i feel that the tie limit was quiet strict...or was there something wrong with my implementation... admin i would like your comments on the same </p>SandeeprippingWed, 28 Jan 2015 01:33:24 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial/63125Comment by pkacprzak on pkacprzak's questionhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial#62843<p><a href="/users/66051/superty">@superty</a>, good point, it's fixed now</p>pkacprzakMon, 26 Jan 2015 01:49:43 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial#62843Comment by vastolorde95 on vastolorde95's answerhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial#62802<p>Never mind, I saw another solution and understood the flow :)</p>vastolorde95Sun, 25 Jan 2015 23:20:49 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial#62802Comment by superty on pkacprzak's questionhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial#62800<p>Please format a bit better, initially I was wondering how Y % M = Y - Y/(M^2).</p>
<p>M*(Y/M) is a lot clearer.</p>supertySun, 25 Jan 2015 23:08:33 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial#62800Answer by vastolorde95https://discuss.codechef.com/questions/62667/lch15jgh-editorial/62788<p>Could you please explain the calc() function from author's solution more clearly? I understood the approach but but I don't understand what the variables 'minst', 'fin' and 'st' mean.
Thanks!</p>vastolorde95Sun, 25 Jan 2015 22:27:45 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial/62788Answer by lord_fhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial/62725<p>The third approach can also be called a sqrt-optimisation/blocks method, but instead of dividing the array into blocks we divide the queries.<br>
I've written it because someone might consider explanation in the editorial as some magic rather than a common approach.</p>lord_fSun, 25 Jan 2015 16:54:22 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial/62725Comment by pkacprzak on noob333's answerhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial#62699<p><a href="/users/13480/noob333">@noob333</a> Thanks a lot, it's corrected now</p>pkacprzakSun, 25 Jan 2015 15:40:05 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial#62699Answer by noob333https://discuss.codechef.com/questions/62667/lch15jgh-editorial/62698<p>I think there is a small typo. It is written as <strong>Y % M = Y - Y / M * Y</strong> but it should be <strong>Y % M = Y - Y / M *M</strong></p>noob333Sun, 25 Jan 2015 15:35:24 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial/62698Comment by sampritipanda on ankitdhall's answerhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial#62685<p>You need to output 0 if there are no families, not the original number of bananas.</p>sampritipandaSun, 25 Jan 2015 15:05:35 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial#62685Comment by pkacprzak on ankitdhall's answerhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial#62684<p><a href="/users/82927/ankitdhall">@ankitdhall</a> Yes, it will be a problem. You have to deal with up to 10^5 queries and imagine that you have 10^5 different families. If you try to iterate over all of them for each query, it will result in TLE easily.</p>pkacprzakSun, 25 Jan 2015 15:04:57 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial#62684Answer by ankitdhallhttps://discuss.codechef.com/questions/62667/lch15jgh-editorial/62673<p>Is there a problem if we use unordered_map to store the number of families with a particular size and iterate over it to answer the queries?
I thought of the above idea while implementing my code. Sadly, it seemed to give me a WA for the first sub-task.
Since the problem statement is clear enough I am unable to find where I went wrong.</p>
<p><a href="http://www.codechef.com/viewsolution/5988423">Code here</a></p>ankitdhallSun, 25 Jan 2015 14:51:34 +0530https://discuss.codechef.com/questions/62667/lch15jgh-editorial/62673