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: Runtime Error)

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <cfloat>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

int main(){
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n,k;
  cin>>n>>k;
  std::vector<int> p(n);
  for(int i=0;i<n;i++){
    cin>>p[i];
  }

  std::set<int> range_value;
  std::unordered_multiset<int> pos;
  for(int i=0;i<k;i++){
    range_value.insert(p[i]);
  }
  int result=1;
  pos.insert(k-1);
  for(int i=k;i<n;i++){
    int curr_min=*range_value.begin();
    int curr_max=*(--range_value.end());
    int next_value=p[i];
    range_value.insert(next_value);
    range_value.erase(p[i-k]);
    if(curr_min<*range_value.begin()&&curr_max<*(--range_value.end())){
      //nothing
    }else{
      pos.insert(i);
      result++;
    }
  }

  int cum[200001]={};
  int increase_len=0;
  int max=-1;
  for(int i=0;i<n;i++){
    if(max<p[i]){
      max=p[i];
      increase_len++;
      if(increase_len>=k){
        cum[i]=cum[i-1]+1;
      }else{
        cum[i]=cum[i-1];
      }
    }else{
      max=p[i];
      increase_len=1;
      cum[i]=cum[i-1];
    }
  }






  for(int i=1;i<n;i++){
    if(pos.find(i)!=pos.end()&&cum[i-1]<cum[i]){
      if(i+k-1<n){
        result-=cum[n-1]-cum[i+k-1];
      }
    }
  }

  if(result<=0){
    abort();
  }



  cout<<result<<endl;

  return 0;
}