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};
trait Monoid {
    fn unit() -> Self;
    fn add(lhs: Self, rhs: Self) -> Self;
}

struct SegTree<T: Monoid + Clone> {
    dat: Vec<T>,
    n: usize,
}

impl<T: Monoid + Copy> SegTree<T> {
    pub fn new(n: usize) -> Self {
        let dat = vec![T::unit(); n << 1];
        SegTree { dat: dat, n: n }
    }
    pub fn update(&mut self, k: usize, v: T) {
        let mut k = k + self.n;
        self.dat[k] = v;
        while {
            k >>= 1;
            k > 0
        } {
            self.dat[k] = T::add(self.dat[k << 1], self.dat[k << 1 | 1]);
        }
    }
    pub fn get(&self, a: usize, b: usize) -> T {
        let mut a = a + self.n;
        let mut b = b + self.n;
        let mut va = T::unit();
        let mut vb = T::unit();
        while a < b {
            if a & 1 != 0 {
                va = T::add(va, self.dat[a]);
                a += 1;
            }
            if b & 1 != 0 {
                vb = T::add(self.dat[b - 1], vb);
            }
            a >>= 1;
            b >>= 1;
        }
        T::add(va, vb)
    }
}

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();
    };
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct Min(usize);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct Max(usize);
impl Monoid for Min {
    fn unit() -> Self {
        Min(0)
    }
    fn add(lhs: Self, rhs: Self) -> Self {
        Min(std::cmp::min(lhs.0, rhs.0))
    }
}
impl Monoid for Max {
    fn unit() -> Self {
        Max(0)
    }
    fn add(lhs: Self, rhs: Self) -> Self {
        Max(std::cmp::max(lhs.0, rhs.0))
    }
}

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 maxseg = SegTree::<Max>::new(n);
    let mut minseg = SegTree::<Min>::new(n);
    for i in 0..n {
        maxseg.update(i, Max(p[i]));
        minseg.update(i, Min(p[i]));
    }
    let mut ret = n - k + 1;
    let mut count = 1;
    for i in 1..(k - 1) {
        if p[i - 1] < p[i] {
            count += 1;
        } else {
            count = 1;
        }
    }
    let mut id = false;
    for l in 0..(n - k + 1) {
        let r = l + k;
        if p[r - 2] < p[r - 1] {
            count += 1;
        } else {
            count = 1;
        }
        if count >= k {
            if id {
                ret -= 1;
                continue;
            } else {
                id = true;
            }
        }
        if r >= n {
            continue;
        }
        if minseg.get(l, r + 1).0 == p[l] && maxseg.get(l, r + 1).0 == p[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);
    }
}