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 Node {
    id: usize,
}

impl Node {
    pub fn new(node_id: usize) -> Node {
        Node { id: node_id }
    }

    pub fn root() -> Node {
        Node { id: 1 }
    }

    pub fn value<'a, T>(&self, t: &'a VecTree<T>) -> &'a T {
        &t.vs[self.id]
    }

    pub fn set<T>(&self, t: &mut VecTree<T>, x: T) {
        t.vs[self.id] = x;
    }

    pub fn parent(&self) -> Option<Node> {
        if self.id > 1 {
            let node_id = self.id / 2;
            let node = Node { id: node_id };
            Some(node)
        } else {
            None
        }
    }

    pub fn left_child(&self) -> Node {
        let node_id = self.id * 2;
        Node { id: node_id }
    }

    pub fn right_child(&self) -> Node {
        let node_id = self.id * 2 + 1;
        Node { id: node_id }
    }

    pub fn min_child<'a, T: Ord>(&self, t: &'a VecTree<T>) -> (Node, &'a T) {
        let (lc, rc) = (self.left_child(), self.right_child());
        let lv = lc.value(t);
        let rv = rc.value(t);
        if lv.cmp(rv) == t.ord {
            (lc, lv)
        } else {
            (rc, rv)
        }
    }
}

struct VecTree<T> {
    vs: Vec<T>,
    ord: Ordering,
}

impl<T: Clone + Ord> VecTree<T> {
    pub fn min(n: usize, init: T) -> VecTree<T> {
        return VecTree {
            vs: vec![init; n],
            ord: Ordering::Less,
        };
    }

    pub fn max(n: usize, init: T) -> VecTree<T> {
        return VecTree {
            vs: vec![init; n],
            ord: Ordering::Greater,
        };
    }

    pub fn root(&self) -> Node {
        Node::root()
    }

    pub fn leaf(&self, leaf_index: usize) -> Node {
        let i = self.vs.len() / 2 + leaf_index;
        Node::new(i)
    }

    pub fn leaf_size(&self) -> usize {
        self.vs.len() / 2
    }
}

struct SegmentTree<T> {
    base: VecTree<T>,
    default_value: T,
}

impl<T: Clone + Ord> SegmentTree<T> {
    pub fn min(leaf_size: usize, init: T) -> SegmentTree<T> {
        SegmentTree {
            base: VecTree::min(leaf_size * 2, init.clone()),
            default_value: init,
        }
    }

    pub fn max(leaf_size: usize, init: T) -> SegmentTree<T> {
        SegmentTree {
            base: VecTree::max(leaf_size * 2, init.clone()),
            default_value: init,
        }
    }

    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 n = self.base.leaf(leaf_index);
        n.set(&mut self.base, x);

        while let Some(p) = n.parent() {
            let v = p.min_child(&self.base).1.clone();
            p.set(&mut self.base, v);
            n = p;
        }
    }

    pub fn query(&self, from: usize, to: usize) -> T {
        let leaf_size = self.base.leaf_size();
        self.q(from, to, self.base.root(), 0, leaf_size)
    }

    fn q(&self, a: usize, b: usize, node: Node, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            self.default_value.clone()
        } else if a <= l && r <= b {
            node.value(&self.base).clone()
        } else {
            let mid = l + ((r - l) / 2);
            let lc = self.q(a, b, node.left_child(), l, mid);
            let rc = self.q(a, b, node.right_child(), mid, r);
            if lc.cmp(&rc) == self.base.ord {
                lc
            } else {
                rc
            }
        }
    }

    pub fn leaf_size(&self) -> usize {
        self.base.vs.len() / 2
    }
}

#[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);
}