因为已经不能发题解了,所以发讨论区
类似 [Violet]蒲公英
,分块,预处理 block_ans[b1][b2]
表示块 b1
~ b2
的答案。
查询时,设 l 在块 bl,r 在块 br,则答案为以下三部分的最大值:
- block_ans[bl+1][br−1]
- “块 bl 里的剩余部分”与其余部分得到的答案
- “块 br 里的剩余部分”与其余部分得到的答案
其中部分 2,3 用离散化颜色 + vector 存每种颜色对应的位置 + lower_bound 寻找位置
实现,具体见代码
设块的大小为 S,则时间复杂度为 Θ(Sn2+mSlogn)
取 S=mlognn 可得最优复杂度 Θ(nmlogn)
实际取 S=n0.3 左右比较快(考虑常数因素)
加个 O2 可以卡过
代码:https://gitee.com/hkxa/mycode/blob/master/luogu/P5906.cpp