Problem:
Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

10 4
2 0 1 3 7 5 4 6 8 9

Sample Output 3

6

Solution in Ruby (2.7.1) (judge verdict: Accepted)

n, k = gets.split.map(&:to_i)
pp = gets.split.map{|e| e = e.to_i; [e, e, true]}

class SegTree
    def update(k); @d[k] = @op.call(@d[2 * k], @d[2 * k + 1]); end

    def initialize(v, e, &op)
        @n = v.size
        @log = (@n - 1).bit_length
        @size = 1 << @log
        @op = op

        @d = Array.new(2 * @size, e) # vector

        @n.times{|i| @d[@size + i] = v[i]}
        (@size - 1).downto(1) {|i| update(i)}
    end

    def set(pos, x)
        pos += @size
        @d[pos] = x
        1.upto(@log) {|i| update(pos >> i)}
    end

    def get(pos); @d[pos + @size]; end

    def prod(l, r)
        sml = smr = @d[0]
        l += @size
        r += @size

        while l < r
            (sml = @op.call(sml, @d[l]); l += 1) if l[0] == 1
            (r -= 1; smr = @op.call(@d[r], smr)) if r[0] == 1
            l >>= 1
            r >>= 1
        end
        @op.call(sml, smr)
    end

    def all_prod; @d[1]; end
end

st = SegTree.new(pp, -1) do |c1, c2|
    if c1 == -1
        c2
    elsif c2 == -1
        c1
    elsif c1[2] && c2[2] && c1[1] < c2[0]
        [c1[0], c2[1], true]
    else
        [[c1[0], c2[0]].min, [c1[1], c2[1]].max, false]
    end
end

ans = 1
f = st.prod(0, k)[2]
(n - k).times do |i|
    a = st.prod(i, i+k)
    b = st.prod(i+1, i+k+1)
    next if a[0] == pp[i][0] && b[1] == pp[i+k][0]
    next if f && b[2]
    f |= b[2]
    ans += 1
end
p ans