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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector< vector<int> > VVI;
typedef vector< vector<ll> > VVL;
typedef pair<int,int> PII;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound

struct UnionFind{
  VI par;
  UnionFind(int N):par(N){
    rep(i,N) par[i]=i;
  }
  int root(int x){
    if(par[x]==x) return x;
    return par[x]=root(par[x]);
  }
  void unite(int x, int y){
    int rx=root(x);
    int ry=root(y);
    if(rx==ry) return;
    par[rx]=ry;
  }
  bool same(int x, int y){
    int rx=root(x);
    int ry=root(y);
    return rx==ry;
  }
};

int main(){
  int N,K;
  cin>>N>>K;
  VI P(N);
  rep(i,N) cin>>P[i];
  if(N==K){
    cout<<1<<endl;
    return 0;
  }
  set<int> X;
  rep(i,K+1) X.insert(P[i]);
  auto itr=X.begin();
  int A=*itr;
  set<int> X2;
  rep(i,K+1) X2.insert(-P[i]);
  auto itr2=X2.begin();
  int B=*itr2;
  UnionFind uf(N-K+1);
  if(A==P[0]&&B==-P[K]) uf.unite(0,1);
  rep(i,N-K-1){
    X.erase(P[i]);
    X.insert(P[i+K+1]);
    itr=X.begin();
    A=*itr;
    X2.erase(-P[i]);
    X2.insert(-P[i+K+1]);
    itr2=X2.begin();
    B=*itr2;
    if(A==P[i+1]&&B==-P[i+K+1]){
      cout<<A<<B<<endl;
      uf.unite(i+1,i+2);
    }
  }
  int num=1;
  int Z=-1;
  rep(i,N){
    if(i){
      if(P[i]>P[i-1]) num++;
      else if(num>=K){
        num=1;
        if(Z==-1) Z=i-K;
        else uf.unite(i-K,Z);
      }
    }
  }
  if(num>=K) if(Z!=-1) uf.unite(N-K,Z);
  set<int> S;
  rep(i,N-K+1) S.insert(uf.root(i));
  cout<<S.size()<<endl;
}