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)

    // https://atcoder.jp/contests/agc038/tasks/agc038_b
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

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

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

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

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

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

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

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

use std::rc::Rc;
type Merger<T> = Rc<Fn(T, T) -> T>;

struct SegmentTree<T> {
    n: usize,
    default: T,
    lazy_default: T,
    bottom_offset: usize,
    data: Vec<T>,
    lazy: Vec<T>,
    merger: Merger<T>,
    lazy_merger: Merger<T>,
}

impl<T: Copy + Eq> SegmentTree<T> {
    fn new(n: usize, initial: T, merger: Merger<T>, lazy_initial: T, lazy_merger: Merger<T>) -> Self {
        let z = (8u32 * std::mem::size_of::<usize>() as u32) - n.leading_zeros();
        let size = (1<<z) as usize;
        SegmentTree {
            n: size*2,
            default: initial,
            lazy_default: lazy_initial,
            bottom_offset: size,
            data: vec![initial; size*2],
            lazy: vec![lazy_initial; size*2],
            merger: merger,
            lazy_merger: lazy_merger
        }
    }

    /// Compute value [i, j).
    fn compute_range(&mut self, i: usize, j: usize) -> T {
        let m = self.bottom_offset;
        self.inner_compute_range(i, j, 1, 0, m)
    }

    fn inner_compute_range(&mut self, i: usize, j: usize, idx: usize, begin: usize, end: usize) -> T {
        self.propagate(idx);

        if end <= i || j <= begin {
            return self.default;
        } else if i <= begin && end <= j {
            return self.data[idx];
        }
        let center = (begin+end)/2;
        let l = self.inner_compute_range(i, j, idx*2, begin, center);
        let r = self.inner_compute_range(i, j, idx*2+1, center, end);
        (self.merger)(l, r)
    }

    /// Apply given value to [i, j).
    fn apply_range(&mut self, i: usize, j: usize, value: T) {
        let m = self.bottom_offset;
        self.inner_apply_range(i, j, value, 1, 0, m);
    }

    fn inner_apply_range(&mut self, i: usize, j: usize, value: T, idx: usize, begin: usize, end: usize) {
        self.propagate(idx);

        if end <= i || j <= begin {
            return;
        } else if i <= begin && end <= j {
            self.lazy[idx] = (self.lazy_merger)(self.lazy[idx], value);
            self.propagate(idx);
            return;
        }
        let center = (begin+end)/2;
        self.inner_apply_range(i, j, value, idx*2, begin, center);
        self.inner_apply_range(i, j, value, idx*2+1, center, end);
        self.data[idx] = (self.merger)(self.data[idx*2], self.data[idx*2+1]);
    }

    fn merge(&mut self, i: usize, j: usize) -> T {
        (self.merger)(self.data[i], self.data[j])
    }

    fn propagate(&mut self, idx: usize) {
        if self.lazy[idx] == self.lazy_default {
            return;
        }
        self.data[idx] = (self.lazy_merger)(self.data[idx], self.lazy[idx]);
        if idx*2+1 < self.lazy.len() {
            self.lazy[idx*2] = (self.lazy_merger)(self.lazy[idx*2], self.lazy[idx]);
            self.lazy[idx*2+1] = (self.lazy_merger)(self.lazy[idx*2+1], self.lazy[idx]);
        }
        self.lazy[idx] = self.lazy_default;
    }
}

// ====

fn main() {
    input! {
        n: usize, k: usize,
        p: [i32; n]
    };

    let mut segmax = SegmentTree::new(n+10, 0i32, Rc::new(|i, j| max(i, j)), 0i32, Rc::new(|i, j| max(i, j)));
    let mut segmin = SegmentTree::new(n+10, (n as i32), Rc::new(|i, j| min(i, j)), (n as i32), Rc::new(|i, j| min(i, j)));
    for i in 0..n {
        segmax.apply_range(i, i+1, p[i]);
        segmin.apply_range(i, i+1, p[i]);
    }

    let mut done = vec![false; n];
    let mut samecnt = 0;
    {

        let mut i = 0;
        while i < n {
            let mut j = i;
            while j+1 < n && p[j] < p[j+1] {
                j += 1;
            }
            let len = j+1-i;
            if len >= k {
                for w in i..i+(len-k)+1 {
                    done[w] = true;
                }
            }
            i = j+1;
        }
    }

    let mut first = true;
    for i in 0..n-k+1 {
        if done[i] {
            continue;
        }
        // compare [i, i+k) vs [i+1, i+k+1)
        if i+k >= n {
            break;
        }
        if done[i+1] {
            continue;
        }
        if segmin.compute_range(i, i+k) == p[i] && segmax.compute_range(i+1, i+k+1) == p[i+k] {
            samecnt += 1;
        }
    }
    let mut idet = 0;
    for i in 0..n {
        if done[i] {
            idet += 1;
        }
    }
    if idet >= 2 {
        samecnt += idet-1;
    }
    assert!((n-k+1) > samecnt);

    println!("{}", (n-k+1)-samecnt);
}