Problem p02904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> struct DisjointSparseTable {
  function<T(T, T)> f;
  vector<vector<T>> v;

  DisjointSparseTable(vector<T> &inp, function<T(T, T)> f) : f(f) {
    int n = inp.size();
    int b;
    for (b = 0; (1 << b) <= inp.size(); ++b)
      ;
    v.assign(b, vector<T>(n));
    for (int i = 0; i < n; ++i)
      v[0][i] = inp[i];
    for (int i = 1; i < b; ++i) {
      int siz = 1 << i;
      for (int j = 0; j < n; j += siz << 1) {
        int t = min(j + siz, n);
        v[i][t - 1] = inp[t - 1];
        for (int k = t - 2; k >= j; --k)
          v[i][k] = f(inp[k], v[i][k + 1]);
        if (t >= n)
          break;
        v[i][t] = inp[t];
        int r = min(t + siz, n);
        for (int k = t + 1; k < r; ++k)
          v[i][k] = f(v[i][k - 1], inp[k]);
      }
    }
  }

  T get(int l, int r) {
    if (l >= --r)
      return v[0][l];
    int p = 31 - __builtin_clz(l ^ r);
    return f(v[p][l], v[p][r]);
  }
};

signed main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  DisjointSparseTable<int> d_max(a, [](auto x, auto y) { return max(x, y); });
  DisjointSparseTable<int> d_min(a, [](auto x, auto y) { return min(x, y); });

  vector<int> ma(n), mi(n);
  iota(ma.begin(), ma.end(), 1);
  iota(mi.begin(), mi.end(), 0);
  for (int i = 0; i < n - 1; ++i)
    if (a[i] < a[i + 1])
      mi[i + 1] = mi[i];
  for (int i = n - 2; i >= 0; --i)
    if (a[i] < a[i + 1])
      ma[i] = ma[i + 1];

  set<pair<int, int>> v;
  bool fl = false;
  for (int i = 0; i < n - k + 1; ++i) {
    int l = i, r = i + k;
    int ma_p = d_max.get(l, r);
    int mi_p = d_min.get(l, r);
    if (r <= ma[l]) {
      fl = true;
      continue;
    }
    auto b = a;
    sort(next(b.begin(), i), next(b.begin(), i + k));
    if (l && a[l - 1] < mi_p)
      l = mi[l - 1];
    if (r != n && ma_p < a[r])
      r = ma[r];
    v.emplace(l, r);
  }
  cout << v.size() + fl << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> struct DisjointSparseTable {
  function<T(T, T)> f;
  vector<vector<T>> v;

  DisjointSparseTable(vector<T> &inp, function<T(T, T)> f) : f(f) {
    int n = inp.size();
    int b;
    for (b = 0; (1 << b) <= inp.size(); ++b)
      ;
    v.assign(b, vector<T>(n));
    for (int i = 0; i < n; ++i)
      v[0][i] = inp[i];
    for (int i = 1; i < b; ++i) {
      int siz = 1 << i;
      for (int j = 0; j < n; j += siz << 1) {
        int t = min(j + siz, n);
        v[i][t - 1] = inp[t - 1];
        for (int k = t - 2; k >= j; --k)
          v[i][k] = f(inp[k], v[i][k + 1]);
        if (t >= n)
          break;
        v[i][t] = inp[t];
        int r = min(t + siz, n);
        for (int k = t + 1; k < r; ++k)
          v[i][k] = f(v[i][k - 1], inp[k]);
      }
    }
  }

  T get(int l, int r) {
    if (l >= --r)
      return v[0][l];
    int p = 31 - __builtin_clz(l ^ r);
    return f(v[p][l], v[p][r]);
  }
};

signed main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  DisjointSparseTable<int> d_max(a, [](auto x, auto y) { return max(x, y); });
  DisjointSparseTable<int> d_min(a, [](auto x, auto y) { return min(x, y); });

  vector<int> ma(n), mi(n);
  iota(ma.begin(), ma.end(), 1);
  iota(mi.begin(), mi.end(), 0);
  for (int i = 0; i < n - 1; ++i)
    if (a[i] < a[i + 1])
      mi[i + 1] = mi[i];
  for (int i = n - 2; i >= 0; --i)
    if (a[i] < a[i + 1])
      ma[i] = ma[i + 1];

  set<pair<int, int>> v;
  bool fl = false;
  for (int i = 0; i < n - k + 1; ++i) {
    int l = i, r = i + k;
    int ma_p = d_max.get(l, r);
    int mi_p = d_min.get(l, r);
    if (r <= ma[l]) {
      fl = true;
      continue;
    }
    if (l && a[l - 1] < mi_p)
      l = mi[l - 1];
    if (r != n && ma_p < a[r])
      r = ma[r];
    v.emplace(l, r);
  }
  cout << v.size() + fl << endl;
}