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;
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;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define endl "\n"
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

template <typename Monoid>
struct SegmentTree
{
    using F = function<Monoid(Monoid, Monoid)>;

private:
    int n;
    vector<Monoid> node;
    Monoid E;
    F f;

public:
    SegmentTree(vector<Monoid> &v, Monoid e, const F func) : f(func), E(e)
    {
        int sz = v.size();
        n = 1;
        while (n < sz)
        {
            n *= 2;
        }
        node.resize(2 * n - 1, E);
        for (int i = 0; i < sz; i++)
        {
            node[i + n - 1] = v[i];
        }
        for (int i = n - 2; i >= 0; i--)
        {
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    void update(int i, Monoid val)
    {
        i += (n - 1);
        node[i] = val;
        while (i > 0)
        {
            i = (i - 1) / 2;
            node[i] = f(node[2 * i + 1], node[2 * i + 2]);
        }
    }

    Monoid query(int a, int b, int i = 0, int l = 0, int r = -1)
    {
        if (r < 0)
        {
            r = n;
        }
        if (r <= a || b <= l)
        {
            return E;
        }
        if (a <= l && r <= b)
        {
            return node[i];
        }
        Monoid vl = query(a, b, 2 * i + 1, l, (l + r) / 2);
        Monoid vr = query(a, b, 2 * i + 2, (l + r) / 2, r);
        return f(vl, vr);
    }

    Monoid operator[](const int &i) const
    {
        return node[i + n - 1];
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    vector<int> p(n);
    for (int i = 0; i < n; i++)
    {
        cin >> p[i];
    }
    SegmentTree<int> segmax(p, -INF, [](int a, int b) { return max(a, b); });
    SegmentTree<int> segmin(p, INF, [](int a, int b) { return min(a, b); });
    int ans = n - k + 1;
    for (int i = 0; i < n - k; i++)
    {
        int bef = segmin.query(i, i + k);
        int aft = segmax.query(i + 1, i + k + 1);
        if (bef == p[i] && aft == p[i + k])
        {
            ans--;
        }
    }
    int tmp = 0;
    int cnt = 0;
    // 単調増加してるかどうかを調べる
    // p.insert(p.begin(), INF);
    // p.push_back(-INF);
    for (int i = 0; i < n - 1; i++)
    {
        if (p[i] < p[i + 1])
        {
            tmp++;
        }
        else
        {
            tmp = 0;
        }
        // cout << cnt << endl;
        if (tmp >= k - 1)
        {
            cnt++;
        }
    }
    if (cnt > 0)
    {
        cnt--;
    }
    ans -= cnt;
    cout << ans << endl;
}