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 C++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> P;

template <typename T>
struct segtree_min{
    int n;
    T UNIT;
    vector<T> dat;
    segtree_min(int n_, T unit){
        UNIT = unit;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    T calc(T a, T b){
        T ans;
        ans = min(a, b);
        return ans;
    }
    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1, 0, 0, segtree.n)と呼ぶこと
    T query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

template <typename T>
struct segtree_max{
    int n;
    T UNIT;
    vector<T> dat;
    segtree_max(int n_, T unit){
        UNIT = unit;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    T calc(T a, T b){
        T ans;
        ans = max(a, b);
        return ans;
    }
    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1, 0, 0, segtree.n)と呼ぶこと
    T query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

const int INF = 1e+8;
int p[200000];
int tmp[200000];
int ur[200000];

vector<P> v;

int main(){
    int N, K;
    cin >> N >> K;
    segtree_min<int> sgt_min(N, INF);
    segtree_max<int> sgt_max(N, -INF);
    for(int i = 0; i < N; i++){
        cin >> p[i];
        sgt_min.insert(i, p[i]);
        sgt_max.insert(i, p[i]);
    }
    sgt_min.update_all();
    sgt_max.update_all();
    int ans = 1;
    for(int i = 0; i+K+1 <= N; i++){
        int r = sgt_max.query(i, i+K+1, 0, 0, sgt_max.n);
        int l = sgt_min.query(i, i+K+1, 0, 0, sgt_max.n);
        //cout << l << ' ' << r << endl;
        if(l != p[i] || r != p[i+K]) ans++;
    }
    for(int i = 0; i+1 < N; i++){
        if(p[i] > p[i+1]) ur[i] = -1;
    }
    ur[N-1] = -1;
    int cnt = 0;
    vector<int> v;
    for(int i = N-1; i >= 0; i--){
        if(ur[i] != -1) ur[i] = ur[i+1]+1;
        if(ur[i] >= K-2) {
            v.push_back(i);
        }
        //cout << ur[i] << ' ';
    }
    if(v.size() != 0) cnt = 1;
    for(int i = 1; i < v.size(); i++){
        if(v[i-1] > v[i+1]+K) cnt++;
    }
    //cout << endl;
    //cout << ans << ' '<< cnt << endl;
    if(cnt >= 2) ans -= (cnt-1);
    cout << ans << endl;
}