You are not logged in. Please login at www.codechef.com to post your questions!

×

PSHTRG - Editorial

10
4

PROBLEM LINK:

Div1, Div2
Practice

Author: Ivan Fekete
Tester: Triveni Mahatha
Editorialist: Adarsh Kumar

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. 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
Tester's solution

This question is marked "community wiki".

asked 05 Mar, 18:21

adkroxx's gravatar image

7★adkroxx
306719
accept rate: 7%

edited 13 Mar, 15:04

admin's gravatar image

0★admin ♦♦
19.6k349497539

4

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.

(12 Mar, 15:54) gdisastery14★
3

very nice editorial @adkroxx

(12 Mar, 18:07) droy05284★

12

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.

link

answered 12 Mar, 18:07

ista2000's gravatar image

4★ista2000 ♦
2.4k621
accept rate: 20%

1

Wow such a different idea . Never thought of this.

(12 Mar, 20:18) sdssudhu6★
4

I knew about this solution too. But for me something else was simpler, hence I decided to describe that solution. But anyway good job. :)

(12 Mar, 23:26) adkroxx7★
1

Ah, yes the intended solution uses something different too. Its a cross between a segtree and a merge sort tree. Thanks for describing the intended one. :D

(14 Mar, 22:30) ista2000 ♦4★

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

link

answered 12 Mar, 18:59

nilmadhab1's gravatar image

5★nilmadhab1
212
accept rate: 0%

I am not getting how this 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 .

link

answered 12 Mar, 23:06

saurabh18213's gravatar image

5★saurabh18213
102
accept rate: 0%

edited 13 Mar, 11:31

The only reason I can think of -
Weak test cases (lengths of required(valid) triangle were very close to r) i.e. number of queries giving required triangle in less number of iterations >> number of queries giving required triangle in more number of iterations

(14 Mar, 05:05) shawnbam_965★

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

link

answered 12 Mar, 16:28

saurabh18213's gravatar image

5★saurabh18213
102
accept rate: 0%

Having a priority queue at each node of the segment tree would be easier I guess, my solution . Also $K = 45$ worked fine.

link

answered 12 Mar, 16:46

bhishma's gravatar image

4★bhishma
2977
accept rate: 11%

Solutions are not visible :(

link

answered 12 Mar, 17:15

b123eginner's gravatar image

3★b123eginner
111
accept rate: 0%

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

link

answered 12 Mar, 19:08

praveenkumar12's gravatar image

5★praveenkumar12
3009
accept rate: 9%

Can I get a link to nice tutorial on Segment tree ? Btw I passes this question using sqrt decomposition. https://www.codechef.com/viewsolution/17743850 With little optimisation.

link

answered 12 Mar, 19:25

aryanc403's gravatar image

5★aryanc403
2.2k515
accept rate: 11%

https://www.codechef.com/viewsolution/17834749 @kailashnath199 Thank you Man. Now, i am able to do this question using segment tree. Thank You.

(14 Mar, 22:35) aryanc4035★

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.. 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++; }

link

answered 12 Mar, 21:26

l_returns's gravatar image

4★l_returns
1.3k19
accept rate: 26%

edited 12 Mar, 21:27

I got AC with full 100 points using Square root decomposition, should have got WA, but test cases were weak. Slution

link

answered 13 Mar, 03:56

rj25's gravatar image

4★rj25
2095
accept rate: 0%

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

link

answered 13 Mar, 11:18

akibkhan12345's gravatar image

1★akibkhan12345
211
accept rate: 0%

Anybody tell me why its showing WA for this solution:solution

link

answered 13 Mar, 11:43

doramon's gravatar image

1★doramon
865
accept rate: 5%

edited 13 Mar, 12:38

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

link

answered 13 Mar, 15:25

tarunnpvs's gravatar image

3★tarunnpvs
1
accept rate: 0%

Editorial just correctly explained that if there are more than 50 nos in the range l to r then as if a plus b is less than c for all (c>b>a) then c should be greater than Fibonacci nos series having elements a b c etc. So c would exceed your upper limit

(13 Mar, 16:31) namanjain0074★

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

link

answered 15 Mar, 00:35

harrypotter0's gravatar image

4★harrypotter0
318110
accept rate: 1%

edited 15 Mar, 01:33

harrypotter0 For mathematical calculations ,refer to this

link

answered 15 Mar, 20:11

namanjain007's gravatar image

4★namanjain007
112
accept rate: 0%

Can anyone help me. Why this code is giving TLE?

link

answered 17 Mar, 11:15

soumen2252's gravatar image

1★soumen2252
1
accept rate: 0%

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

link

answered 17 Mar, 23:51

gyanendra371's gravatar image

3★gyanendra371
2936
accept rate: 33%

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 Thanks

link

answered 26 Mar, 22:31

jha_gaurav98's gravatar image

4★jha_gaurav98
1
accept rate: 0%

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

link

answered 14 May, 00:55

pavitra_ag's gravatar image

4★pavitra_ag
696
accept rate: 6%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,182
×2,536
×1,703
×265
×144
×68
×64

question asked: 05 Mar, 18:21

question was seen: 7,381 times

last updated: 14 May, 00:55