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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

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

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
struct edge {
  int to, time, cost;
};
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

const int maxN = 200010;

class RMQ {
private:
  int n;
  P dat[maxN * 4];

  //[a,b),[l,r) //(Max,Min)
  P rec(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return P(-infi, infi);
    if (a <= l && r <= b)
      return P(dat[k].first, dat[k].second);
    else {
      P vl = rec(a, b, 2 * k + 1, l, (l + r) / 2),
        vr = rec(a, b, 2 * k + 2, (l + r) / 2, r);
      return P(std::max(vl.first, vr.first), std::min(vl.second, vr.second));
    }
  }

public:
  void init(int x) {
    n = 1;
    while (n < x)
      n *= 2;
    rep(i, 2 * n - 1) dat[i] = P(-infi, infi);
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = P(a, a);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k].first = std::max(dat[2 * k + 1].first, dat[2 * k + 2].first);
      dat[k].second = std::min(dat[2 * k + 1].second, dat[2 * k + 2].second);
    }
  }

  //[a,b] //(Max,Min)
  P query(int a, int b) { return rec(a, b + 1, 0, 0, n); }
};

struct Star {
  int x, y, b;
};
Star star[200001];
int n, d;
RMQ up, right;

bool comp(const Star &lhs, const Star &rhs) { return lhs.b < rhs.b; }

int main() {
  std::cin >> n >> d;
  rep(i, n) std::cin >> star[i].x >> star[i].y >> star[i].b;
  std::sort(star, star + n, comp);
  ll ans = 0;
  int first = 0;
  up.init(n), right.init(n);
  rep(i, n) {
    up.update(i, star[i].y);
    right.update(i, star[i].x);
    while (star[i].b - star[first].b > d)
      ++first;
    P a = up.query(first, i), b = right.query(first, i);
    ll mul = (ll)(a.first - a.second) * (b.first - b.second);
    ans = std::max(ans, mul);
  }
  std::cout << ans << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

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

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
struct edge {
  int to, time, cost;
};
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

class RMQ {
private:
  int n;
  P dat[200001];

  //[a,b),[l,r) //(Max,Min)
  P rec(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return P(-infi, infi);
    if (a <= l && r <= b)
      return P(dat[k].first, dat[k].second);
    else {
      P vl = rec(a, b, 2 * k + 1, l, (l + r) / 2),
        vr = rec(a, b, 2 * k + 2, (l + r) / 2, r);
      return P(std::max(vl.first, vr.first), std::min(vl.second, vr.second));
    }
  }

public:
  void init(int x) {
    n = 1;
    while (n < x)
      n *= 2;
    rep(i, 2 * n - 1) dat[i] = P(-infi, infi);
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = P(a, a);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k].first = std::max(dat[2 * k + 1].first, dat[2 * k + 2].first);
      dat[k].second = std::min(dat[2 * k + 1].second, dat[2 * k + 2].second);
    }
  }

  //[a,b] //(Max,Min)
  P query(int a, int b) { return rec(a, b + 1, 0, 0, n); }
};

struct Star {
  int x, y, b;
};
Star star[200001];
int n, d;
RMQ up, right;

bool comp(const Star &lhs, const Star &rhs) { return lhs.b < rhs.b; }

int main() {
  std::cin >> n >> d;
  rep(i, n) std::cin >> star[i].x >> star[i].y >> star[i].b;
  std::sort(star, star + n, comp);
  int ans = 0;
  int first = 0;
  up.init(n), right.init(n);
  rep(i, n) {
    up.update(i, star[i].y);
    right.update(i, star[i].x);
    while (star[i].b - star[first].b > d)
      ++first;
    P a = up.query(first, i), b = right.query(first, i);
    ans = std::max(ans, (a.first - a.second) * (b.first - b.second));
  }
  std::cout << ans << std::endl;
  return 0;
}