Language: C++

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

template <typename T, class F=function<T(const T&, const T&)> >
class SparseTable {
    public:
        int n;
        vector<vector<T> > mat;
        F func;

    SparseTable(const vector<T>&a, const F& f): func(f) {
        n = static_cast<int>(a.size());
        int max_log = 32-__builtin_clz(n);
        mat.resize(max_log);
        mat[0] = a;
        for (int j=1; j<max_log; j++) {
            mat[j].resize(n-(1<<j)+1);
            for (int i=0; i<=n-(1<<j); i++) {
                mat[j][i] = func(mat[j-1][i], mat[j-1][i+(1<<(j-1))]);
            }
        }
    }

    T get(int from, int to) const {
        assert(0<=from && from<=to && to<=n-1);
        int lg = 32-__builtin_clz(to-from+1)-1;
        return func(mat[lg][from], mat[lg][to-(1<<lg)+1]);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k; cin >> n >> k;
    vector<int> p(n); REP(i, n) cin >> p[i];
    SparseTable<int> smin(p, [&](int i, int j) {return min(i, j); });
    SparseTable<int> smax(p, [&](int i, int j) {return max(i, j); });
    vector<int> bad(n-1), pref(n, 0);
    for (int i=0; i<n-1; i++) bad[i] = (p[i]>p[i+1]);
    for (int i=0; i<n-1; i++) pref[i+1] = pref[i] + bad[i];
    int ans=0, nosort=0;
    for (int i=0; i<=n-k; i++) {
        if (pref[i+k-1] - pref[i] == 0) {
            nosort = 1;  // kの範囲がすでに昇順なら
            continue;
        }
        if (i>0 && smin.get(i-1, i+k-2)==p[i-1] && smax.get(i, i+k-1)==p[i+k-1]) continue;
        else ans++;
    }
    cout << ans + nosort << '\n';
    return 0;
}


Compiler output:
a.cc:37:31: error: 'function' does not name a type; did you mean 'union'?
   37 | template <typename T, class F=function<T(const T&, const T&)> >
      |                               ^~~~~~~~
      |                               union
a.cc:37:39: error: expected '>' before '<' token
   37 | template <typename T, class F=function<T(const T&, const T&)> >
      |                                       ^
a.cc: In member function 'T SparseTable<T, F>::get(int, int) const':
a.cc:58:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   58 |         assert(0<=from && from<=to && to<=n-1);
      |         ^~~~~~
a.cc:58:9: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: In function 'int main()':
a.cc:69:20: error: template argument 2 is invalid
   69 |     SparseTable<int> smin(p, [&](int i, int j) {return min(i, j); });
      |                    ^
a.cc:69:68: error: expression list treated as compound expression in initializer [-fpermissive]
   69 |     SparseTable<int> smin(p, [&](int i, int j) {return min(i, j); });
      |                                                                    ^
a.cc:69:30: error: cannot convert 'main()::<lambda(int, int)>' to 'int' in initialization
   69 |     SparseTable<int> smin(p, [&](int i, int j) {return min(i, j); });
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                              |
      |                              main()::<lambda(int, int)>
a.cc:70:20: error: template argument 2 is invalid
   70 |     SparseTable<int> smax(p, [&](int i, int j) {return max(i, j); });
      |                    ^
a.cc:70:68: error: expression list treated as compound expression in initializer [-fpermissive]
   70 |     SparseTable<int> smax(p, [&](int i, int j) {return max(i, j); });
      |                                                                    ^
a.cc:70:30: error: cannot convert 'main()::<lambda(int, int)>' to 'int' in initialization
   70 |     SparseTable<int> smax(p, [&](int i, int j) {return max(i, j); });
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                              |
      |                              main()::<lambda(int, int)>
a.cc:80:25: error: request for member 'get' in 'smin', which is of non-class type 'int'
   80 |         if (i>0 && smin.get(i-1, i+k-2)==p[i-1] && smax.get(i, i+k-1)==p[i+k-1]) continue;
      |                         ^~~
a.cc:80:57: error: request for member 'get' in 'smax', which is of non-class type 'int'
   80 |         if (i>0 && smin.get(i-1, i+k-2)==p[i-1] && smax.get(i, i+k-1)==p[i+k-1]) continue;
      |                                                         ^~~