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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, k) = (v[0], v[1]);
    let p = read_vec::<i64>();

    if n == k {
        println!("1");
        return;
    }

    let spans_sorted = (1..n)
        .map(|i| if p[i] > p[i - 1] { 1 } else { 0 })
        .collect::<Vec<_>>();

    let mut sum = vec![0; n];
    for i in 0..n - 1 {
        sum[i + 1] += sum[i] + spans_sorted[i];
    }

    let mut ordered = vec![false; n - k + 1];
    // debug!(sum);
    let mut ans = 0;
    for i in 0..n + 1 - k {
        ordered[i] = sum[i + k - 1] - sum[i] == k - 1;
        if !ordered[i] {
            ans += 1;
        }
    }
    // debug!(ordered);

    if ordered.iter().any(|&x| x) {
        ans += 1;
    }

    let s_mins = slide_minimum(&p, k + 1);
    let s_maxs = slide_maximum(&p, k + 1);

    for i in 0..ordered.len() - 1 {
        if !ordered[i] && !ordered[i + 1] {
            if s_mins[i] == p[i] && s_maxs[i] == p[i + k] {
                ans -= 1;
            }
        }
    }

    println!("{}", ans);
}

fn slide_minimum(a: &Vec<i64>, k: usize) -> Vec<i64> {
    assert!(k <= a.len());
    let mut s = 0;
    let mut t = 0;
    let mut deq = vec![0; a.len()];
    let mut ans = vec![0; a.len() - k + 1];
    for i in 0..a.len() {
        while s < t && a[deq[t - 1]] >= a[i] {
            t -= 1;
        }
        deq[t] = i;
        t += 1;

        if i + 1 >= k {
            ans[i + 1 - k] = a[deq[s]];
            if deq[s] == i + 1 - k {
                s += 1;
            }
        }
    }
    ans
}

fn slide_maximum(a: &Vec<i64>, k: usize) -> Vec<i64> {
    assert!(k <= a.len());
    let mut s = 0;
    let mut t = 0;
    let mut deq = vec![0; a.len()];
    let mut ans = vec![0; a.len() - k + 1];
    for i in 0..a.len() {
        while s < t && a[deq[t - 1]] <= a[i] {
            t -= 1;
        }
        deq[t] = i;
        t += 1;

        if i + 1 >= k {
            ans[i + 1 - k] = a[deq[s]];
            if deq[s] == i + 1 - k {
                s += 1;
            }
        }
    }
    ans
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}