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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define RREP(i, n) RFOR(i, n, 0)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << "[L " << __LINE__ << "] " << #x << " = " << (x) << "\\n";

const int INF = 1e9;
const int _N = 200000;
const int _M = 270000;

struct SEG { // min
  int d[_M * 2];
  int m;
  void init(int m) {
    this->m = m;
    REP(i, m * 2 - 1) d[i] = INF;
  }
  void update(int i, int x) {
    i += m - 1;
    d[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      d[i] = min(d[i * 2 + 1], d[i * 2 + 2]);
    }
  }
  int query(int a, int b) { return _query(a, b, 0, 0, m); }

private:
  int _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return d[k];
    int vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int n, d;
int sizes[_N]; // sizes exist
// map<int, int> sizeRef; // [size] = index

SEG xms, xMs, yms, yMs;
typedef pair<int, PII> Star;
Star stars[_N];
bool comp(const Star &a, const Star &b) { return a.first < b.first; }
int last = -1, si = -1;
int xm = INF, xM, ym = INF, yM;
void calc() {
  sizes[si] = last;
  //	sizeRef[last] = si;
  xms.update(si, xm);
  xMs.update(si, -xM);
  yms.update(si, ym);
  yMs.update(si, -yM);
  xm = INF, xM = 0, ym = INF, yM = 0;
}
// O(N log N)
int main() {
  omajinai;
  cin >> n >> d;

  // segment tree initial
  int m = 1;
  while (m < n)
    m *= 2;
  xms.init(m);
  xMs.init(m);
  yms.init(m);
  yMs.init(m);
  //

  REP(i, n) {
    int x, y, b;
    cin >> x >> y >> b;
    stars[i] = MP(b, MP(x, y));
  }
  sort(stars, stars + n, comp);
  REP(i, n) {
    int b = stars[i].first, x = stars[i].second.first,
        y = stars[i].second.second;
    if (b != last) {
      if (si > -1)
        calc();
      si++;
      last = b;
    }
    xm = min(xm, x);
    xM = max(xM, x);
    ym = min(ym, y);
    yM = max(yM, y);
  }
  calc();

  si++;       // to use as sizes' size
  ll ans = 0; // max = 2e6 ^ 2 = 4e12
  REP(i, si) {
    int ed = upper_bound(sizes, sizes + si, sizes[i] + d) - sizes;
    xm = xms.query(i, ed);
    xM = -xMs.query(i, ed);
    ym = yms.query(i, ed);
    yM = -yMs.query(i, ed);
    ans = max(ans, (ll)(xM - xm) * (yM - ym));
    if (ed == si)
      break;
  }
  cout << ans << endl;
}

Error: 0
[L 92] xm = 1
[L 92] xm = 2
[L 92] xm = 5
[L 92] xm = 3
[L 92] xm = 6
[L 92] xm = 5
[L 92] xm = 1


Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define RREP(i, n) RFOR(i, n, 0)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << "[L " << __LINE__ << "] " << #x << " = " << (x) << "\\n";

const int INF = 1e9;
const int _N = 200000;
const int _M = 270000;

struct SEG { // min
  int d[_M * 2];
  int m;
  void init(int m) {
    this->m = m;
    REP(i, m * 2 - 1) d[i] = INF;
  }
  void update(int i, int x) {
    i += m - 1;
    d[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      d[i] = min(d[i * 2 + 1], d[i * 2 + 2]);
    }
  }
  int query(int a, int b) { return _query(a, b, 0, 0, m); }

private:
  int _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return d[k];
    int vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int n, d;
int sizes[_N];         // sizes exist
map<int, int> sizeRef; // [size] = index

SEG xms, xMs, yms, yMs;
typedef pair<int, PII> Star;
Star stars[_N];
bool comp(const Star &a, const Star &b) { return a.first < b.first; }
int last = -1, si = -1;
int xm = INF, xM, ym = INF, yM;
void calc() {
  sizes[si] = last;
  sizeRef[last] = si;
  dump(xm);
  xms.update(si, xm);
  xMs.update(si, -xM);
  yms.update(si, ym);
  yMs.update(si, -yM);
  xm = INF, xM = 0, ym = INF, yM = 0;
}

int main() {
  omajinai;
  cin >> n >> d;

  // segment tree initial
  int m = 1;
  while (m < n)
    m *= 2;
  xms.init(m);
  xMs.init(m);
  yms.init(m);
  yMs.init(m);
  //

  REP(i, n) {
    int x, y, b;
    cin >> x >> y >> b;
    stars[i] = MP(b, MP(x, y));
  }
  sort(stars, stars + n, comp);
  REP(i, n) {
    int b = stars[i].first, x = stars[i].second.first,
        y = stars[i].second.second;
    if (b != last) {
      if (si > -1)
        calc();
      si++;
      last = b;
    }
    xm = min(xm, x);
    xM = max(xM, x);
    ym = min(ym, y);
    yM = max(yM, y);
  }
  calc();

  si++; // to use as sizes' size
  int ans = 0;
  REP(i, n) {
    int b = stars[i].first;
    int st = sizeRef[b];
    int ed = upper_bound(sizes, sizes + si, b + d) - sizes;
    xm = xms.query(st, ed);
    xM = -xMs.query(st, ed);
    ym = yms.query(st, ed);
    yM = -yMs.query(st, ed);
    ans = max(ans, (xM - xm) * (yM - ym));
  }
  cout << ans << endl;
}