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;
#define int long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)
#define itn int
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
struct SegTree{
    int n;
    vector <pair <int,int> > node;
    SegTree(vector <int> v){
        int sz = v.size();
        n = 1;
        while(n<sz) n*=2;
        node.resize(2*n-1, {INF,INF});
        for(int i=0; i<sz; i++) node[i+n-1] = {v[i],i}; //一番下側
        for(int i=n-2;i>=0;i--) node[i] = min(node[2*i+1], node[2*i+2]);
    }
    int getnum(int p){
        return node[n-1+p].F;
    }
    int geti(int p){
        return node[n-1+p].S;
    }
    void update(int number, int val){
        number += n-1;
        node[number].F= val;
        while(number>0){
            number=(number-1)/2;
            node[number] = min(node[number*2+1], node[number*2+2]);
        }
    }
    pair <int,int> getmin(int a, int b, int k=0, int l = 0, int r = -1){
        
        if(r<0) r=n;
        if(r<=a||b<=l) return {INF,INF};
        if(a <= l&& r<=b) return node[k];
        pair <int,int>  vl = getmin(a, b, 2*k+1, l, (l+r)/2);
        pair <int,int>  vr = getmin(a, b, 2*k+2, (l+r)/2, r);
        return min(vl, vr);
    }
    void showall(){
        for(int i=0;i<2*n-1;i++)cout<<node[i].F<<'&'<<node[i].S<<endl;
    }
};
struct maxSegTree{
    int n;
    vector <pair <int,int> > node;
    maxSegTree(vector <int> v){
        int sz = v.size();
        n = 1;
        while(n<sz) n*=2;
        node.resize(2*n-1, {-INF,-INF});
        for(int i=0; i<sz; i++) node[i+n-1] = {v[i],i}; //一番下側
        for(int i=n-2;i>=0;i--) node[i] = max(node[2*i+1], node[2*i+2]);
    }
    int getnum(int p){
        return node[n-1+p].F;
    }
    int geti(int p){
        return node[n-1+p].S;
    }
    void update(int number, int val){
        number += n-1;
        node[number].F= val;
        while(number>0){
            number=(number-1)/2;
            node[number] = max(node[number*2+1], node[number*2+2]);
        }
    }
    pair <int,int> getmax(int a, int b, int k=0, int l = 0, int r = -1){ //0~n
        
        if(r<0) r=n;
        if(r<=a||b<=l) return {-INF,-INF};
        if(a <= l&& r<=b) return node[k];
        pair <int,int>  vl = getmax(a, b, 2*k+1, l, (l+r)/2);
        pair <int,int>  vr = getmax(a, b, 2*k+2, (l+r)/2, r);
        return max(vl, vr);
    }
    void showall(){
        for(int i=0;i<2*n-1;i++)cout<<node[i].F<<'&'<<node[i].S<<endl;
    }
};


//参考:http://tsutaj.hatenablog.com/entry/2017/03/29/204841
signed main(void){
    int n,k; cin>>n>>k;
    vector <int> a(n); 
    rep(i,n) cin>>a[i];
    SegTree seg = SegTree(a);
    maxSegTree mseg = maxSegTree(a);
    int ans = 0;
    bool manma = false;
    map <pair <int,int>, int> ma;
    rep(i,n-k){
        //cout<<i+k<<endl;
        if(a[i] != seg.getmin(i, i+k+1).F) {
            ma[{i, seg.getmin(i, i+k+1).S}] = 1;
        }
        else manma = true;
       //cout<<<<endl;
    }
    //cout<<manma<<endl;
    cout<<2*ma.size()<<endl;
}