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>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (int i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 2e5, lgsz = 18;
bool f;
int n, k, a[nsz + 5], ans = 1;

struct sparse_table {
    int mn[nsz + 5][lgsz + 5], mx[nsz + 5][lgsz + 5], lg[nsz + 5];
    
    void inline init() {
        cont (i, n) {
            lg[i] = lg[i >> 1] + 1;
            mn[i][0] = mx[i][0] = a[i];
        }
        cont (k, lgsz) {
            cont (u, n) {
                if (u + (1 << k) - 1 > n)  break;
                int p = u + (1 << (k - 1));
                mn[u][k] = min(mn[u][k - 1], mn[p][k - 1]);
                mx[u][k] = max(mx[u][k - 1], mx[p][k - 1]);
            }
        }
    }
    
    int inline qry_min(int l, int r) {
        int k = lg[r - l + 1] - 1, len = 1 << k;
        return min(mn[l][k], mn[r - len + 1][k]);
    }
    
    int inline qry_max(int l, int r) {
        int k = lg[r - l + 1] - 1, len = 1 << k;
        return max(mx[l][k], mx[r - len + 1][k]);
    }
};
sparse_table st;

struct segment {
    bool g;
    int l, r;
    
    segment() {}
    
    segment(int a) {
        l = r = a;
        g = 1;
    }
};
segment inline operator & (segment ls, segment rs) {
    segment u;
    u.g = ls.g & rs.g & (ls.r <= rs.l);
    u.l = ls.l, u.r = rs.r;
    return u;
}

struct segment_tree {
    segment tr[4 * nsz + 5];
    
    void init(int u = 1, int l = 1, int r = n) {
        if (l == r) {
            tr[u] = segment(a[l]);
            return;
        }
        int md = (l + r) >> 1;
        init(u << 1, l, md);
        init(u << 1 | 1, md + 1, r);
        tr[u] = tr[u << 1] & tr[u << 1 | 1];
    }
    
    segment qry(int el, int er, int u = 1, int l = 1, int r = n) {
        if (l >= el && r <= er)  return tr[u];
        bool f = 0;
        int md = (l + r) >> 1;
        segment res;
        if (el <= md) {
            res = qry(el, er, u << 1, l, md);
            f = 1;
        }
        if (er >= md + 1) {
        	segment tmp = qry(el, er, u << 1 | 1, md + 1, r);
            if (f) {
                res = res & tmp;
            } else {
                res = tmp;
            }
        }
        return res;
    }
};
segment_tree tr;

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n >> k;
    cont (i, n) {
        cin >> a[i];
    }
    st.init();
    tr.init();
    f = tr.qry(1, 1 + k - 1).g;
    for (int l = 2, r = l + k - 1; r <= n; ++l, ++r) {
        int l1 = st.qry_min(l - 1, r - 1), r1 = st.qry_max(l - 1, r - 1), l2 = st.qry_min(l, r), r2 = st.qry_max(l, r);
        if (l1 != a[l - 1] || r2 != a[r]) {
        	++ans;
        }
        bool tmp = tr.qry(l, r).g;
        ans -= tmp & f;
        f |= tmp;
    }
    cout << ans << ln;
}