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, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}


macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => {};
    ($($xs:expr),*) => {}
    // ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    // ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

use std::collections::BTreeSet;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let k: usize = sc.cin();
    let xs: Vec<i32> = (0..n).map(|_| sc.cin()).collect();

    let mut sorted = vec![0; n];  // i = sorted[j] := (i, j) is sorted.
    for i in 1..n {
        if xs[i - 1] < xs[i] {
            sorted[i] = sorted[i - 1];
        } else {
            sorted[i] = i;
        }
    }

    let mut tree = BTreeSet::new();
    let mut tree_rev = BTreeSet::new();

    for i in 0..k {
        tree.insert(xs[i]);
        tree_rev.insert(-xs[i]);
    }

    let mut any_sorted = false;
    let mut ans = 0;

    for i in 0..n-k+1 {
        trace!(i, i + k - 1);
        let this_is_sorted = sorted[i + k - 1] <= i;
        if this_is_sorted { any_sorted = true; }
        trace!(this_is_sorted);
        if i == 0 {
            if !this_is_sorted {
                ans += 1;
            }
        } else {
            tree.insert(xs[i + k - 1]);
            tree_rev.insert(-xs[i + k - 1]);
            let min = *tree.iter().next().unwrap();
            let max = -*tree_rev.iter().next().unwrap();
            trace!(&tree);
            if !this_is_sorted && (min != xs[i - 1] || max != xs[i + k - 1]) {
                ans += 1;
                trace!(ans);
            }
            tree.remove(&xs[i - 1]);
            tree_rev.remove(&-xs[i - 1]);
        }
    }

    if any_sorted {
        ans += 1;
    }
    put!(ans);

}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}