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 rep(i,n) for(long long (i)=0;(i)<(n);++(i))
#define ref(i,a,b) for (long long (i)=(a); (i)<=(b); ++(i))
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define mp make_pair
const int mx=2e6+5;
#define int long long
int a[mx],c[mx],tmx[mx*5],t[mx*5];
void build(int n,int l,int r){
	if(l==r){
		t[n]=a[l];
		tmx[n]=a[l];
		return;
	}
	else if(l>r)return;
	int mid=(l+r)/2;
	build(n*2,l,mid);
	build(n*2+1,mid+1,r);
	t[n]=min(t[n*2],t[n*2+1]);
	tmx[n]=max(tmx[n*2],tmx[n*2+1]);
}
int rmq(int v,int l,int r,int ll,int rr){	
	if(rr<l or ll>r)return 1e18;
	if(ll<=l and rr>=r)return t[v];
	int mid=(l+r)/2;
	int bir=rmq(v*2,l,mid,ll,rr);
	int iki=rmq(v*2+1,mid+1,r,ll,rr);
	return min(bir,iki);
}
int rmx(int v,int l,int r,int ll,int rr){	
	if(rr<l or ll>r)return 0;
	if(ll<=l and rr>=r)return tmx[v];
	int mid=(l+r)/2;
	int bir=rmx(v*2,l,mid,ll,rr);
	int iki=rmx(v*2+1,mid+1,r,ll,rr);
	return max(bir,iki);
}
signed main(){
	/*freopen(".gir", "r", stdin);
	freopen(".cik", "w", stdout); 
    */ios_base::sync_with_stdio(false);cin.tie(NULL);
	int n,k;cin >> n >> k;
	for(int i=1;i<=n;++i){
		cin >> a[i];
	}
	build(1,1,n);
	int say=1,cik=0,ans=1;
	for(int i=1;i<k;++i){
		if(a[i]<a[i+1]){c[i]=1;++say;}
	}
	if(say==k)++cik;
	for(int i=k;i<=n;++i){
		if(i!=n){
			if(a[i]<a[i+1]){c[i]=1;++say;}
			say-=c[i-k+1];
			if(say==k)++cik;
		}
		if(i!=k){
			if(a[i]!=rmx(1,1,n,i-k,i))++ans;
			else if(a[i-k]!=rmq(1,1,n,i-k,i))++ans;
		}
	}
	//cout << cik << endl;
	if(cik>1){
		if(ans-(cik-1)<1)cout << 1 <<endl;
		else cout << ans-(cik-1) << endl;
	}
	else cout << ans << endl;
	//cout << ans << endl;
	//cout << ans-(cik-1) << endl;
}