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)

// orz dxm
#include<bits/stdc++.h>

using namespace std;

#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define y0 yORZDXM0
#define y1 yORZDXM1
#define y2 yORZDXM2
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9 + 7;
const int maxn = 2e5 + 5;
const int logn = 21;

int n, k;
int p[maxn], q[maxn], lg[maxn];
int mx[maxn][logn], mn[maxn][logn];

inline bool check(int l, int r){
	int k = lg[r - l + 1] - 1;
	if(p[l - 1] < min(mn[l][k], mn[l + (1 << k)][k]) && p[r + 1] > max(mx[l][k], mx[l + (1 << k)][k])) return true;
	return false;
}

int main(){
	//freopen("b.in", "r", stdin);
	//freopen("b.out", "w", stdout);
	scanf("%d%d", &n, &k);
	lg[0] = 0;
	REP(i, 1, n){
		lg[i] = lg[i - 1];
		if((1 << lg[i]) < i) ++lg[i];
	}
	FOR(i, 0, n){
		scanf("%d", p + i);
		mn[i][0] = p[i];
		mx[i][0] = p[i];
	}
	for(int i = n - 1; i >= 0; --i){
		if(p[i] > p[i + 1]) q[i] = i + 1;
		else q[i] = q[i + 1];
	}
	FOR(j, 1, logn) FOR(i, 0, n){
		if(i + (j - 1 << 1) < n) mn[i][j] = min(mn[i][j - 1], mn[i + (j - 1 << 1)][j - 1]);
      	else mn[i][j] = mn[i][j - 1];
		if(i + (j - 1 << 1) < n) mx[i][j] = max(mx[i][j - 1], mx[i + (j - 1 << 1)][j - 1]);
      	else mx[i][j] = mx[i][j - 1];
	}
	int ans = 0, f;
	if(q[0] < k) ans = 1;
	else f = 1;
	REP(i, 1, n - k){
		if(q[i] >= i + k) f = 1;
		if(q[i] >= i + k || check(i, i + k - 2)) continue;
		++ans;
		//printf("%d\n", i);
	}
	printf("%d\n", ans + f);
	return 0;
}