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)

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#ifdef LOCAL
struct PrettyOS {
    ostream& os;
    bool first;
    template <class T> auto operator<<(T&& x) {
        if (!first) os << ", ";
        first = false;
        os << x;
        return *this;
    }
};
template <class... T> void dbg0(T&&... t) {
    (PrettyOS{cerr, true} << ... << t);
}
#define dbg(...)                                            \
    do {                                                    \
        cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
        dbg0(__VA_ARGS__);                                  \
        cerr << endl;                                       \
    } while (false);
#else
#define dbg(...)
#endif

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << ", " << p.second << ")";
}

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n, k;
    cin >> n >> k;
    V<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    V<bool> mi(n - k + 1), ma(n - k + 1);
    set<int> st;
    for (int i = 0; i < k; i++) st.insert(a[i]);
    for (int l = 0; l <= n - k; l++) {
        mi[l] = (a[l] == *st.begin());
        ma[l] = (a[l + k - 1] == *st.rbegin());
        if (l == n - k) break;
        st.erase(a[l]);
        st.insert(a[l + k]);
    }
    dbg(mi);
    dbg(ma);
    int ans = n - k + 1;
    bool same = false;
    int cnt = 0;
    for (int i = 0; i < k - 1; i++) {
        if (a[i] > a[i + 1]) cnt++;
    }
    for (int l = 0; l <= n - k; l++) {
        if (cnt == 0) {
            //sorted
            dbg(l);
            if (mi[l] && ma[l]) {
                same = true;
                ans--;
                mi[l] = ma[l] = false;
            }
        }
        if (l == n - k) break;
        if (a[l] > a[l + 1]) cnt--;
        if (a[l + k - 1] > a[l + k]) cnt++;
    }
    if (same) ans++;
    for (int l = 0; l < n - k; l++) {
        if (mi[l] && ma[l + 1]) ans--;
    }
    cout << ans << endl;
    return 0;
}