Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 2e5 + 10;
const int MAXM = 5e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;

int A[MAXN];
int dp[MAXN];
bool chk[MAXN];
pii tree[MAXN];
char S[MAXN];

pii getv(pii a, pii b) {
  int mn = min(a.f, b.f);
  int mx = max(a.s, b.s);

  return pii(mn, mx);
}

pii getans(int L, int R) {
  pii res = pii(INF, 0);

  while (L <= R) {
    if (L & 1) {
      res = getv(res, tree[L]);
      L++;
    }
    if (!(R & 1)) {
      res = getv(res, tree[R]);
      R--;
    }
    L >>= 1;
    R >>= 1;
  }

  return res;
}

int main() {
  int n, m, k, a, b, x, y, q;
  int sum = 0;
  int cnt = 0;
  int mx = 0;
  int mn = INF;
  int idx = -1;
  int tc;
  int base = 1;

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;

  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (; base < n; base <<= 1)
    ;

  for (int i = 0; i < n; i++)
    tree[base + i] = pii(A[i], A[i]);

  for (int i = n; i < base; i++)
    tree[base + i] = pii(INF, 0);

  for (int i = base - 1; i >= 1; i--)
    tree[i] = getv(tree[i << 1], tree[(i << 1) | 1]);

  pii cur;

  int res = n - k + 1;

  dp[0] = 1;

  for (int i = 1; i < n; i++)
    if (A[i] > A[i - 1])
      dp[i] = dp[i - 1] + 1;
    else
      dp[i] = 1;

  if (dp[k - 1] >= k)
    chk[k - 1] = true;

  for (int i = 0; i < n - k; i++) {
    cur = getans(base + i, base + i + k);
    if (chk[i])
      cnt++;
    if (dp[i + k] >= k)
      chk[i + k] = true;

    if (cur.f == A[i] && cur.s == A[i + k])
      res--;
    else if (cnt && dp[i + k] >= k)
      res--;
  }

  cout << res << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 2e5 + 10;
const int MAXM = 5e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;

int A[MAXN];
int dp[MAXN];
bool chk[MAXN];
pii tree[4 * MAXN];

pii getv(pii a, pii b) {
  int mn = min(a.f, b.f);
  int mx = max(a.s, b.s);

  return pii(mn, mx);
}

pii getans(int L, int R) {
  pii res = pii(INF, 0);

  while (L <= R) {
    if (L & 1) {
      res = getv(res, tree[L]);
      L++;
    }
    if (!(R & 1)) {
      res = getv(res, tree[R]);
      R--;
    }
    L >>= 1;
    R >>= 1;
  }

  return res;
}

int main() {
  int n, m, k, a, b, x, y, q;
  int sum = 0;
  int cnt = 0;
  int mx = 0;
  int mn = INF;
  int idx = -1;
  int tc;
  int base = 1;

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> k;

  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (; base < n; base <<= 1)
    ;

  for (int i = 0; i < n; i++)
    tree[base + i] = pii(A[i], A[i]);

  for (int i = n; i < base; i++)
    tree[base + i] = pii(INF, 0);

  for (int i = base - 1; i >= 1; i--)
    tree[i] = getv(tree[i << 1], tree[(i << 1) | 1]);

  pii cur;

  int res = n - k + 1;

  dp[0] = 1;

  for (int i = 1; i < n; i++)
    if (A[i] > A[i - 1])
      dp[i] = dp[i - 1] + 1;
    else
      dp[i] = 1;

  if (dp[k - 1] >= k)
    chk[k - 1] = true;

  for (int i = 0; i < n - k; i++) {
    cur = getans(base + i, base + i + k);
    if (chk[i])
      cnt++;
    if (dp[i + k] >= k)
      chk[i + k] = true;

    if (cur.f == A[i] && cur.s == A[i + k])
      res--;
    else if (cnt && dp[i + k] >= k)
      res--;
  }

  cout << res << "\n";

  return 0;
}