How to solve problem B in day 1
# | User | Rating |
---|---|---|
1 | tourist | 3690 |
2 | jiangly | 3647 |
3 | Benq | 3581 |
4 | orzdevinwang | 3570 |
5 | Geothermal | 3569 |
5 | cnnfls_csy | 3569 |
7 | Radewoosh | 3509 |
8 | ecnerwala | 3486 |
9 | jqdai0815 | 3474 |
10 | gyh20 | 3447 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 173 |
2 | awoo | 164 |
3 | adamant | 163 |
4 | TheScrasse | 160 |
5 | nor | 157 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 152 |
8 | Petr | 146 |
8 | orz | 146 |
10 | pajenegod | 145 |
Name |
---|
It would be better if you provided some link to the problem, or just described the problem yourself, anyway if someone needs the problem here you can find pictures of the statements. (sorry I couldn't find anything better).
solution complexity:
O(nlog^2n)
Firstly let's notice that the maximum weight that he needs to swap is the inversion with the maximum sum in the subarray
[l,r]
. So let's consider that our indexes of the books that give maximum answer areansl
andansr
. Now we can see that number at indexansl
is the maximum in the subarray[l,ansr-1]
, otherwise we could pick the maximum and get a bigger answer.We need to make a merge-sort tree, and at each node we need to store the answer for it's subtree. So if we get a query for range
[l,r]
we should break that subarray to the nodes of our MS tree. Now there are two variants1.
ansl
andansr
are in the same node. In this case we can just take the maximum answer for our nodes in MS tree.2.
ansl
andansr
are in different nodes. For every node we should know the maximum number in nodes that are located lefter than our current node, let's call itmx
. So if theansr
is located in our current nodeansl
will be the index ofmx
. We can getansl
by BS at our current node.P.S sorry for my poor English.