Problem p02904 (C++)

Buggy submission (status: Runtime Error):
/* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```
```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` ``  ` ` ` ` ` ` ` ` `
dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` ``
````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` ``
```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` `  ````````.`````` ``
``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...`````  .,.`  ````....````` ```````
(Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<``
`````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ````````````````
._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- ``````````````````````````````````
._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.``
OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____..  ````````  ..__~~_((<<!.`.``` .``.``
j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```
jOuWHHqHIdH
// gmmqkW!`(_ J>`    `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. `   _ ?&._.I`_``````
.XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}
.`..````jqg@@@@@@
// XHWZ{..<Jk~!.`..
(<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;<
._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~`
_-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
//
kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
//
@@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~```
~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
//
@gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
//
mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
//
qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~``
(jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ````
d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( ``
WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
//
XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<``
(v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.``
:.~+=?~.(;_(...jdQQQQQkkk
//
H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
//
@@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
//
@H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
//
@@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
//
@@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
//
MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
//
HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
//
HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
//
rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%``
(Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ `
_``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>`
~````.OXtt>~._<?MM
*/

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  for (unsigned int i = 0; i < xs.size(); ++i)
    os << (i ? " " : "") << xs[i];
  return os;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
  string name;
  getline(iss, name, ',');
  cout << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}

#ifdef PYONPOI
#define DEBUG(...)                                                             \
  do {                                                                         \
    istringstream ss(#__VA_ARGS__);                                            \
    debug<' '>(ss, __VA_ARGS__);                                               \
    cout << endl;                                                              \
  } while (0)
#else
#define DEBUG
#endif

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

const int MAX_LOG = 18;
VI mn[MAX_LOG];
VI mx[MAX_LOG];
class SparseTable {
private:
  int n;
  VI logTable;

public:
  SparseTable(int n, const VI &xs) : n(n) {
    logTable.resize(n + 1);
    logTable[0] = logTable[1] = 0;
    for (int i = 2; i <= n; ++i)
      logTable[i] = logTable[i >> 1] + 1;

    mn[0] = mx[0] = xs;
    for (int i = 1; i < MAX_LOG; ++i) {
      mn[i].resize(n);
      mx[i].resize(n);
      for (int j = 0; j < n; ++j) {
        mn[i][j] = min(mn[i - 1][j], mn[i - 1][min(n - 1, j + (1 << (i - 1)))]);
        mx[i][j] = max(mx[i - 1][j], mx[i - 1][min(n - 1, j + (1 << (i - 1)))]);
      }
    }
  }

  // RMQ [l,r)
  int query_min(int l, int r) {
    int d = logTable[r - l];
    return min(mn[d][l], mn[d][r - (1 << d)]);
  }
  int query_max(int l, int r) {
    int d = logTable[r - l];
    return max(mx[d][l], mx[d][r - (1 << d)]);
  }
};

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

  int N, K;
  cin >> N >> K;
  VI xs(N);
  cin >> xs;

  REP(i, K + 1) xs.PB(N + i);

  VI ord(N);
  ord.back() = 1;
  for (int i = N - 2; i >= 0; --i) {
    ord[i] = 1;
    if (xs[i] < xs[i + 1])
      ord[i] += ord[i + 1];
  }

  SparseTable sp(SZ(xs), xs);

  int ans = 0, nop = 0;
  for (int i = 0; i + K <= N;) {
    int k = 1;
    for (; k < K; ++k) {
      if (xs[i + k - 1] == sp.query_min(i + k - 1, i + k + K - 1) &&
          sp.query_max(i, i + k + K) == xs[i + k + K - 1]) {
      } else
        break;
    }

    if (ord[i] >= K)
      ++nop;
    else
      ++ans;

    i += k;
  }
  mini(nop, 1);
  DEBUG(nop);
  ans += nop;
  cout << ans << endl;

  return 0;
}

Fixed submission:
/* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```
```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` ``  ` ` ` ` ` ` ` ` `
dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` ``
````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` ``
```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` `  ````````.`````` ``
``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...`````  .,.`  ````....````` ```````
(Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<``
`````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ````````````````
._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- ``````````````````````````````````
._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.``
OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____..  ````````  ..__~~_((<<!.`.``` .``.``
j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```
jOuWHHqHIdH
// gmmqkW!`(_ J>`    `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. `   _ ?&._.I`_``````
.XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}
.`..````jqg@@@@@@
// XHWZ{..<Jk~!.`..
(<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;<
._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~`
_-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
//
kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
//
@@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~```
~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
//
@gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
//
mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
//
qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~``
(jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ````
d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( ``
WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
//
XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<``
(v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.``
:.~+=?~.(;_(...jdQQQQQkkk
//
H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
//
@@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
//
@H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
//
@@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
//
@@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
//
MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
//
HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
//
HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
//
rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%``
(Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ `
_``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>`
~````.OXtt>~._<?MM
*/

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  for (unsigned int i = 0; i < xs.size(); ++i)
    os << (i ? " " : "") << xs[i];
  return os;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
  string name;
  getline(iss, name, ',');
  cout << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}

#ifdef PYONPOI
#define DEBUG(...)                                                             \
  do {                                                                         \
    istringstream ss(#__VA_ARGS__);                                            \
    debug<' '>(ss, __VA_ARGS__);                                               \
    cout << endl;                                                              \
  } while (0)
#else
#define DEBUG
#endif

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

const int MAX_LOG = 20;
VI mn[MAX_LOG];
VI mx[MAX_LOG];
class SparseTable {
private:
  int n;
  VI logTable;

public:
  SparseTable(int n, const VI &xs) : n(n) {
    logTable.resize(n + 1);
    logTable[0] = logTable[1] = 0;
    for (int i = 2; i <= n; ++i)
      logTable[i] = logTable[i >> 1] + 1;

    mn[0] = mx[0] = xs;
    for (int i = 1; i < MAX_LOG; ++i) {
      mn[i].resize(n);
      mx[i].resize(n);
      for (int j = 0; j < n; ++j) {
        mn[i][j] = min(mn[i - 1][j], mn[i - 1][min(n - 1, j + (1 << (i - 1)))]);
        mx[i][j] = max(mx[i - 1][j], mx[i - 1][min(n - 1, j + (1 << (i - 1)))]);
      }
    }
  }

  // RMQ [l,r)
  int query_min(int l, int r) {
    int d = logTable[r - l];
    return min(mn[d][l], mn[d][r - (1 << d)]);
  }
  int query_max(int l, int r) {
    int d = logTable[r - l];
    return max(mx[d][l], mx[d][r - (1 << d)]);
  }
};

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

  int N, K;
  cin >> N >> K;
  VI xs(N);
  cin >> xs;

  REP(i, K + 1) xs.PB(N + i);

  VI ord(N);
  ord.back() = 1;
  for (int i = N - 2; i >= 0; --i) {
    ord[i] = 1;
    if (xs[i] < xs[i + 1])
      ord[i] += ord[i + 1];
  }

  SparseTable sp(SZ(xs), xs);

  int ans = 0, nop = 0;
  for (int i = 0; i + K <= N;) {
    int k = 1;
    for (; k < K; ++k) {
      if (xs[i + k - 1] == sp.query_min(i + k - 1, i + k + K - 1) &&
          sp.query_max(i, i + k + K) == xs[i + k + K - 1]) {
      } else
        break;
    }

    if (ord[i] >= K)
      ++nop;
    else
      ++ans;

    i += k;
  }
  mini(nop, 1);
  DEBUG(nop);
  ans += nop;
  cout << ans << endl;

  return 0;
}