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: Runtime Error)

// Dmitry _kun_ Sayutin (2019)

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::cerr;

using std::vector;
using std::map;
using std::array;
using std::set;
using std::string;
using std::queue;

using std::pair;
using std::make_pair;

using std::tuple;
using std::make_tuple;
using std::get;

using std::min;
using std::abs;
using std::max;
using std::swap;

using std::unique;
using std::sort;
using std::generate;
using std::reverse;
using std::min_element;
using std::max_element;

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) {}
#endif

template <typename T>
T input() {
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e) {
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

#define SZ(vec)         int((vec).size())
#define ALL(data)       data.begin(),data.end()
#define RALL(data)      data.rbegin(),data.rend()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()

const int mod = 1000 * 1000 * 1000 + 7;
int p = 10000481;
vector<int> pows;

struct node_t {
    int cnt;
    int hash;
};

node_t operator+(node_t a, node_t b) {
    node_t res;

    res.cnt = a.cnt + b.cnt;
    res.hash = (a.hash * int64_t(pows[b.cnt]) + b.hash) % mod;
    return res;
}

const int max_n = 1 << 17;
vector<node_t> segtree(2 * max_n, node_t {0, 0});

int main() {
    std::iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // code here
    int n, k;
    cin >> n >> k;

    vector<int> arr(n);
    for (int& elem: arr)
        elem = input<int>() + 1;

    pows.push_back(1);
    for (int i = 0; i < n + 3; ++i)
        pows.push_back((pows.back() * int64_t(p)) % mod);

    vector<int> prefhash(n + 1);
    for (int i = 0; i != n; ++i)
        prefhash[i + 1] = (int64_t(p) * prefhash[i] + arr[i]) % mod;

    auto update = [&](int i) {
        for (i /= 2; i != 0; i /= 2) {
            segtree[i] = segtree[2 * i] + segtree[2 * i + 1];
        }
    };

    for (int pos = n - 1; pos >= n - k; --pos) {
        segtree[max_n + arr[pos]] = node_t {1, arr[pos]};
        update(max_n + arr[pos]);
    }

    set<int> st;
    node_t sufhash = node_t {0, 0};
    for (int end = n - 1; end >= k - 1; --end) {
        node_t n1 = node_t {end - k + 1, prefhash[end - k + 1]};
        node_t n2 = segtree[1];
        node_t n3 = sufhash;

        st.insert((n1 + n2 + n3).hash);
        
        if (end != k - 1) {
            segtree[max_n + arr[end]] = node_t {0, 0};
            update(max_n + arr[end]);

            segtree[max_n + arr[end - k]] = node_t {1, arr[end - k]};
            update(max_n + arr[end - k]);

            sufhash = node_t {1, arr[end]} + sufhash;
        }
    }
    

    cout << SZ(st) << "\n";

    return 0;
}