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;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define ll long long
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using pii = pair<int, int>;
// template<class T> ostream &operator<<(ostream &os,T &t){dump(t);return os;}
template <typename T, typename S>istream &operator>>(istream &is, pair<T, S> &p) { is >> p.first >> p.second; return is; }
//template <typename T, typename S>ostream &operator<<(ostream &os, pair<T, S> &p) {os << p.first << " " << p.second;return os;}

template <typename T> void printvv(const vector<vector<T>> &v) {
	cerr << endl;
	rep(i, 0, v.size()) rep(j, 0, v[i].size()) {
		if (typeid(v[i][j]).name() == typeid(INF).name() and v[i][j] == INF) {
			cerr << "INF";
		}
		else
			cerr << v[i][j];
		cerr << (j == v[i].size() - 1 ? '\n' : ' ');
	}
	cerr << endl;
}

#ifndef _DEBUG
#define printvv(...)
#endif
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T, class U>bool chmin(T& a, U b) { if (a > b) { a = b; return true; }return false; }
template <class T, class U>bool chmax(T& a, U b) { if (a < b) { a = b; return true; }return false; }
template< typename Monoid >
struct SegmentTree {
	using F = function< Monoid(Monoid, Monoid) >;

	int sz;
	vector< Monoid > seg;

	const F f;
	const Monoid e;

	SegmentTree(int n, const F f, const Monoid &e) : f(f), e(e) {
		sz = 1;
		while (sz < n) sz <<= 1;
		seg.assign(2 * sz, e);
	}
	// k:=[0,sz)
	void set(int k, const Monoid &x) {
		seg[k + sz] = x;
	}

	void build() {
		for (int k = sz - 1; k > 0; k--) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}
	// k:=[0,sz)
	void update(int k, const Monoid &x) {
		k += sz;
		seg[k] = x;
		while (k >>= 1) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}
	// [a,b)
	Monoid query(int a, int b) {
		Monoid L = e, R = e;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (a & 1) L = f(L, seg[a++]);
			if (b & 1) R = f(seg[--b], R);
		}
		return f(L, R);
	}
	// k:=[0,sz)
	const Monoid &operator[](const int &k) const {
		return seg[k + sz];
	}
	Monoid &operator[](const int &k) {
		return seg[k + sz];
	}
};

int guchoku(int N, int K, vector<int>P) {
	set<vector<int>>s;
	rep(i, 0, N - K + 1) {
		auto p = P;
		sort(p.begin() + i, p.begin() + i + K);
		if (s.count(p)) {
			cout << "! ";
		}
		s.insert(p);
		dump(p);
		
	}
	dump(s.size());
	return s.size();
}

void solve(int N, int K, vector<int>P) {
	SegmentTree<int>m(N, [](int a, int b) {return min(a, b); }, INF);
	SegmentTree<int>M(N, [](int a, int b) {return max(a, b); }, -INF);
	rep(i, 0, N) {
		m[i] = P[i];
		M[i] = P[i];
	}
	m.build();
	M.build();

	int ans = 1;
	rep(i, 1, N - K + 1) {
		bool f = P[i - 1] == m.query(i - 1, i - 1 + K);
		bool g = P[i + K - 1] == M.query(i, i + K);

		if (not (f and g))ans++;
	}

	deque<pii>q;
	int cnt = 0;
	rep(i, 0, N) {
		dump(q);
		while (q.size() and q.back().first > P[i]) {
			q.pop_back();
		}
		q.push_back(pii(P[i], i));
		cnt += q.size() == K;

		if (q.front().second <= i - K + 1)q.pop_front();

	}
	dump(cnt);
	cnt = max(0ll, cnt - 1);
	ans -= cnt;
	dump(ans);
	//assert(ans == guchoku(N, K, P));
	cout << ans << endl;
}

signed main(signed argc, char *argv[]) {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(12);

	int N, K; cin >> N >> K;
	vector<int>P(N); rep(i, 0, N) { cin >> P[i]; }
	solve(N, K, P);

	return 0;
}