# PSHTRG - Editorial

**URL:** <https://discuss.codechef.com/t/pshtrg-editorial/18196>\
**Category:** editorial\
**Tags:** editorial, march18, medium, pshtrg, segment-tree, sorting, triangle\
**Created:** [March 5, 2018, 6:21pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196 "2018-03-05T18:21:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![adkroxx](https://discuss.codechef.com/user_avatar/discuss.codechef.com/adkroxx/32/9164_2.png) [@adkroxx](https://discuss.codechef.com/u/adkroxx)\
**Post date:** [March 5, 2018, 6:21pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/1 "2018-03-05T18:21:39Z")

</div>

## PROBLEM LINK:

[Div1](http://www.codechef.com/MARCH18A/problems/PSHTRG), [Div2](http://www.codechef.com/MARCH18B/problems/PSHTRG)

[Practice](http://www.codechef.com/problems/PSHTRG)

**Author:** [Ivan Fekete](http://www.codechef.com/users/fekete)

**Tester:** [Triveni Mahatha](http://www.codechef.com/users/triveni)

**Editorialist:** [Adarsh Kumar](http://www.codechef.com/users/adkroxx)

## DIFFICULTY:

Medium

## PREREQUISITES:

Segment tree

## PROBLEM:

You are given an array with N elements. You need to support two types of operations on this array. First one is point update. For the second operation, you will be given query range l,r and you need to find the maximum possible perimeter of a triangle with non-zero area whose sides are A\_x, A\_y, A\_z, where l \le x \< y \< z \le r. If no triangle can be formed, you just need to output 0.

## QUICK EXPLANATION:

Build a segment tree where each node stores largest K elements from the interval. Value of K for given constraint will be around 50. Update function on the tree can now be supported in O(KlogN).

**Claim:** For a given range, triangle with maximum perimeter can be formed using largest K elements from that interval. Answer will be 0, only for the case, when there will be less than K elements in the interval and no triangle formation is possible.

You can query for largest K elements in O(KlogN). Finding the triangle with maximum perimeter afterward is relatively easy and procedure for the same can be found in detailed explanation.

## EXPLANATION:

Let’s try to solve a simpler version first.

### Solve a simple version first:
In this version, you are given an array of $N$ integers and you need to find the maximum possible perimeter of a triangle.

To solve this problem, first, we sort our array in non-ascending order. Next, we iterate over each A[i] (where 1 \le i \< N-1 ) and fix the longest side, c = A[i] . To satisfy the condition a+b\>c and maximize a+b+c , we must choose b = A[i+1] and a = A[i+2] . a+b+c will be the largest perimeter for minimal i satisfying the condition a+b\>c. The answer will be 0 if no such i exist.

**Claim:** If N\>K, answer always exist and can be found using largest K elements, i.e. i \le K . Here K = O(log\_{\phi}(\sqrt{5}.MAX)), where \phi = \frac{1+\sqrt{5}}{2} and MAX is the maximum value which can be present in the array.

**Proof:** Lets say you keep adding elements in an array in increasing order such that no triangle formation is possible at any instant. Say there are r elements currently in the array, when you add (r+1)^{th} element, it must not satisy triangle inequality with any elements already present in the array. Hence we can write,

$A[r+1] \ge A[r]+A[r-1]$

If we keep on adding elements in this manner, we will end up on [fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number). Fibonacci numbers grows very fast and soon they will exceed the maximum value that can be present in the array. If we do not exceed the maximum value that can be present in the array, triangle can definitely be formed using K elements. You can compute the exact value of K, but we will just use approximation here for our purpose:

$fib(K) \> MAX$

\Rightarrow \frac{\phi^K}{\sqrt{5}} \> MAX (Using approximation)

\Rightarrow K \> log\_{\phi}(\sqrt{5}.MAX)

**Conclusion:** For our original purpose, we will only need largest K elements from the query range. We need a Data Structure that can support point update and can report largest K elements from any query range in logarithmic time complexity or similar. For current constraints K = 50.

### Using segment tree for our purpose

We can perform our required operation with the help of segment tree. In each node of segment tree, you need to store largest K elements from that interval. If size of interval is less than K then store all elements from that interval.

Merge function for two nodes can be implemented in a manner similar to merge sort algorithm. Merging of two nodes can be done in O(K). For each update/query, you will need to change/visit atmax logN nodes. Hence the time complexity for each update/query will be O(KlogN). For more implementation details, you can have a look at attached solutions.

## Time Complexity:

O(Q.K.logN)

## AUTHOR’S AND TESTER’S SOLUTIONS

[Setter’s solution](http://www.codechef.com/download/Solutions/MARCH18/Setter/PSHTRG.cpp)

[Tester’s solution](http://www.codechef.com/download/Solutions/MARCH18/Tester/PSHTRG.cpp)

---

<div class="post-metadata">

**Author:** ![saurabh18213](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@saurabh18213](https://discuss.codechef.com/u/saurabh18213)\
**Post date:** [March 12, 2018, 4:28pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/2 "2018-03-12T16:28:37Z")

</div>

@kayak use long long for calculating answer as the answer can be the sum of three maximum integers, which will result in overflow.

---

<div class="post-metadata">

**Author:** ![bhishma](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@bhishma](https://discuss.codechef.com/u/bhishma)\
**Post date:** [March 12, 2018, 4:46pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/3 "2018-03-12T16:46:16Z")

</div>

Having a priority queue at each node of the segment tree would be easier I guess, my [solution](https://www.codechef.com/viewsolution/17634490) . Also K = 45 worked fine.

---

<div class="post-metadata">

**Author:** ![b123eginner](https://discuss.codechef.com/user_avatar/discuss.codechef.com/b123eginner/32/21_2.png) [@b123eginner](https://discuss.codechef.com/u/b123eginner)\
**Post date:** [March 12, 2018, 5:15pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/4 "2018-03-12T17:15:23Z")

</div>

Solutions are not visible ☹

---

<div class="post-metadata">

**Author:** ![ista2000](https://discuss.codechef.com/user_avatar/discuss.codechef.com/ista2000/32/85671_2.png) [@ista2000](https://discuss.codechef.com/u/ista2000)\
**Post date:** [March 12, 2018, 6:07pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/5 "2018-03-12T18:07:04Z")

</div>

Wouldn’t a much simpler solution be to just make a segtree which returns the index of the max element in a range? After extracting the max index, we can save it and then point update it to 0 and then do the max query again to get the second largest. This is O(KlogN) query and O(logN) update but is much simpler.

---

<div class="post-metadata">

**Author:** ![nilmadhab1](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@nilmadhab1](https://discuss.codechef.com/u/nilmadhab1)\
**Post date:** [March 12, 2018, 6:59pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/6 "2018-03-12T18:59:15Z")

</div>

I have used square root decomposition for the problem.  
[https://www.codechef.com/viewsolution/17760397](https://www.codechef.com/viewsolution/17760397)

---

<div class="post-metadata">

**Author:** ![praveenkumar12](https://discuss.codechef.com/user_avatar/discuss.codechef.com/praveenkumar12/32/24644_2.png) [@praveenkumar12](https://discuss.codechef.com/u/praveenkumar12)\
**Post date:** [March 12, 2018, 7:08pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/7 "2018-03-12T19:08:47Z")

</div>

@kayak 2^31 is less than 3\*10^9.

---

<div class="post-metadata">

**Author:** ![l\_returns](https://discuss.codechef.com/user_avatar/discuss.codechef.com/l_returns/32/233293_2.png) [@l\_returns](https://discuss.codechef.com/u/l_returns)\
**Post date:** [March 12, 2018, 9:26pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/9 "2018-03-12T21:26:53Z")

</div>

No need to store 50 points… while merging two arrays from child nodes… we can break adding points when we get a triangle and by fibonacci it is clear that no. of points would be less than 50 always… so it would be more optimized…  
sample code for merging could be…

```auto

s1=size of v1 , s2 = size of v2....
 while(i<s1 && j<s2)
  {
        if( v[k-1] + v[k-2] > v[k-3]) /// breaking condition
            return ;
        if(v1[i] > v2[j])
        v.push_back(v1[i++]);
        else
        v.push_back(v2[j++]);
        k++;
  }

```

---

<div class="post-metadata">

**Author:** ![saurabh18213](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@saurabh18213](https://discuss.codechef.com/u/saurabh18213)\
**Post date:** [March 12, 2018, 11:06pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/10 "2018-03-12T23:06:34Z")

</div>

I am not getting how [this](https://www.codechef.com/viewsolution/17665748) O(Q\*N) submission of mine is getting 100 points.  
My approach :  
First I created a vector of pair in which i stored every element’s value and index. Then I sorted this vector.  
For first query I used an algorithm similar to one used in insertion sort. And for query 2 I just traversed  
the array from backward and checked the conditions for an element, whether its index is between l and r .

---

<div class="post-metadata">

**Author:** ![rj25](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@rj25](https://discuss.codechef.com/u/rj25)\
**Post date:** [March 13, 2018, 3:56am UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/11 "2018-03-13T03:56:48Z")

</div>

I got AC with full 100 points using Square root decomposition, should have got WA, but test cases were weak. [Slution](https://www.codechef.com/viewsolution/17731209)

---

<div class="post-metadata">

**Author:** ![akibkhan12345](https://discuss.codechef.com/user_avatar/discuss.codechef.com/akibkhan12345/32/21_2.png) [@akibkhan12345](https://discuss.codechef.com/u/akibkhan12345)\
**Post date:** [March 13, 2018, 11:18am UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/12 "2018-03-13T11:18:35Z")

</div>

neither tester nor setter’s solution are opening… showing the error “This XML file does not appear to have any style information associated with it. The document tree is shown below.” fix it @admin

---

<div class="post-metadata">

**Author:** ![doramon](https://discuss.codechef.com/user_avatar/discuss.codechef.com/doramon/32/24644_2.png) [@doramon](https://discuss.codechef.com/u/doramon)\
**Post date:** [March 13, 2018, 11:43am UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/13 "2018-03-13T11:43:21Z")

</div>

Anybody tell me why its showing WA for this solution:[solution](https://www.codechef.com/viewsolution/17822034)

---

<div class="post-metadata">

**Author:** ![tarunnpvs](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@tarunnpvs](https://discuss.codechef.com/u/tarunnpvs)\
**Post date:** [March 13, 2018, 3:25pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/14 "2018-03-13T15:25:28Z")

</div>

Is there any reason to choose k=50? If Yes can some one explain?

---

<div class="post-metadata">

**Author:** ![harrypotter0](https://discuss.codechef.com/user_avatar/discuss.codechef.com/harrypotter0/32/256059_2.png) [@harrypotter0](https://discuss.codechef.com/u/harrypotter0)\
**Post date:** [March 15, 2018, 12:35am UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/15 "2018-03-15T00:35:09Z")

</div>

Please can someone explain to me the math involved behind the limiting the value of K to 50 ?

---

<div class="post-metadata">

**Author:** ![namanjain007](https://discuss.codechef.com/user_avatar/discuss.codechef.com/namanjain007/32/22910_2.png) [@namanjain007](https://discuss.codechef.com/u/namanjain007)\
**Post date:** [March 15, 2018, 8:11pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/16 "2018-03-15T20:11:01Z")

</div>

[harrypotter0](https://discuss.codechef.com/users/183523/harrypotter0)For mathematical calculations ,refer to [this](https://en.wikipedia.org/wiki/Fibonacci_number)

---

<div class="post-metadata">

**Author:** ![soumen2252](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@soumen2252](https://discuss.codechef.com/u/soumen2252)\
**Post date:** [March 17, 2018, 11:15am UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/17 "2018-03-17T11:15:39Z")

</div>

Can anyone help me. Why [this](https://www.codechef.com/viewsolution/17862222) code is giving TLE?

---

<div class="post-metadata">

**Author:** ![gyanendra371](https://discuss.codechef.com/user_avatar/discuss.codechef.com/gyanendra371/32/257356_2.png) [@gyanendra371](https://discuss.codechef.com/u/gyanendra371)\
**Post date:** [March 17, 2018, 11:51pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/18 "2018-03-17T23:51:01Z")

</div>

can anyone just tell me how setter is doing without sorting nodes.

---

<div class="post-metadata">

**Author:** ![jha\_gaurav98](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@jha\_gaurav98](https://discuss.codechef.com/u/jha_gaurav98)\
**Post date:** [March 26, 2018, 10:31pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/19 "2018-03-26T22:31:26Z")

</div>

Please can someone help me in improving my solution. I have understood the concept and written a code but it is giving TLE.The complexity is Q.K.log n where k = 45. [here is my code](https://www.codechef.com/viewsolution/17965123) Thanks

---

<div class="post-metadata">

**Author:** ![pavitra\_ag](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@pavitra\_ag](https://discuss.codechef.com/u/pavitra_ag)\
**Post date:** [May 14, 2018, 12:55am UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/20 "2018-05-14T00:55:11Z")

</div>

@ista2000 can you please explain your approach. I am new to segment trees.  
Thanks in Advance:)

---

<div class="post-metadata">

**Author:** ![gdisastery1](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@gdisastery1](https://discuss.codechef.com/u/gdisastery1)\
**Post date:** [March 12, 2018, 3:54pm UTC](https://discuss.codechef.com/t/pshtrg-editorial/18196/21 "2018-03-12T15:54:47Z")

</div>

You can also simply use Max Segment Tree: Query for the largest element, remove it (by setting the value to -infinity), and repeat it K times. Afterwards you just restore the values. This is also O(K \log n) per query.

[Next page](https://discuss.codechef.com/t/pshtrg-editorial/18196.md?page=2)
