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: Accepted)

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>

#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 10000000000000007ll;

const double eps = 1e-10;

// del

const i64 MAXN = 200005;

i64 a[MAXN];

i64 n, k;

i64 merge(vector<i64>& arr, const vector<i64>& left, const vector<i64>& right) {
    i64 i = 0, j = 0, count = 0;
    while (i < left.size() || j < right.size()) {
        if (i == left.size()) {
            arr[i + j] = right[j];
            j++;
        }
        else if (j == right.size()) {
            arr[i + j] = left[i];
            i++;
        }
        else if (left[i] <= right[j]) {
            arr[i + j] = left[i];
            i++;
        }
        else {
            arr[i + j] = right[j];
            count += left.size() - i;
            j++;
        }
    }
    return count;
}

i64 invCount(vector<i64>& arr) {
    if (arr.size() < 2) {
        return 0;
    }

    i64 m = (arr.size() + 1) / 2;
    vector<i64> left;
    left.resize(m);
    memcpy(left.data(), arr.data(), sizeof(i64) * m);

    i64 n = arr.size() - m;
    vector<i64> right;
    right.resize(n);
    memcpy(right.data(), arr.data() + m, sizeof(i64) * n);
    
    return invCount(left) + invCount(right) + merge(arr, left, right);
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed);

    cin >> n >> k;

    vector<i64> a;
    a.resize(n);

    for (i64 i = 0; i < n; i++) {
        cin >> a[i];
    }

    i64 R = 1;
    set<i64> q;

    i64 TR = 0;
    i64 t = 0;
    for (i64 i = 0; i < k; i++) {
        q.insert(a[i]);
        if (i > 0) {
            if (a[i] < a[i - 1]) {
                t = 0;
            }
            else {
                t++;
            }
        }
    }

    if (t == k - 1) {
        TR++;
    }

    for (i64 j = 0; j + k < n; j++) {
        q.insert(a[j + k]);

        if (a[j + k] < a[j + k - 1]) {
            t = 0;
        }
        else {
            t++;
        }
        if (t == k - 1) {
            TR++;
        }

        if (!(*q.begin() == a[j] && *q.rbegin() == a[j + k])) {
            R++;
        }
        q.erase(a[j]);
    }

    cout << R - max(0ll, TR - 1) << endl;

    return 0;
}