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)

use std::collections::VecDeque;
use std::io::{stdin, Read};
use std::str::FromStr;

// https://qiita.com/tubo28/items/e6076e9040da57368845
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
fn main() {
    let n = read::<usize>();
    let k = read::<usize>();
    let pp = (0..n).map(|_| read::<usize>()).collect::<Vec<_>>();
    let end = n - k + 1;

    let mut deq: VecDeque<usize> = VecDeque::new();
    let mut deq_max: VecDeque<usize> = VecDeque::new();

    for i in 0..k {
        push(&mut deq, &pp, i);
        push_max(&mut deq_max, &pp, i);
    }

    let i = 0;
    let min = pop(&mut deq, &pp, i);
    let max = pop_max(&mut deq_max, &pp, i);
    let mut prev_min = min;
    let mut prev_max = max;

    let mut ans = 1;
    for i in 1..end {
        push(&mut deq, &pp, i + k - 1);
        push_max(&mut deq_max, &pp, i + k - 1);
        let min = pop(&mut deq, &pp, i);
        let max = pop_max(&mut deq_max, &pp, i);

        let is_duplicated = prev_min < min && prev_max < max;

        /*
        let is_duplicated = pp[i - 1] == prev_min
            && pp[i - 1] < pp[i]
            && pp[i - 1 + k - 1] == prev_max
            && pp[i - 1 + k - 1] < pp[i + k - 1];
            */
        if !is_duplicated {
            ans += 1;
        }

        prev_min = min;
        prev_max = max;
    }

    let mut already_sorted = 0;
    let mut prev = pp[0];
    let mut count = 1;

    for i in 1..n {
        if prev < pp[i] {
            count += 1;
            if count == k {
                already_sorted += 1;
            }
        } else {
            count = 1;
        }
        prev = pp[i];
    }

    let ans = if already_sorted > 0 {
        ans - already_sorted + 1
    } else {
        ans
    };
    println!("{}", ans);

    fn push(deq: &mut VecDeque<usize>, aa: &Vec<usize>, i: usize) {
        while let Some(&x) = deq.back() {
            if aa[x] > aa[i] {
                deq.pop_back();
            } else {
                break;
            }
        }
        deq.push_back(i)
    }

    fn pop(deq: &mut VecDeque<usize>, aa: &Vec<usize>, i: usize) -> usize {
        while let Some(&x) = deq.front() {
            if x >= i {
                return aa[x];
            } else {
                deq.pop_front();
            }
        }
        panic!("program error");
    }
    fn push_max(deq: &mut VecDeque<usize>, aa: &Vec<usize>, i: usize) {
        while let Some(&x) = deq.back() {
            if aa[x] < aa[i] {
                deq.pop_back();
            } else {
                break;
            }
        }
        deq.push_back(i)
    }

    fn pop_max(deq: &mut VecDeque<usize>, aa: &Vec<usize>, i: usize) -> usize {
        while let Some(&x) = deq.front() {
            if x >= i {
                return aa[x];
            } else {
                deq.pop_front();
            }
        }
        panic!("program error");
    }
}