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>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const {
        long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u);
    }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r;
}
int getrand(int L, int R) { // [L,R]
    random_device rd; mt19937 mt(rd()); uniform_int_distribution<> rand(L, R); return rand(mt);
}
typedef ModInt<1000000007> mint1;
typedef ModInt<1000000009> mint2;
int primes[10] = { 10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093 };
bool isShuffle = false;
mint1 p1[201010];
mint2 p2[201010];
template <typename V>
struct SuperMultiSet
{
    int getrand(int x) { static uint32_t y = time(0); y ^= (y << 13); y ^= (y >> 17); y ^= (y << 5); return y % x; }
    struct Node {
        V val;
        Node *lch, *rch;
        int sz;

        mint1 hash1;
        mint2 hash2;

        Node(V a) :val(a), lch(0), rch(0), sz(1) { init();
            hash1 = a;
            hash2 = a;
        }

        void init() { }
        void push() { }
        void update() {
            hash1 = val;
            hash2 = val;
            int cnt = 0;
            if (lch)
            {
                cnt += lch->sz;
                hash1 = lch->hash1 + hash1 * p1[cnt];
                hash2 = lch->hash2 + hash2 * p2[cnt];
            }
            cnt++;
            if (rch)
            {
                hash1 = rch->hash1 * p1[cnt] + hash1;
                hash2 = rch->hash2 * p2[cnt] + hash2;
            }
        }
    };


    SuperMultiSet() :root(NULL) {}

    Node *root;
    inline int size(Node *t) { return t ? t->sz : 0; }
    int size() { return size(root); }
    inline void push(Node *t) {
        if (t == NULL)return;
        t->push();
    }
    inline Node *update(Node *t) {
        if (!t)return NULL;
        t->sz = size(t->lch) + size(t->rch) + 1;
        t->update();
        return t;
    }

    Node *merge(Node *a, Node *b) {
        push(a); push(b);
        if (!a)return b;
        if (!b)return a;

        if (getrand(size(a) + size(b))<size(a)) {
            a->rch = merge(a->rch, b);
            return update(a);
        }
        else {
            b->lch = merge(a, b->lch);
            return update(b);
        }
    }

    template<class... T> Node *merge(Node *a, T... y) { return merge(a, merge(y...)); }

    pair<Node *, Node *>split(Node *t, int k) {//[0,k) [k,N)
        push(t);
        if (!t)return make_pair(t, t);
        if (k <= size(t->lch)) {
            pair<Node *, Node *>s = split(t->lch, k);
            t->lch = s.second;
            return make_pair(s.first, update(t));
        }
        else {
            pair<Node *, Node *>s = split(t->rch, k - size(t->lch) - 1);
            t->rch = s.first;
            return make_pair(update(t), s.second);
        }
    }

    int lower_bound(V x) { return lower_bound(root, x); }
    int lower_bound(Node *t, V x) {
        push(t);
        if (!t)return 0;
        if (t->val >= x)return lower_bound(t->lch, x);
        return lower_bound(t->rch, x) + size(t->lch) + 1;
    }

    // [l,r]番目でa,b,cに分ける(終わったらちゃんとマージすること)
    // rootは0になってる
    tuple<Node*, Node*, Node*> split3(int l, int r) {
        auto p = split(root, l);

        auto a = p.first;

        auto q = split(p.second, r - l + 1);

        auto b = q.first;
        auto c = q.second;
        root = 0;

        return make_tuple(a, b, c);
    }

    void dump() {
        dump(root);
        cout << endl;
    }
    void dump(Node *t) {
        if (t == NULL)return;
        push(t);
        dump(t->lch);
        cout << t->val << " ";
        dump(t->rch);
    }

    void insert(V x) {
        int k = lower_bound(x);

        Node *a, *b;
        tie(a, b) = split(root, k);

        root = merge(a, new Node(x), b);
    }

    void erase(V x) {
        int k = lower_bound(x);

        Node *a, *b;
        tie(a, b) = split(root, k);

        Node *ba, *bb;
        tie(ba, bb) = split(b, 1);

        root = merge(a, bb);
    }

    V getAt(int i) {
        auto p = split(root, i);
        auto a = p.first;
        auto q = split(p.second, 1);
        Node* b = q.first;
        auto c = q.second;

        root = merge(merge(a, b), c);

        return b->val;
    }
};
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     @hamayanhamayan
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

int N, K, A[201010];
SuperMultiSet<int> sms;
mint1 lft1[201010], rht1[201010];
mint2 lft2[201010], rht2[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> K;
    rep(i, 0, N) cin >> A[i];

    p1[0] = 1;
    rep(i, 1, 201010) p1[i] = p1[i - 1] * primes[0];
    p2[0] = 1;
    rep(i, 1, 201010) p2[i] = p2[i - 1] * primes[1];

    lft1[0] = A[0];
    rep(i, 1, N) lft1[i] = lft1[i-1] + p1[i] * A[i];
    lft2[0] = A[0];
    rep(i, 1, N) lft2[i] = lft2[i-1] + p2[i] * A[i];

    rht1[N - 1] = A[N - 1];
    rrep(i, N - 2, 0) rht1[i] = rht1[i + 1] * primes[0] + A[i];
    rht2[N - 1] = A[N - 1];
    rrep(i, N - 2, 0) rht2[i] = rht2[i + 1] * primes[1] + A[i];

    set<pair<int, int>> se;
    rep(i, 0, K) sms.insert(A[i]);

    mint1 h1;
    mint2 h2;

    h1 = sms.root->hash1 + rht1[K] * p1[K];
    h2 = sms.root->hash2 + rht2[K] * p2[K];

    se.insert({h1.get(), h2.get()});
    rep(i, K, N) {
        sms.erase(A[i - K]);
        sms.insert(A[i]);

        h1 = lft1[i - K] + sms.root->hash1  * p1[i - K + 1] + rht1[i + 1] * p1[i + 1];
        h2 = lft2[i - K] + sms.root->hash2  * p2[i - K + 1] + rht2[i + 1] * p2[i + 1];

        se.insert({h1.get(), h2.get()});
    }

    int ans = se.size();
    cout << ans << endl;
}