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;

using ll = long long;
const int INF = 1e9;

struct segtree {
	
	vector <int> tree;
	int size;
	
	void init (int n) {
		size = 1;
		while (size < n) {
			size <<= 1;
		}
		tree.assign(size<<1, INF);
	}
	
	int son (int v, int num) {
		return (v<<1)+(num-1);
	}
	
	void set (int v, int lv, int rv, int i, int x) {
		if (lv == rv) {
			tree[v] = x;
			return;
		}
		int mid = (lv+rv)>>1;
		if (mid >= i) {
			set (son(v, 1), lv, mid, i, x);
		} else {
			set (son(v, 2), mid+1, rv, i, x);
		}
		tree[v] = min(tree[son(v, 1)], tree[son(v, 2)]);
	}
	
	void set (int i, int x) {
		if (!x) x = INF;
		set (1, 1, size, i, x);
	}
	
	int inv (int v, int lv, int rv, int l, int r) {
		if (lv > r || rv < l) {
			return INF;
		}
		if (lv >= l && rv <= r) {
			return tree[v];
		}
		int mid = (lv+rv)>>1;
		int left = inv(son(v, 1), lv, mid, l, min(r, mid));
		int right = inv(son(v, 2), mid+1, rv, max(mid+1, l), r);
		return min(right, left);
	}
	
	int inv (int l, int r) {
		return inv (1, 1, size, l, r);
	}
	
};

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, k, ans = 0;
	segtree t;
	cin >> n >> k;
	t.init(n);
	vector <int> a(n+1);
	for (int i=1; i<=n; i++) {
		cin >> a[i];
		a[i] ++;
	}
	int tec = 1;
	for (int i=2; i<=n; i++) {
		if (a[i] > a[i-1]) {
			tec ++;
		} else {
			if (tec >= k) break;
			tec = 1;
		}
	}
	if (tec >= k) {
		ans ++;
	}
	tec = 0; // will use for the leftmost inversion with current element
	set <pair <int, int>> s;
	for (int i=1; i<=n; i++) {
		if (i > k) {
			t.set(a[i-k], INF);
		}
		bool check = 0;
		tec = t.inv(a[i], n);
		if (tec < INF) {
			s.insert({tec, i});
			check = 1;
		}
		while (!s.empty()) {
			pair <int, int> m = *s.begin();
			if (m.first <= i-k) {
				s.erase(m);
				check = 1;
			} else break;
		}
		if (check && i >= k) {
			ans ++;
		}
		t.set(a[i], i);
	}
	cout << ans;
	return 0;
}