One of the most unique and brilliant solution i have every heard, repeated application of sqrt decomposition to get query time O(1).
I would be glad to see ur submission. Thanks for sharing ur approach friend.
PS:if u r willing, i am recieving request for editorial of POLY, but wasn’t able to solve that one. If u r willing to share ur approach with me, that would be really helpful. Be assured, due credit will be given to u. If u urself want to write editorial, that would also be welcome. In case u r busy, no problem mate!!!
Great editorial. But I am having difficulty in understanding square decomposition solution of CSUBQ, what is array found used for in your code ? @taran_1407
@taran_1407 There is lower_bound() function in the algorithms library which uses binary search on an iterator range. Here it’s a map/set method with the same name.
Well, I hope you convey this suggestion to admin. There should be proper testing in these three languages at least. No one should ever get undue advantage just because of a particular language.
Happens, mate. You are not the first one for this, nor you will be the last. I too wasted nearly 12 submissions (WA Segment tree ones) on CSUBQ because of a variable mistake. Just changed variable and got AC.
@taran_1407 bro, it may be my immaturity in knowledge, since I have just gone through the MMI concept on GeeksforGeeks, but I can’t understand how will it give the desired answer in O(1)?
I might be wrong, but i guess this solution is going to get TLE because of “precalculated F[l+1] * F[l+2] * … * F[r-1]” as you mentioned. Bounds of this problem were so tight even to refuse O(QlogN) solutions to pass even after many optimisations.
And In case we try pre-computation like F[L+1][R-1] is going to give us MLE/TLE or both.
I solved CSUBQ(after contest) using 2 Fenwick trees cuz i am no expert with segment tree and Fenwick seemed easy to implement with need of just handling the merge or break segment conditions with if else blocks.
So what i did is, maintained 2 maps for storing the index of consecutive blocks and the length of array from that index(array-1 is 1 when element is <= R ; array-2 is 1 when element is < L) .
I used Fenwick to store sum of segments from 1 to any index, thus on querying i’d do something like let a = --map.upperbound®, b= --map.upperbound(l)(i.e. decrement both upperbounds),
then ans = a - b;(mind me for the syntax her) after that, decremented the unnecessary part of segments
For a 1-query used lowerbound on both maps, accordingly merged or break the segments.
For a 2-query just used lowerbound on both maps, accordingly decremented the unnecessary part of segments and printed the answer .
My code would seem lengthy because i didn’t make any function for the merge or break part and cuz of the repeated if else blocks(sorry).