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 <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=2e5+2;
int n,k,p[N],f[N];

template <typename _Tp> inline void IN(_Tp&x) {
    char ch;bool flag=0;x=0;
    while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1 ;
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    if(flag) x=-x;
}

struct Segment_Tree {
    #define mid ((l+r)>>1)
    int Mi[N<<2],Mx[N<<2];
    void build(int x,int l,int r) {
        if(l==r) {Mi[x]=Mx[x]=p[l];return;}
        build(x<<1,l,mid),build(x<<1|1,mid+1,r);
        Mi[x]=min(Mi[x<<1],Mi[x<<1|1]),Mx[x]=max(Mx[x<<1],Mx[x<<1|1]);
    }
    int query_Mi(int x,int l,int r,int L,int R) {
        if(L==l&&r==R) return Mi[x];
        if(R<=mid) return query_Mi(x<<1,l,mid,L,R);
        else if(L>mid) return query_Mi(x<<1|1,mid+1,r,L,R);
        else return min(query_Mi(x<<1,l,mid,L,mid),query_Mi(x<<1|1,mid+1,r,mid+1,R));
    }
    int query_Mx(int x,int l,int r,int L,int R) {
        if(L==l&&r==R) return Mx[x];
        if(R<=mid) return query_Mx(x<<1,l,mid,L,R);
        else if(L>mid) return query_Mx(x<<1|1,mid+1,r,L,R);
        else return max(query_Mx(x<<1,l,mid,L,mid),query_Mx(x<<1|1,mid+1,r,mid+1,R));
    }
}T;

int main() {
    IN(n),IN(k);
    for(int i=1;i<=n;++i) IN(p[i]);
    T.build(1,1,n);
    int Mi=T.query_Mi(1,1,n,1,k),Mx=T.query_Mx(1,1,n,1,k),cnt=1;
    f[1]=1;
    bool flag=false;
    for(int i=1;i<=n;++i)
        f[i]=(p[i-1]<p[i])?f[i-1]+1:1,
        flag|=((f[i]>=k)&&i>k);
    if(flag) ++cnt;
    for(int r=k+1;r<=n;++r) {
        int now_Mi=T.query_Mi(1,1,n,r-k+1,r),now_Mx=T.query_Mx(1,1,n,r-k+1,r);
        if(!(Mi==p[r-k]&&now_Mx==p[r])&&f[r]<k) ++cnt;
        Mi=now_Mi,Mx=now_Mx;
    }
    printf("%d\n",cnt);
    return 0;
}