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)

//#pragma GCC optimize ("-O3","unroll-loops")
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<time.h>
#include <random>

using namespace std;

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define print(x) cout<<x<<'\n';
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i,v.size())cout << v[i] << ((i == v.size() - 1) ? "\n" : " ");
//#define double long double



template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a>b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a<b) a = b; }

const int MOD = 1e9 + 7;
const ll INF = 1e17;
const int INT_INF = 1e9;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int>P;
const int MAX = 200020;

const int MAX_N = 1 << 19;
int n;
int dat2[2 * MAX_N - 1];//セグ木を持つグローバル配列
int dat[2 * MAX_N - 1];//セグ木を持つグローバル配列

//初期化
void init2(int n_) {
	n = 1;
	//簡単のため、要素数を2のべき乗に
	while (n < n_)n *= 2;
	//gcdに影響を与えない値で初期化する
	for (int i = 0; i < 2 * n - 1; i++)dat2[i] = 0;
}

//k番目の値(0-indexed)をaに更新
void update2(int k, int a) {
	k += n - 1;//葉の節点
	dat2[k] = a;
	//上りながら更新
	while (k > 0) {
		k = (k - 1) / 2;
		dat2[k] = max(dat2[k * 2 + 1], dat2[k * 2 + 2]);
	}
}

//[a,b)のgcdを求める
//そとからは、(a,b,0,0,n)として呼ぶ
int query2(int a, int b, int k, int l, int r) {
	//[a,b)と[l,r)が交差していなければ、gcdに影響を与えない値を返す
	//関数定義でgcdの単位元を0としている
	if (r <= a || b <= l)return 0;
	//[a,b)が[l,r)を完全に含んでいれば、この節点の値
	if (a <= l && r <= b)return dat2[k];
	//そうでなければ、二つの子のgcd
	else {
		int vl = query2(a, b, k * 2 + 1, l, (l + r) / 2);
		int vr = query2(a, b, k * 2 + 2, (l + r) / 2, r);
		return max(vl, vr);
	}
}

void init(int n_) {
	n = 1;
	//簡単のため、要素数を2のべき乗に
	while (n < n_)n *= 2;
	//gcdに影響を与えない値で初期化する
	for (int i = 0; i < 2 * n - 1; i++)dat[i] = INF;
}

//k番目の値(0-indexed)をaに更新
void update(int k, int a) {
	k += n - 1;//葉の節点
	dat[k] = a;
	//上りながら更新
	while (k > 0) {
		k = (k - 1) / 2;
		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
}

//[a,b)のgcdを求める
//そとからは、(a,b,0,0,n)として呼ぶ
int query(int a, int b, int k, int l, int r) {
	//[a,b)と[l,r)が交差していなければ、gcdに影響を与えない値を返す
	//関数定義でgcdの単位元を0としている
	if (r <= a || b <= l)return INF;
	//[a,b)が[l,r)を完全に含んでいれば、この節点の値
	if (a <= l && r <= b)return dat[k];
	//そうでなければ、二つの子のgcd
	else {
		int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
		int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	}
}


int A[200020];
int N;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> N;
	int K; cin >> K;
	init(N+1); init2(N+1);
	REP(i, N) {
		cin >> A[i];
		update(i, A[i]);
		update2(i, A[i]);
	}
	int ans = 1;
	FOR(l, 1, N - K + 1) {
		int r = l + K;
		if (query2(l, r, 0, 0, n) == A[r - 1] && query(l - 1, r - 1, 0, 0, n) == A[l - 1])continue;
		else ans++;
	}

	int r = 0;
	set<int>st;

	set<int>st2;
	st2.insert(A[0]);
	int found = 0;
	REP(l, N - K + 1) {
		while (r + 1 < l + K) {
			r++;
			auto las = st2.end(); las--;
			//pe(*las)print(A[r]);
			if (A[r] < *las) {
				st2.insert(A[r]);
				FOR(i, l, r + 1) {
					st2.erase(A[i]);
				}
				l = r, r = l;
				if (l < N)st2.insert(A[l]);
				l--;
				goto END;
			}
			st2.insert(A[r]);
		}
		if (r - l == K - 1) {
			//pe(l)pe(r)print("+");
			found++;
			FOR(i, l, r + 1) {
				st2.erase(A[i]);
			}
			l = r;
			l++;
			if (l + 1 < N)st2.insert(A[l + 1]);
			r = l;
			continue;
		}

		st2.erase(A[l]);
	END:;
	}

	if (found > 1) {
		ans -= (found - 1);
	}
	print(ans);
}