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>
#include <functional>
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: In member function 'T SparseTable<T, F>::get(int, int) const':
a.cc:59:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   59 |         assert(0<=from && from<=to && to<=n-1);
      |         ^~~~~~
a.cc:59:9: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: In instantiation of 'T SparseTable<T, F>::get(int, int) const [with T = int; F = std::function<int(const int&, const int&)>]':
a.cc:81:28:   required from here
   81 |         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:59:15: error: 'assert' was not declared in this scope
   59 |         assert(0<=from && from<=to && to<=n-1);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:22:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   21 | #include <functional>
  +++ |+#include <cassert>
   22 | using namespace std;