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>	
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ld long double
#define pii pair <int, int>
#define sz(a) (int)(a.size()) 
#define resize(v) v.resize(unique(all(v)) - v.begin()); 
#define all(a) a.begin(), a.end()
#define what prev
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)

// random_device rd; mt19937 g(rd());    
                                        	
using namespace std;
using namespace __gnu_pbds;
 
void Fast_Read_Out()
{
    ios_base::sync_with_stdio(0);
    cin.tie(), cout.tie();
}
 
void Random()
{
    unsigned int seed;                                                                        
    asm("rdtsc" : "=A" (seed));
    srand(seed);        
}
 
unsigned int Time()
{
     unsigned int time = clock() / 1000.00;                           
     return time;
}

template <typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>; 
 
const int mod = (int)1e9 + 7;
const int N = (int)2e5 + 123; 

int n, k;
int a[N];
int pref[N];
int ans[N];
int t[N * 4];
int t1[N * 4];
int t_min[N * 4], t_max[N * 4];
int res;
vector <pair <pair <int, int>, int> > g[N];

void Update(int pos, int val, int tl = 1, int tr = n, int v = 1)
{
	if(tl == tr)
	{	
		t[v] = val;
		return;
	}
	int tm = (tl + tr) / 2;
	if(pos <= tm) Update(pos, val, tl, tm, v + v);
	else Update(pos, val, tm + 1, tr, v + v + 1);
	t[v] = max(t[v + v], t[v + v + 1]);
}

int Get(int l, int r, int tl = 1, int tr = n, int v = 1)
{
	if(tl > r || l > tr) return 0;
	if(l <= tl && tr <= r) return t[v];
	int tm = (tl + tr) / 2;
	return max(Get(l, r, tl, tm, v + v), Get(l, r, tm + 1, tr, v + v + 1));
}

void Update1(int pos, int val, int tl = 1, int tr = n, int v = 1)
{
	if(tl == tr)
	{
		t1[v] = val;
		return;
	}
	int tm = (tl + tr) / 2;
	if(pos <= tm) Update1(pos, val, tl, tm, v + v);
	else Update1(pos, val, tm + 1, tr, v + v + 1);
	t1[v] = max(t1[v + v], t1[v + v + 1]);
}

int Get1(int l, int r, int tl = 1, int tr = n, int v = 1)
{
	if(tl > r || l > tr) return 0;
	if(l <= tl && tr <= r) return t1[v];
	int tm = (tl + tr) / 2;
	return max(Get1(l, r, tl, tm, v + v), Get1(l, r, tm + 1, tr, v + v + 1)); 
}

void Update_M(int pos, int val, int tl = 1, int tr = n, int v = 1)
{
	if(tl == tr)
	{
		t_min[v] = val;
		t_max[v] = val;
		return;
	}
	int tm = (tl + tr) / 2;
	if(pos <= tm) Update_M(pos, val, tl, tm, v + v);
	else Update_M(pos, val, tm + 1, tr, v + v + 1);
	t_min[v] = min(t_min[v + v], t_min[v + v + 1]);
	t_max[v] = max(t_max[v + v], t_max[v + v + 1]);
}

int Min(int l, int r, int tl = 1, int tr = n, int v = 1)
{                               
	if(tl > r || l > tr) return mod;
	if(l <= tl && tr <= r) return t_min[v];
	int tm = (tl + tr) / 2;
	return min(Min(l, r, tl, tm, v + v), Min(l, r, tm + 1, tr, v + v + 1)); 
}

int Max(int l, int r, int tl = 1, int tr = n, int v = 1)
{
	if(tl > r || l > tr) return -mod;
	if(l <= tl && tr <= r) return t_max[v];
	int tm = (tl + tr) / 2;
	return max(Max(l, r, tl, tm, v + v), Max(l, r, tm + 1, tr, v + v + 1)); 
}

int main ()
{
	#ifdef JUDGE
		freopen("input.txt", "r", stdin);
	#endif		
	Fast_Read_Out();
	Random();   
	cin >> n >> k;
	for(int i = 1; i <= n; i++) cin >> a[i], a[i]++;
	for(int i = 1; i <= n; i++)
	{
		pref[i] = Get(a[i] + 1, n);
		Update(a[i], i);
	}	
	for(int i = 1; i <= n - k + 1; i++)
	{
		int l, r;
		l = i, r = i + k - 1;
		g[r].pb(mp(mp(l, 0), i));
	}
	for(int i = 1; i <= n; i++)
	{
		if(pref[i]) Update1(pref[i], a[i] + a[pref[i]]);
		for(auto to : g[i])
		{
			if(Get1(to.F.F, i) <= to.F.S) ans[to.S] = 1;
			else ans[to.S] = 0;		
		}
	}
	for(int i = 1; i <= n; i++) Update_M(i, a[i]);
	for(int i = 1; i <= n - k; i++)
	{
		if(i == 1)
		{
			if(a[i] == Min(i, i + k) && a[i + k] == Max(i, i + k)) res++;
			else res += 2;
		}
		else
		{
			if(ans[i]) continue;	
			if(a[i] == Min(i, i + k) && a[i + k] == Max(i, i + k));
			else res++;
		}
	}
	if(n == k) res++;
	cout << res << endl;
}