Skip to content

P15313 向未来说你好

Step 1

Let's first consider the reduced case:

We want to count the number of ways to split a sequence \(a\), such that each element in the sequence must be shorter or equal to the length of its containing segment.

Let's consider \(f[i]\) to be the number of ways to split the first \([1 \dots n]\). Notice that making the sequence longer wouldn't effect the previous included elements. Then the transitions is: $\(f[i] = \sum_{\substack{0 \le j < i \\ \max(a[j+1\dots i]) \le i-j}}{f[j]}\)$

We can solve this with CDQ D&C. Suppose we have split point \(m\), then the max condition becomes: $$ \begin{cases} i - j \ge \max(a[j+1 \dots m]) \ i - j \ge \max(a[m+1 \dots i]) \end{cases} $$ We can rearrange: $$ \begin{cases} i \ge \max(a[j+1 \dots m]) + j \ i - \max(a[m+1 \dots i]) \ge j \end{cases} $$This is in partial order form: $\((i, i-\max(a[m+1 \dots i])) \ge (\max(a[j+1 \dots m], j))\)$ We essentially need to query this partial order every time for each \(i\). That gives us \(\mathcal{O}(n \log^2 n)\). We can also do this for the suffix.

Step 2

We consider how we can add back the original modifications. The modification can only add more configurations and is only going to effect the direct intervals containing it. Reducing it in the middle is going to change a invalid interval into a valid one iff the value changed is the largest and after changing the second maximum is valid. By this constraint, we can simply find all intervals that satisfy this condition, and accumulate the answer on the interval maximum.

The condition after we put it into CDQ: If maximum value is in \([j+1,m]\): $\(\max(a[j+1 \dots m]) \ge i - j \ge \max(a[m+1, i])\)$ Otherwise we flip the whole thing around.

This is also a partial order problem, can the solution is similar to step 1.