https://www.codechef.com/problems/PSHTRG Can you please guide me how i answer for the second query in this question?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
https://www.codechef.com/problems/PSHTRG Can you please guide me how i answer for the second query in this question?
Название |
---|
Disclaimer: I haven't actually tried submitting this.
First, you should know about the triangle inequality.
Clearly, testing all triples is too slow, so we need a faster way. First, can you solve the problem for a single range in $$$O(n\log{n})$$$?
Sort the numbers.
If we ignore the constraint from the triangle inequality, what three numbers should we pick? If these happen to satisfy the constraint, we are done. Otherwise, one of the three can be ignored from now on. Repeat.
Notice we don't need to repeat many times, since the numbers decrease rapidly. (The maximum halves every other iteration).
Back to the original problem:
Suppose we have a data structure that gets the largest numbers in a range in $$$O(\log{n})$$$ each. Can you solve the original problem now?
How can you use a standard max segment tree to make this data structure?
You can ignore numbers by temporarily setting them to $$$-\infty$$$. ($$$0$$$ would work for this problem.)
This should lead to an $$$O(n\log{n}\log{(\max{A_i})})$$$ solution.
Lemma: If you have the sorted sequence of $$$a[l,r]$$$. If there is an answer then it will be contiguous three elements in the sorted sequence.
Start with retrieving elements from $$$[l,r]$$$ in decreasing order. If $$$1^{st}, 2^{nd}, 3^{rd}$$$ elements doesn't satisfy triangle inequality then we know $$$3^{rd}$$$ element is $$$<1^{st}/2$$$. So we just have to retrieve $$$O(lg(A_{MAX}))$$$ elements per query and we will find the answer among them.
If the question was you are given an array on n integers and if we have to select the sides forming maximum perimeter. Then our procedure would be to sort the array in decreasing order then check for triangle inequality until we find one and just break out of the loop, right? is this what you said. but i don't get this line ""So we just have to retrieve O(lg(AMAX)) elements per query and we will find the answer among them."" won't iterating give time limit?
You won't sort the sequence for every query since it will lead to tle, but retrieve the maximum $$$O(lg(A_{MAX}))$$$ elements using segment tree.