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 Julia (0.5.0) (judge verdict: Wrong Answer)

parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function root(uf::Array{Int,1},x)
	s=x
	while uf[s]!=s
		s=uf[s]
	end
	s
end
function same(uf::Array{Int,1},x,y)
	root(uf,x)==root(uf,y)?true:false
end
function unite(uf::Array{Int,1},x,y)
	l = root(uf,x)
	r = root(uf,y)
	if l < r
		uf[r] = l
	elseif l > r
		uf[l] = r
	end
end

function evalma(st::Array{Int,1},ll,rr)
	r = 0
	while 2^r<length(st)
		r += 1
	end
	r -= 1
	hd = 2^r-1
	sl = hd+ll
	sr = hd+rr
	v = max(st[sl],st[sr])
	for i in r:-1:1
		if sr>>i-sl>>i>1
			if (sl>>i)%2==0
				v=max(v,st[sl>>i+1])
			end
			if (sr>>i)%2==1
				v = max(v,st[sr>>i-1])
			end
		end
	end
	v
end

function evalmi(st::Array{Int,1},ll,rr)
	r = 0
	while 2^r<length(st)
		r += 1
	end
	r -= 1
	hd = 2^r-1
	sl = hd+ll
	sr = hd+rr
	v = min(st[sl],st[sr])
	for i in r:-1:1
		if sr>>i-sl>>i>1
			if (sl>>i)%2==0
				v=min(v,st[sl>>i+1])
			end
			if (sr>>i)%2==1
				v = min(v,st[sr>>i-1])
			end
		end
	end
	v
end


function main()
	n,k = readline() |> split |> parseMap
	p = readline() |> split |> parseMap
	ve = zeros(Int,n)
	for i in 2:n
		ve[i] = ve[i-1]
		if p[i]<p[i-1]
			ve[i] += 1
		end
	end
	r = 0
	while 2^r<=n
		r += 1
	end
	hd = 2^r-1
	stma = zeros(Int,2^(r+1))
	stmi = 200000*ones(Int,2^(r+1))
	for i in 1:n
		for j in 0:r
			stma[(hd+i)>>j] = max(stma[(hd+i)>>j],p[i])
			stmi[(hd+i)>>j] = min(stmi[(hd+i)>>j],p[i])
		end
	end
	uf = Int[i for i in 1:n-k+2]
	f = 1
	if ve[k] == 0
		f = 0
		unite(uf,1,n-k+2)
	end
	for i in 2:n-k+1
		if ve[i+k-1]-ve[i]==0
			f = 0
			unite(uf,i,n-k+2)
		else
			ma = evalma(stma,i,i+k-2)
			mi = evalmi(stmi,i,i+k-2)
			if p[i-1]<mi&&p[i+k-1]>ma
				unite(uf,i,i-1)
			end
		end
	end
	println(length(Set(uf))-f)
end

main()