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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);(i)++)
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);(i)--)
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);(i)--)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
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; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& input, vector<T>& v) { for (auto& elemnt : v)input >> elemnt; return input; }

class segtree {
    int n = 1; VI mindat, maxdat;
public:
    segtree(VI a) {
        int _n = a.size();
        while (n < _n)n *= 2;
        mindat.resize(2 * n - 1, INF);
        maxdat.resize(2 * n - 1, 0);
        FOR(i, 0, _n)mindat[i + n - 1] = maxdat[i + n - 1] = a[i];
        rFOR(i, 0, n - 1) {
            mindat[i] = min(mindat[2 * i + 1], mindat[2 * i + 2]);
            maxdat[i] = max(maxdat[2 * i + 1], maxdat[2 * i + 2]);
        }
    }
    int Min(int a, int b, int k, int l, int r) {
        if (b <= l || r <= a)return INF;
        if (a <= l && r <= b)return mindat[k];
        else {
            int s = Min(a, b, 2 * k + 1, l, (l + r) / 2);
            int t = Min(a, b, 2 * k + 2, (l + r) / 2, r);
            return min(s, t);
        }
    }
    int Min(int a, int b) { return Min(a, b, 0, 0, n); }
    int Max(int a, int b, int k, int l, int r) {
        if (b <= l || r <= a)return 0;
        if (a <= l && r <= b)return maxdat[k];
        else {
            int s = Max(a, b, 2 * k + 1, l, (l + r) / 2);
            int t = Max(a, b, 2 * k + 2, (l + r) / 2, r);
            return max(s, t);
        }
    }
    int Max(int a, int b) { return Max(a, b, 0, 0, n); }
};

int main() {
    init();

    int n, k;
    cin >> n >> k;
    VI p(n);
    FOR(i, 0, n)cin >> p[i];

    segtree seg(p);

    vector<bool> isSorted(n - k + 1);
    int cnt = 0, itr = 1;
    FOR(i, 0, n - k + 1) {
        while (itr < i + k) {
            if (p[itr - 1] > p[itr])cnt++;
            itr++;
        }
        isSorted[i] = (cnt == 0);
        if (p[i] > p[i + 1])cnt--;
    }

    vector<bool> New(n - k + 1, false);
    New[0] = true;
    FOR(i, 0, n - k) {
        int MAX = seg.Max(i, i + k);
        int MIN = seg.Min(i, i + k);
        New[i + 1] = !(p[i] == MIN && MAX < p[i + k]);
    }

    int ans = 0;
    bool plus = false;
    FOR(i, 0, n - k + 1) {
        if (isSorted[i])plus = true;
        else if(New[i])ans++;
    }
    if (plus)ans++;

    cout << ans << "\n";
}