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: Accepted)

#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P std::pair<int,int>
#define PiP std::pair<int,std::pair<int,int>>
#define all(v) v.begin(),v.end()
#define mkp std::make_pair
#define prique(T) std::priority_queue<T,vector<T>,greater<T>>
using namespace std;
template<class T> inline void chmax(T& a, T b) { a = std::max(a, b); }
template<class T> inline void chmin(T& a, T b) { a = std::min(a, b); }

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)return false;
	}
	return x != 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
	std::random_device rnd;
	std::mt19937_64 mt64(rnd());
	std::uniform_int_distribution<int>engine(MIN, MAX);
	return engine(mt64);
}
/*--------Library Zone!--------*/


struct Segtree {
	int size = 1;
	vector<int>dat1, dat2;
	Segtree(int x) {
		while (size < x)size *= 2;
		dat1.resize(2 * size - 1, -1);
		dat2.resize(2 * size - 1, inf);
	}
	void update(int x, int y, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = size;
		if (r - l == 1) {
			dat1[k] = y;
			dat2[k] = y;
			return;
		}
		if (x < (l + r) / 2) {
			update(x, y, k * 2 + 1, l, (l + r) / 2);
		}
		else {
			update(x, y, k * 2 + 2, (l + r) / 2, r);
		}
		dat1[k] = max(dat1[k * 2 + 1], dat1[k * 2 + 2]);
		dat2[k] = min(dat2[k * 2 + 1], dat2[k * 2 + 2]);
	}
	int querymax(int x, int y, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = size;
		if (r <= x || y <= l)return -1;
		if (x <= l && r <= y) {
			return dat1[k];
		}
		int lval = querymax(x, y, k * 2 + 1, l, (l + r) / 2);
		int rval = querymax(x, y, k * 2 + 2, (l + r) / 2, r);
		return max(lval, rval);
	}
	int querymin(int x, int y, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = size;
		if (r <= x || y <= l)return inf;
		if (x <= l && r <= y) {
			return dat2[k];
		}
		int lval = querymin(x, y, k * 2 + 1, l, (l + r) / 2);
		int rval = querymin(x, y, k * 2 + 2, (l + r) / 2, r);
		return min(lval, rval);
	}
};
int n, k;
int p[214514];
signed main() {
	cin >> n >> k;
	Segtree S(n);
	rep(i, n) {
		cin >> p[i];
		S.update(i, p[i]);
	}
	int ans = 1;
	int cnt = 1;
	REP(i, k) {
		if (p[i - 1] < p[i])cnt++;
		else cnt = 1;
	}
	bool flag = cnt == k;
	for (int i = k; i < n; i++) {
		if (p[i - 1] < p[i])cnt++;
		else cnt = 1;
		if (S.querymin(i - k + 1, i) < p[i - k] || S.querymax(i - k, i) > p[i]) {
			if (cnt >= k && flag)continue;
			ans++;
		}
		if (cnt >= k)flag = true;
	}
	cout << ans << endl;
}