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;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i,s,n) for(int i = s; i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ( (n) ? "Yes":"No") << endl;
#define CFYN(n) cout << ( (n) ? "YES":"NO") << endl;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;

int main(void)
{
    IOS
    constexpr int N = 200005;
    int n,k; cin >>n >> k;
    vector<int> v(n);
    bitset<N> mask((1LL << (k-1)) -1);
    bitset<N> bs;
    set<int> st;
    int mx = 0;
    int mn = 0;
    
    REP(i,n) cin >> v[i];
    REP(i,k-1) {
        st.insert(v[i]);
        bs <<= 1;
        bs[0] = v[i+1] > v[i];
    }
    mn = *st.begin();
    mx = *st.rbegin();
    int ans = 1;

    // 単調増加列 または押し出されるのがminかつ入るのがmax のとき ansは増えない
    bool once = true;
    REP(i,n-k) {
        //cerr << "i:" << i << endl;
        bool plus = true;
        bs <<= 1;
        bs[0] = v[k+i] > v[k+i-1];
        bs &= mask;
        if (bs.count() == k-1) {
            //cerr << "p1:" << bs[2] << bs[1] << bs[0] << endl;
            if (once) once = false;
            else plus = false;
        }
        if (v[i] == mn && v[i+k] > mx ) {
            //cerr << "p2:mn:mx " << mn << " " << mx << endl;
            plus = false;
        }
        st.erase(v[i]);
        st.emplace(v[k+i]);
        mn = *st.begin();
        mx = *st.rbegin();
        if (plus) ans++;
    }

    cout << ans << endl;

    return 0;
}