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: Wrong Answer)

use std::io::{stdin, Read};

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let (n, k): (usize, usize) = parse!(it, 2);
    let p: Vec<usize> = parse!(it, n);
    let mut ids = std::collections::HashSet::new();
    let mut ret = n - k + 1;
    let mut count = 1;
    for i in 1..n {
        if p[i - 1] < p[i] {
            count += 1;
        } else {
            count = 1;
        }
        if count >= k {
            ids.insert(i);
        }
    }
    if ids.len() > 0 {
        ret -= ids.len() - 1;
    }
    let mut minq = std::collections::VecDeque::new();
    let mut maxq = std::collections::VecDeque::new();
    let pushmin = |x, i, minq: &mut std::collections::VecDeque<_>| {
        while minq.len() > 0 && minq[minq.len() - 1] > (x, i) {
            minq.pop_back().unwrap();
        }
        minq.push_back((x, i));
        if i > k && minq[0].1 <= i - k {
            minq.pop_front().unwrap();
        }
        minq[0].1
    };
    let pushmax = |x, i, maxq: &mut std::collections::VecDeque<_>| {
        while maxq.len() > 0 && maxq[maxq.len() - 1] < (x, i) {
            maxq.pop_back().unwrap();
        }
        maxq.push_back((x, i));
        if i > k && maxq[0].1 <= i - k {
            maxq.pop_front().unwrap();
        }
        maxq[0].1
    };
    for i in 0..k {
        pushmin(p[i], i, &mut minq);
        pushmax(p[i], i, &mut maxq);
    }

    for l in 0..(n - k) {
        let r = l + k;
        if ids.contains(&r) {
            continue;
        }
        let mini = pushmin(p[r], r, &mut minq);
        let maxi = pushmax(p[r], r, &mut maxq);
        if mini == l && maxi == r {
            ret -= 1;
        }
    }
    println!("{}", ret);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}