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>
using namespace std;
#define Int int64_t
#define dump(x) cout << (x) << endl
#define fi first
#define se second
Int mod = 1e9+7;
//Int mod = 998244353;
Int INF = 1e18;
double EPS = 0.00000001;

const int MAX_N = 1<<18;

int N = 1<<16;
int seg1[MAX_N * 2 - 1];

void init1() {
    for (int i = 1; i <= 2 * N - 1; i++) {
        seg1[i] = INT_MAX;
    }
}

// i は 0-indexed
void update1(int i, int x) {
    i += N; 
    seg1[i] = x;
    while (i > 1) {
        i /= 2;
        seg1[i] = min(seg1[i * 2], seg1[i * 2 + 1]);
    }
}

int query1(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return INT_MAX;
    if (a <= l && r <= b) return seg1[k];
    else {
        int vl = query1(a, b, k * 2, l, (l + r) / 2);
        int vr = query1(a, b, k * 2 + 1, (l + r) / 2, r);
        return min(vl, vr);
    }
}

int seg2[MAX_N * 2 - 1];

void init2() {
    for (int i = 1; i <= 2 * N - 1; i++) {
        seg2[i] = 0;
    }
}

// i は 0-indexed
void update2(int i, int x) {
    i += N; 
    seg2[i] = x;
    while (i > 1) {
        i /= 2;
        seg2[i] = max(seg2[i * 2], seg2[i * 2 + 1]);
    }
}

int query2(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return seg2[k];
    else {
        int vl = query2(a, b, k * 2, l, (l + r) / 2);
        int vr = query2(a, b, k * 2 + 1, (l + r) / 2, r);
        return max(vl, vr);
    }
}

int main() {
    init1();
    init2();
    Int n, k;
    cin >> n >> k;
    vector<Int> a(n);
    for (Int i = 0; i < n; i++) {
        cin >> a[i];
        update1(i, a[i]);
        update2(i, a[i]);
    }
    Int res = 1;
    Int cnt = 1;
    vector<bool> v(n, false);
    for (Int i = n - 1; i > 0; i--) {
        if (a[i] > a[i - 1]) {
            cnt++;
        }
        else {
            cnt = 1;
        }
        if (cnt >= k) {
            v[i - 1] = true;
            //res = 1;
        }
    }
    for (Int i = 0; i < n - k; i++) {
        if (v[i]) continue;
       // dump(query1(i, i + k, 1, 0, N));
        //dump(query2(i + 1, i + k + 1, 1, 0, N));
        if (!(a[i] == query1(i, i + k, 1, 0, N) && a[i + k] == query2(i + 1, i + k + 1, 1, 0, N))) {
            res++;
        }
    }
    dump(res);
    return 0;
}