Problem p02551 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
// const ll INF = 1e18+3;
const ll mod = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define print(x) cerr << #x << " is " << x << endl;

using DD = ll;
#define maxmin(a, b) max(a, b)
#define INIT_VAL 0

const int MAX_N = 1 << 20;
int n;
DD dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INIT_VAL; // �����l
}

void update(int k, DD a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = maxmin(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
// [a, b)�̍ő�l�����߂�
// �O����́Aa, b, 0, 0, n)�Ƃ��ČĂ� n��2�ׂ̂�

DD query(int a, int b, int k, int l, int r) {

  if (r <= a || b <= l)
    return INIT_VAL;
  if (a <= l && r <= b)
    return dat[k];
  else {
    DD vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    DD vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return maxmin(vl, vr);
  }
}

int n2;
DD dat2[2 * MAX_N - 1];

void init2(int n_) {
  n2 = 1;
  while (n2 < n_)
    n2 *= 2;
  for (int i = 0; i < 2 * n2 - 1; i++)
    dat2[i] = INIT_VAL; // �����l
}

void update2(int k, DD a) {
  k += n2 - 1;
  dat2[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat2[k] = maxmin(dat2[k * 2 + 1], dat2[k * 2 + 2]);
  }
}
// [a, b)�̍ő�l�����߂�
// �O����́Aa, b, 0, 0, n)�Ƃ��ČĂ� n��2�ׂ̂�

DD query2(int a, int b, int k, int l, int r) {

  if (r <= a || b <= l)
    return INIT_VAL;
  if (a <= l && r <= b)
    return dat2[k];
  else {
    DD vl = query2(a, b, k * 2 + 1, l, (l + r) / 2);
    DD vr = query2(a, b, k * 2 + 2, (l + r) / 2, r);
    return maxmin(vl, vr);
  }
}
int D(ll a, ll b, int lb, int mid) {
  if (a == 1) {
    return query2(lb, mid + 1, 0, 0, n) >= b;
  } else {
    return query(lb, mid + 1, 0, 0, n) >= b;
  }
}

int main() {
  ll N, Q;
  cin >> N >> Q;

  n = N + 1;  // global
  n2 = N + 1; // global
  init(n);
  init2(n2);
  ll ans = (N - 2) * (N - 2);
  rep(q, Q) {
    int a, b;
    cin >> a >> b;
    int lb = 0, ub = N;
    while (ub - lb > 1) {

      int mid = (ub + lb) / 2;
      // if (q == 1)
      // fprintf(stderr, "lb = %d, ub = %d, mid = %d,   %d\\n", lb, ub, mid, D(a,
      // b, lb, mid)); fprintf(stderr, "hoge, [%d, %d]\\n", lb, ub);

      if (D(a, b, lb, mid)) {
        ub = mid;
      } else {
        lb = mid;
      }
    }

    if (a == 1) {
      update(b, ub);

    } else {
      update2(b, ub);
    }
    ans -= max(0, ub - 2);
    // fprintf(stderr, "ub = %d, ans = %d\\n", ub, ans);
    // cout << "a = " << a << ", b = " << b << ", ub = " << ub << ", ans = " <<
    // ans << endl;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
// const ll INF = 1e18+3;
const ll mod = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define print(x) cerr << #x << " is " << x << endl;

using DD = int;
#define maxmin(a, b) max(a, b)
#define INIT_VAL 0

const int MAX_N = 1 << 17;
int n;
DD dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INIT_VAL; // �����l
}

void update(int k, DD a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = maxmin(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
// [a, b)�̍ő�l�����߂�
// �O����́Aa, b, 0, 0, n)�Ƃ��ČĂ� n��2�ׂ̂�

DD query(int a, int b, int k, int l, int r) {

  if (r <= a || b <= l)
    return INIT_VAL;
  if (a <= l && r <= b)
    return dat[k];
  else {
    DD vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    DD vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return maxmin(vl, vr);
  }
}

int n2;
DD dat2[2 * MAX_N - 1];

void init2(int n_) {
  n2 = 1;
  while (n2 < n_)
    n2 *= 2;
  for (int i = 0; i < 2 * n2 - 1; i++)
    dat2[i] = INIT_VAL; // �����l
}

void update2(int k, DD a) {
  k += n2 - 1;
  dat2[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat2[k] = maxmin(dat2[k * 2 + 1], dat2[k * 2 + 2]);
  }
}
// [a, b)�̍ő�l�����߂�
// �O����́Aa, b, 0, 0, n)�Ƃ��ČĂ� n��2�ׂ̂�

DD query2(int a, int b, int k, int l, int r) {

  if (r <= a || b <= l)
    return INIT_VAL;
  if (a <= l && r <= b)
    return dat2[k];
  else {
    DD vl = query2(a, b, k * 2 + 1, l, (l + r) / 2);
    DD vr = query2(a, b, k * 2 + 2, (l + r) / 2, r);
    return maxmin(vl, vr);
  }
}
int D(ll a, ll b, int mid) {
  if (a == 1) {
    return query2(0, mid, 0, 0, n) >= b;
  } else {
    return query(0, mid, 0, 0, n) >= b;
  }
}

int main() {
  int N, Q;
  cin >> N >> Q;

  n = N;  // global
  n2 = N; // global
  init(n);
  init(n2);

  ll ans = (N - 2) * (N - 2);
  rep(q, Q) {
    int a, b;
    cin >> a >> b;

    ll lb = 0, ub = N - 1;
    while (ub - lb > 1) {
      ll mid = (ub + lb) / 2;
      if (D(a, b, ub)) {
        ub = mid;
      } else {
        lb = mid;
      }
    }
    // fprintf(stderr, "ub = %d\\n", ub);
    if (a == 1) {
      update(b, ub - 1);

    } else {
      update2(b, ub - 1);
    }
    ans -= ub - 1;
  }
  cout << ans;
  return 0;
}