Please help with this problem from October circuit from Hackerearth. In their editorial they just posted solution code. Please provide some idea(I know that Segment tree can be used) but I am not sure how to handle the reverse operation.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Please help with this problem from October circuit from Hackerearth. In their editorial they just posted solution code. Please provide some idea(I know that Segment tree can be used) but I am not sure how to handle the reverse operation.
Name |
---|
This Problem helped me in solving this problem — GSS1
Disclaimer: There may be multiple ways to solve this problem. Below I outline my approach during the contest.
Assuming that you can do basic stuff with segment trees, I suggest reading up this article: Answering max subarray range queries
Try the problem on your own for some time after reading up the article. If you don't make much progress, read up the hints in the order till you get to something new which you didn't think of earlier.
Now coming to the reversing part, consider the 3 intervals (or 2 when either of the side ones converge to 0 length) I1: = [1, L - 1], I2: = [L, R] and I3: = [R + 1, N].
For each interval Ik denote by Prek, Sufk, Sumk, maxSubk the max prefix sum, max suffix sum, total sum and the max subarray sum respectively (for 1 ≤ k ≤ 3). ...
Note that reversing interval I2, only swaps the value of Pre2 and Suf2 for it. ...
Then the maximum subarray sum of the array (after I2 has been reversed) will be the maximum of the following 6 possible sums:
maxSub1
maxSub2
maxSub3
max(0, Suf1) + Sum2 + max(0, Pre3)
max(0, Suf1) + max prefix sum of reversed I2 = max(0, Suf1) + Suf2
max suffix sum of reversed I2 + max(0, Pre3) = Pre2 + max(0, Pre3) ...