A simple problem.
Since N and K are below 2×105, we may need an O(N) or O(NlogN) solution.
Consider sorting the interval [L,R], we need to make sure that:
- For all i(2≤i<L), Ai≥Ai−1.
- For all i(R<i<N), Ai≤Ai+1.
- i=LmaxRAi mustn’t be bigger than AR+1. (You may consider AN+1 infinity.)
- i=LminRAi mustn’t be smaller than AL−1.
By spending O(N) time initializing 1 and 2, and using segment tree or ST table, we can solve this problem in O(NlogN) time, O(NlogN) space. This problem can also be solved in O(N) time.