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 (GCC 9.2.1) (judge verdict: Accepted)

#include <stdio.h>

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[200001];
	int size;
} max_heap;

void push(data x, max_heap* h)
{
	int i = h->size;
	data tmp;
	h->obj[(h->size)++] = x;
	while (1) {
		if (i == 0) break;
		else if (h->obj[i].key > h->obj[(i-1)/2].key) {
			tmp = h->obj[(i-1)/2];
			h->obj[(i-1)/2] = h->obj[i];
			h->obj[i] = tmp;
			i = (i - 1) / 2;
		} else break;
	}
}

data pop(max_heap* h)
{
	int i = 0, j;
	data output = h->obj[0], tmp;
	h->obj[0] = h->obj[--(h->size)];
	while (1) {
		if (i * 2 + 1 >= h->size) break;
		else if (i * 2 + 2 >= h->size) j = i * 2 + 1;
		else if (h->obj[i*2+1].key > h->obj[i*2+2].key) j = i * 2 + 1;
		else j = i * 2 + 2;
		if (h->obj[j].key > h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
		} else break;
	}
	return output;
}

int main()
{
	int i, N, K, P[200002];
	scanf("%d %d", &N, &K);
	for (i = 1; i <= N; i++) scanf("%d", &(P[i]));
	
	int ans = 1;
	data d;
	max_heap h[2];
	h[0].size = 0;
	h[1].size = 0;
	for (i = 1; i <= K; i++) {
		d.key = -P[i];
		d.id = i;
		push(d, &(h[0]));
		d.key *= -1;
		push(d, &(h[1]));
	}
	for (i = 1; i <= N - K; i++) {
		while (h[0].obj[0].id < i) pop(&(h[0]));
		while (h[1].obj[0].id < i) pop(&(h[1]));
		
		if (!(h[0].obj[0].id == i && h[1].obj[0].key < P[i+K])) ans++;
		
		d.key = -P[i+K];
		d.id = i + K;
		push(d, &(h[0]));
		d.key *= -1;
		push(d, &(h[1]));
	}
	
	int seq = 1, tmp = 0;
	for (i = 2, P[N+1] = -1; i <= N + 1; i++) {
		if (P[i-1] < P[i]) seq++;
		else {
			if (seq >= K) tmp++;
			seq = 1;
		}
	}
	
	if (tmp > 1) ans -= tmp - 1;
	printf("%d\n", ans);
	fflush(stdout);
	return 0;
}