# PSHTRG [Unofficial Editorial] (Pishty and Triangles) MARCH LONG

Anybody plz tell me why i am getting TLE for this solution : link text

Heyy… Can anyone provide the editorial for the problem Chef And GCD queries(GCDCNT). I tried to solve the problem and my complexity for each query was around O(2^6 log(n)) but still it gave TLE. Here is the link to my soln.

Nice Editorial. For similar problems, check out yandex problem D(the last round ie (waitt… last or the second last… i forgot XD)). Also, Polish informatics Olympiad’s problem : triangles and triangles2

1 Like

My solution: CodeChef: Practical coding for everyone

I have used square root decomposition. Each segment is sorted using TreeSet for logarithmic time complexity.Each query in √n log n time using heap.

2 Likes

thanks, @harshmmodi Now I understand the reason behind the value of K = 45 or 50 in the official editorial.

My solution

I used iterative segment tree as described here. Very efficient and easy to implement/debug/personalise.

1 Like

@doramon u r getting wrong because of the size of temporary array which is b[60] set the size to 90 because u can have 45 +45 from the fibonacci series
But suprisingly the last test case is timing out

Happy to hear that it helped

@doramon change all those 30’s to 50’s , 29’s to 49’s and 60’s to 100’s and run… it may work then

Yes, you are completely correct that the temp array is not required. I included it just so that its easier for others to understand. (But, should’ve mentioned that it’s not necessary :’) )

And as far as that 40th number is concerned, I think there’s some confusion. 40th number is 102334155 which is less than 1e9.

Oh… Miscounted digits…

I guess i miscounted digits of 40th fib number

TCs didnt include a failing test case, because my soln passed with 40.

In every node of the tree, you are storing all the elements in its span. This is giving TLE. Store only those elements which form the largest valid triangle and those greater than these sides. You may read the Approach for building the segment tree written above

Happy to hear that

Can you please explain your solution. What are you storing in bucket of n^(1/2) and how are u handling the queries.

For query, I have used a PriorityQueue (Heap). In this heap, I have inserted the max value from each bucket (and their bucket index). Complexity: √(N).log N

Now, the tricky part. I have 'poll’ed the maximum value from the heap and inserted the next largest value in that bucket

Suppose the heap contains [9,5,4]. 9 is polled. The next largest value from the bucket containing the ‘9’ is added to the heap, say 3. So, the heap becomes [5,4,3]. Again 5 is polled and so on…

The last tricky part: handling multiple occurrence of the same value.
For this, TreeMap is used all over instead of TreeSet

Each bucket is just a frequency record, containing the frequency of each value in that segment.

TreeMap is used for that purpose.

thank you sir for the editorial please keep posting such awesome contents for us to learn

I need some reputation points… Please upvote this comment /\

Official editorial is out… and here’s my solution which breaks the range in blocks of 60 and solves the problem.

can u explain the get function in your solution?

can u please explain the approach?