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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define EM 1000000
using namespace std;
using LL = long long;
using P = pair<LL, LL>;
LL LINF = 1e18;
int INF = 1e9;
LL mod = 1e9+7;
using vint = vector<int>;
using vLL = vector<LL>;
using vvint = vector<vector<int>>;
using vvLL = vector<vector<LL>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

vector<LL> deq(vector<LL> a, LL k) {
    LL n = a.size();
    vector<LL> b;
    vector<LL> deque(n + 10);
    int s = 0, t = 0;
    for (int i = 0; i < n; i++) {
        while (s < t && a[deque[t - 1]] >= a[i]) t--;
        deque[t++] = i;
        if (i - k + 1 >= 0) {
            b.push_back(a[deque[s]]);
            if (deque[s] == i + 1 - k) s++;
        }
    }
    return b;
}

vector<LL> deq2(vector<LL> a, LL k) {
    LL n = a.size();
    vector<LL> b;
    vector<LL> deque(n + 10);
    int s = 0, t = 0;
    for (int i = 0; i < n; i++) {
        while (s < t && a[deque[t - 1]] <= a[i]) t--;
        deque[t++] = i;
        if (i - k + 1 >= 0) {
            b.push_back(a[deque[s]]);
            if (deque[s] == i + 1 - k) s++;
        }
    }
    return b;
}

int main(){
    int N, K;
    cin >> N >> K;
    vector<LL>  p(N);
    vector<int> noc(N, 0);
    for(int i = 0;i < N;i++)    scanf("%d", &p[i]);
    vector<LL>  bmin = deq(p, K+1);
    vector<LL>  bmax = deq2(p, K+1);
    int cnt = 1;
    LL ans = 0;
    for(int i = 1;i < N;i++){
        if(p[i] > p[i-1]){
            cnt = min(cnt+1, K);
            if(cnt == K){
                noc[i-K+1] = 1;
                ans = 1;
            }
        }else{
            cnt = 1;
        }
    }
    if(noc[0] == 0) ans++;
    for(int i = 0;i+K < N;i++){
        if(noc[i+1] == 1) continue;
        if(i > 0 && (bmin[i] != p[i] || bmax[i] != p[i+K]))    ans++;
    }

    printf("%d", ans);
}