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)

#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif


template<typename T>
class SegTree{
private:
    T n;
    vector<T> node;

public:
    void init(T sz){
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n - 1, INTINF);

        // 最下段を初期化
        // それより上を比べて初期化
        for(int i=0; i<n; i++) node[i+n-1] = 0;
        for(int i=n-2; i>=0; i--) node[i] = min(node[i*2+1], node[i*2+2]);
    }

    void update(int k, T val){
        k += n-1;

        node[k] = val;
        while(k > 0){
            k = (k - 1) / 2;
            node[k] = min(node[k*2+1], node[k*2+2]);
        }
    }

    T query(int a, int b, T k=0, int l=0, int r=-1){
        if(r < 0) r = n;

        if(r <= a || b <= l) return INTINF;
        if(a <= l && r <= b) return node[k];

        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return min(vl, vr);
    }
};


template<typename T>
class SegTreeB{
private:
    T n;
    vector<T> node;

public:
    void init(T sz){
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n - 1, INTINF);

        // 最下段を初期化
        // それより上を比べて初期化
        for(int i=0; i<n; i++) node[i+n-1] = INTINF;
        for(int i=n-2; i>=0; i--) node[i] = max(node[i*2+1], node[i*2+2]);
    }

    void update(int k, T val){
        k += n-1;

        node[k] = val;
        while(k > 0){
            k = (k - 1) / 2;
            node[k] = max(node[k*2+1], node[k*2+2]);
        }
    }

    T query(T a, T b, T k=0, int l=0, int r=-1){
        if(r < 0) r = n;

        if(r <= a || b <= l) return -1;
        if(a <= l && r <= b) return node[k];

        T vl = query(a, b, k*2+1, l, (l+r)/2);
        T vr = query(a, b, k*2+2, (l+r)/2, r);
        return max(vl, vr);
    }
};

int main(int argc, const char * argv[]) {
	std::cout << std::fixed << std::setprecision(15);

	int n, k; cin >> n >> k;
	vector<ll> p(n);
	rep(i, n)  cin >> p[i];
	SegTree<ll> seg;
	seg.init(n);
	rep(i, n) seg.update(i, p[i]);
	SegTreeB<ll> Seg;
	Seg.init(n);
	rep(i, n) Seg.update(i, p[i]);


	int ans = 0;
	bool change_l = false;
	rep2(i, 0, n - k + 1){
		// segki
		ll mi = seg.query(i, i + k - 1);
		ll ma = Seg.query(i, i + k - 1);
		// print(mi, ma);

		if(i)
			if(change_l){
				ans++;
			}
			else{
				if(ma != p[i+k-1]){
					ans++;
				}
			}

		change_l = mi == p[i];

	}

	if(n == k)
		cout << 1 << endl;
	else
		cout << ans << endl;

	return 0;
}