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::cmp::Ordering;
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")
}

struct SegmentTree<T> {
    vs: Vec<T>,
    ord: Ordering,
    leaf_size: usize,
    default_value: T,
}

impl<T: Clone + Ord> SegmentTree<T> {
    pub fn min(n: usize, init: T) -> SegmentTree<T> {
        let d = init.clone();
        SegmentTree {
            vs: vec![init; n * 2],
            ord: Ordering::Less,
            leaf_size: n,
            default_value: d,
        }
    }

    pub fn max(n: usize, init: T) -> SegmentTree<T> {
        let d = init.clone();
        SegmentTree {
            vs: vec![init; n * 2],
            ord: Ordering::Greater,
            leaf_size: n,
            default_value: d,
        }
    }

    pub fn update(&mut self, leaf_index: usize, x: T) {
        self.upd(leaf_index, x)
    }

    fn upd(&mut self, leaf_index: usize, x: T) {
        let mut i = self.vs.len() - (self.leaf_size - leaf_index);
        self.vs[i] = x;

        while i > 1 {
            i = i / 2; // parent node id
            let (lc, rc) = (&self.vs[i * 2].clone(), &self.vs[i * 2 + 1].clone());
            let up = if lc.cmp(rc) == self.ord { lc } else { rc };
            self.vs[i] = up.clone();
        }
    }

    pub fn query(&self, from: usize, to: usize) -> T {
        self.q(from, to, 1, 0, self.leaf_size)
    }

    fn q(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.default_value.clone();
        } else if a <= l && r <= b {
            return self.vs[k].clone();
        } else {
            let mid = l + ((r - l) / 2);
            let lc = self.q(a, b, 2 * k, l, mid);
            let rc = self.q(a, b, 2 * k + 1, mid, r);
            if lc.cmp(&rc) == self.ord {
                lc
            } else {
                rc
            }
        }
    }
}

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

    let u_max = usize::max_value() as usize;
    let mut size = 1;
    loop {
        if size >= n {
            break;
        } else {
            size *= 2;
        }
    }

    let mut s_min = SegmentTree::min(size, u_max);
    let mut s_max = SegmentTree::max(size, 0);

    for i in 0..n {
        s_min.update(i, pp[i]);
        s_max.update(i, pp[i]);
    }

    let mut prev_min = s_min.query(0, k);
    let mut prev_max = s_max.query(0, k);

    let mut ans: usize = 1;
    let end = n - k + 1;
    for i in 1..end {
        let min = s_min.query(i, i + k);
        let max = s_max.query(i, i + k);

        let same_with_prev = prev_min < min && prev_max < max;
        if !same_with_prev {
            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);
}