Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;

#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()

#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v)                                                              \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << v[i] << " ";

#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)

#define INF (ll)1e17
#define matrix vector<vector<ll>>

#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
const int N = (ll)2e3 + 10, M = (ll)1e9 + 1;
const int mod = (ll)1e9 + 7, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";

void work() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  vl pref(n, 0);
  for (ll i = 1; i < n; i++)
    pref[i] = a[i - 1] <= a[i];

  for (ll i = 1; i < n; i++)
    pref[i] += pref[i - 1];

  vl mini(n, 0), maxi(n, 0);

  set<ll> s;

  for (ll i = 0; i < k - 1; i++) {
    s.insert(a[i]);
    // s_maxi.insert(-a[i]);
  }

  for (ll i = k - 1; i < n; i++) {
    if (i >= k)
      s.erase(a[i - k]);
    s.insert(a[i]);

    mini[i - k + 1] = (a[i - k + 1] == *(s.begin()));
    maxi[i] = (a[i] == *(s.rbegin()));
  }

  for (ll i = 0; i < n; i++) {
    //	cout << pref[i] << " ";
  }

  vl issorted(n, 0);
  ll anysorted = 0;
  for (ll i = 0; i < n; i++) {
    if (i - k + 1 >= 0)
      issorted[i] = (pref[i] - pref[i - k + 1] == k - 1);

    anysorted |= issorted[i];
  }

  ll ans = anysorted;
  if (issorted[k - 1] == 0)
    ans++;
  // cout << anysorted << "\n";

  for (ll i = k; i < n; i++) {
    if (issorted[i])
      continue;
    if (mini[i - k] && maxi[i])
      continue;
    // cout << i << " ";
    ans++;
  }

  cout << ans << "\n";
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // to avoid printing in Scientific Notation like 2.14748e+09 and instead
  // print: 2147483648.000000
  cout << fixed;

  // precomp();
  ll t, z;
  // cin >> t;
  t = 1;
  // z=t;
  while (t--) {
    // cout << "Case #" << z-t << ": " << "\n";
    work();
  }

  return 0;
}


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

#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;

#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()

#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v)                                                              \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << v[i] << " ";

#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)

#define INF (ll)1e17
#define matrix vector<vector<ll>>

#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
const int N = (ll)2e3 + 10, M = (ll)1e9 + 1;
const int mod = (ll)1e9 + 7, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";

void work() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  vl pref(n, 0);
  for (ll i = 1; i < n; i++)
    pref[i] = a[i - 1] <= a[i];

  for (ll i = 1; i < n; i++)
    pref[i] += pref[i - 1];

  vl mini(n, 0), maxi(n, 0);

  set<ll> s;

  for (ll i = 0; i < k - 1; i++) {
    s.insert(a[i]);
    // s_maxi.insert(-a[i]);
  }

  for (ll i = k - 1; i < n; i++) {
    if (i >= k)
      s.erase(a[i - k]);
    s.insert(a[i]);

    mini[i - k + 1] = (a[i - k + 1] == *(s.begin()));
    maxi[i] = (a[i] == *(s.rbegin()));
  }

  for (ll i = 0; i < n; i++) {
    //	cout << pref[i] << " ";
  }

  vl issorted(n, 0);
  ll anysorted = 0;
  for (ll i = 0; i < n; i++) {
    if (i - k + 1 >= 0)
      issorted[i] = (pref[i] - pref[i - k + 1] == k - 1);

    anysorted |= issorted[i];
  }

  ll ans = anysorted;
  if (issorted[k - 1] == 0)
    ans++;
  // cout << anysorted << "\n";

  for (ll i = k; i < n; i++) {
    if (issorted[i])
      continue;
    if (mini[i - k] && maxi[i])
      continue;
    // cout << i << " ";
    ans++;
  }

  cout << ans << "\n";
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // to avoid printing in Scientific Notation like 2.14748e+09 and instead
  // print: 2147483648.000000
  cout << fixed;

  // precomp();
  ll t, z;
  // cin >> t;
  t = 1;
  // z=t;
  while (t--) {
    // cout << "Case #" << z-t << ": " << "\n";
    work();
  }

  return 0;
}