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<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<algorithm>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define For(i,x,y) for(int i=((int)x);i<=((int)y);i++)
#define Dep(i,y,x) for(int i=((int)y);i>=((int)x);i--)
#define Rep(i,x) for (int y,i=head[x];i;i=E[i].nxt)
using namespace std;

const int inf=1000000009;

typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef pair<int,int> pii;

int rd() {
	char c=getchar(); int t=0,f=1;
	while (!isdigit(c)) f=(c=='-')?-1:1,c=getchar();
	while (isdigit(c)) t=t*10+c-48,c=getchar(); return t*f;
}
void wt(int x) {
	if (x<0) putchar('-'),wt(-x);
	else { if (x>9) wt(x/10); putchar(x%10+48); }
}

const int N=2e5+35;

int n,K,P[N],Q[N],h,t,L[N],R[N],ans;

int main() {
	n=rd(),K=rd();
	For (i,1,n) P[i]=rd();
	
	h=1,t=0;
	For (i,1,K-1) {
		while (h<=t && P[i]<P[Q[t]]) t--;
		Q[++t]=i;
	}
	For (i,K,n) {
		while (Q[h]<i-K+1) h++;
		L[i-K+1]=Q[h];
		while (h<=t && P[i]<P[Q[t]]) t--;
		Q[++t]=i;
	}
	
	h=1,t=0;
	Dep (i,n,n-K+2) {
		while (h<=t && P[i]>P[Q[t]]) t--;
		Q[++t]=i;
	}
	Dep (i,n-K+1,1) {
		while (Q[h]>i+K-1) h++;
		R[i]=Q[h];
		while (h<=t && P[i]>P[Q[t]]) t--;
		Q[++t]=i;
	}
	
	ans=n-K+1;
	For (i,1,n-K)
		if (L[i]==i && R[i+1]==i+K) ans--;
	printf("%d\n",ans);
	return 0;
}