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>
#include <complex>
#include <iomanip>

using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define fi first
#define se second
#define mp make_pair
typedef long long int64;
typedef vector<int64> vec;
typedef vector<vec> mat;

int64 N,M,H,W,K,Q,A,B,C,L,R;
string S,T;
const int64 MOD = (1e+9) + 7;
const int64 INF = 1LL << 60;
const long double EPS = 1e-9;
typedef pair<int64,int64> P;

typedef vector<P> vp;
typedef vector<vp> matP;

void kill()
{
  cout << -1 << endl;
  exit(0);
}
int main()
{
    int N, K;
    cin >> N;
    cin >> K;
    vector<int> P(N);
    vector<int> UD(N);
    int cnt = 1;
    for (int i = 0; i < N; i++){
        cin >> P[i];
        if (i >= 1 && P[i - 1] < P[i]){
            cnt++;
        } else {
            cnt = 1;
        }
        UD[i] = cnt;
    }
    set<int> a, b;
    
    for (int i = 0; i < K; i++){
        a.insert(P[i]);
    }
    set<int>::iterator ite, ite2;
    

    //int ans = 1;
    int ans = UD[K - 1] < K;
    int sameFlag = 0;
    int prevFlag = 0;
    for (int i = K; i < N; i++){
        //printf("(%d, %d) UD[%d] = %d\n",*a.begin(), -*b.begin(), i, UD[i]);
        if (UD[i] >= K){
            sameFlag = 1;
        //} else if (!(P[i - K] == *a.begin() && P[i] > -*b.begin())){
        } else if ((P[i - K] != (*a.begin()) || P[i] < (*a.rbegin()))) {
            ans++;
        }
        a.insert(P[i]);
        a.erase(P[i - K]);
    }
    if (UD[K - 1] >= K){
      sameFlag = 1;
    }
    cout<<ans + sameFlag <<endl;
    return 0;
}