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)

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        k: usize,
        v: [usize; n]
    }
    let mut c = 0;
    let mut inc = 1;
    for i in 1..n {
        if v[i] > v[i - 1] {
            inc += 1;
        } else {
            if inc >= k {
                c += 1;
            }
            inc = 1;
        }
    }
    if inc >= k {
        c += 1;
    }
    let mut ans = 1;
    let mut seg_max = SegT::from_vec(&v, 0, |_, y| y, max);
    let mut seg_min = SegT::from_vec(&v, n, |_, y| y, min);
    for i in 0..n - k {
        if v[i] != seg_min.get(i, i + k) || v[i + k] != seg_max.get(i, i + k) {
            ans += 1;
        }
    }
    if c > 1 {
        ans -= c - 1;
    }
    println!("{}", ans);
}

struct SegT<T, F, G> where T: Copy, F: FnMut(T, T) -> T, G: FnMut(T, T) -> T {
    n: usize,
    v: Vec<T>,
    def: T,
    upf: F,
    getf: G
}

impl<T, F, G> SegT<T, F, G> where T: Copy, F: FnMut(T, T) -> T, G: FnMut(T, T) -> T {
    fn new(n: usize, def: T, upf: F, getf: G) -> SegT<T, F, G> {
        let mut s = 1;
        while s < n {
            s *= 2;
        }
        SegT{n: s, v: vec![def; 2 * s - 1], def: def, upf: upf, getf: getf}
    }

    fn from_vec(xs: &Vec<T>, def: T, upf: F, mut getf: G) -> SegT<T, F, G> {
        let l = xs.len();
        let mut s = 1;
        while s < l {
            s *= 2;
        }
        let mut v: Vec<T> = vec![def; 2 * s - 1];
        for i in 0..l {
            v[i + s - 1] = xs[i];
        }
        for i in 2..s + 1 {
            let p = s - i;
            v[p] = getf(v[2 * p + 1], v[2 * p + 2]);
        }
        SegT{n: s, v: v, def: def, upf: upf, getf: getf}
    }

    fn update(&mut self, i: usize, x: T) {
        let n = self.n;
        self.update_rec(i, x, 0, 0, n - 1);
    }

    fn update_rec(&mut self, i: usize, x: T, p: usize, pl: usize, pr: usize) {
        if pl > i || pr < i {
            return;
        } else if p == i + self.n - 1 {
            self.v[p] = (self.upf)(self.v[p], x);
        } else {
            self.update_rec(i, x, 2 * p + 1, pl, (pl + pr - 1) / 2);
            self.update_rec(i, x, 2 * p + 2, (pl + pr + 1) / 2, pr);
            self.v[p] = (self.getf)(self.v[2 * p + 1], self.v[2 * p + 2]);
        }
    }

    fn get(&mut self, l: usize, r: usize) -> T {
        let n = self.n;
        self.get_rec(l, r, 0, 0, n - 1)
    }

    fn get_rec(&mut self, l: usize, r: usize, p: usize, pl: usize, pr: usize) -> T {
        if pl > r || pr < l {
            self.def
        } else if l <= pl && pr <= r {
            self.v[p]
        } else {
            let a = self.get_rec(l, r, 2 * p + 1, pl, (pl + pr - 1) / 2);
            let b = self.get_rec(l, r, 2 * p + 2, (pl + pr + 1) / 2, pr);
            (self.getf)(a, b)
        }
    }
}