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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.*;

public class Main {

    static int N;
    static int[] bit = new int[1000010];

    public static void add(int a, int w) {
        for (int i=a;i<=N;i+=i&(-i)) {
            bit[i]+=w;
        }
    }

    public static int sum(int a) {
        int ret = 0;
        for (int i=a;i>0;i-=i&(-i)) {
            ret+=bit[i];
        }
        return ret;
    }

    public static int get(int k) { // 0_indexed find
        ++k;
        int res = 0;
        int nn = 1; while (nn < (int)N) nn *= 2;
        for (int i = nn / 2; i > 0; i /= 2) {
            if (res + i < (int)N && bit[res + i] < k) {
                k = k - bit[res + i];
                res = res + i;
            }
        }
        return res + 1;
    }
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        N = in.nextInt();
        int K = in.nextInt();
        int[] P = new int[N];
        int sorted = 0;
        int cnt = 0;
        int before= -1;
        for (int i=0;i<N;i++) {
            P[i] = in.nextInt();
            if (P[i]>before) {
                cnt++;
                if (cnt==K) sorted++;
            } else {
                cnt=1;
            }
            before = P[i];
        }

        for (int i=0;i<K;i++) {
            add(P[i]+1, 1);
        }

        int ans = N-K+1;
        for (int i=K;i<N;i++) {
            int flag = 0;
            if (get(0)!=P[i-K]+1) flag=1;
            add(P[i-K]+1, -1);
            add(P[i]+1, 1);
            if (get(K-1)!=P[i]+1) flag=1;
            if (flag==0) ans--;

            // out.println(flag);
        // for (int j=0;j<K;j++) {
        //     if (j==K-1) out.println(get(j));
        //     else out.print(get(j)+" ");
        // }
        }
        if (sorted>=2) out.println(ans-sorted+1);
        else out.println(ans);
        // out.println(sorted);

        out.close();
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}