题解:AT_abc478_c [ABC478C] Sort Subarray

· · 题解

A simple problem.

Since N and K are below 2\times 10^5, we may need an O(N) or O(N\log N) solution.

Consider sorting the interval [L,R], we need to make sure that:

  1. For all i(2\le i < L), A_i \ge A_{i-1}.
  2. For all i(R < i < N), A_i \le A_{i+1}.

By spending O(N) time initializing 1 and 2, and using segment tree or ST table, we can solve this problem in O(N \log N) time, O(N\log N) space. This problem can also be solved in O(N) time.