FJOI2022 重复函数
Step 1
We notice that \(A\) is always a border of \(S\). If we have fixed \(A\), we can greedily take to find it's maximum number of occurrence. Any number of occurrence lower than max is doable.
Step 2
We need to track the next occurrence, we use a DSU. The total number of \(A\)s we need to iterate through is \(O(n \ln n)\). The complexity is \(O(n \ln(n)\, \alpha(n))\).