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 | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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) ...