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

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define in ,
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct Star {
  ll x, y, d;
};

ll minX[400000 * 2 - 1], maxX[400000 * 2 - 1], minY[400000 * 2 - 1],
    maxY[400000 * 2 - 1];
ll n, nn = 1;

void update(ll k, ll a, ll c[]) {
  k += nn - 1;
  c[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;
    c[k] = min(c[k * 2 + 1], c[k * 2 + 2]);
  }
}

ll query(ll a, ll b, ll k, ll l, ll r, ll c[]) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return c[k];

  ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2, c);
  ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r, c);

  return min(vl, vr);
}

bool asc(Star &l, Star &r) { return l.d < r.d; }

int main() {
  ll d, ans = 0;
  cin >> n >> d;
  vector<Star> v(n);

  while (nn < n)
    nn *= 2;

  rep(i, 0, nn * 2 - 1) {
    minX[i] = INT_MAX;
    minY[i] = INT_MAX;
    maxX[i] = INT_MAX;
    minX[i] = INT_MAX;
  }

  rep(i, 0, n) cin >> v[i].x >> v[i].y >> v[i].d;

  sort(v.begin(), v.end(), asc);

  rep(i, 0, n) {
    update(i, v[i].x * -1, maxX);
    update(i, v[i].x, minX);
    update(i, v[i].y * -1, maxY);
    update(i, v[i].y, minY);
  }

  // rep(i, 0, n) cout << v[i].x << " " << v[i].y << " " << v[i].d << endl;;

  // rep(i, 0, nn * 2 - 1) cout << minX[i] << endl;
  ll ld = v[0].d, lp = 0;
  rep(i, 1, n) {
    if (v[i].d - ld > d) {
      rep(j, lp, i + 1) {
        if (v[i].d - v[j].d <= d) {
          lp = j;
          ld = v[lp].d;
          break;
        }
      }
    }
    ll hx = query(lp, i + 1, 0, 0, nn, maxX) * -1;
    ll lx = query(lp, i + 1, 0, 0, nn, minX);
    ll hy = query(lp, i + 1, 0, 0, nn, maxY) * -1;
    ll ly = query(lp, i + 1, 0, 0, nn, minY);
    if (lp != i)
      ans = max(ans, ((hx - lx) * (ll)(hy - ly)));
    // cout << lp << " " << i << endl;
    // cout << hx << " " << lx << " " << hy << " " << ly << endl;
    // cout << ans << endl;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define in ,
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct Star {
  ll x, y, d;
};

ll minX[200001 * 2 - 1] = {}, maxX[200001 * 2 - 1] = {},
                     minY[200001 * 2 - 1] = {}, maxY[200001 * 2 - 1] = {};
ll n, nn = 1;

void update(ll k, ll a, ll c[]) {
  k += nn - 1;
  c[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;
    c[k] = min(c[k * 2 + 1], c[k * 2 + 2]);
  }
}

ll query(ll a, ll b, ll k, ll l, ll r, ll c[]) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return c[k];

  ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2, c);
  ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r, c);

  return min(vl, vr);
}

bool asc(Star &l, Star &r) { return l.d < r.d; }

int main() {
  ll d, ans = 0;
  cin >> n >> d;
  vector<Star> v(n);

  while (nn < n)
    nn *= 2;

  rep(i, 0, nn * 2 - 1) {
    minX[i] = INT_MAX;
    minY[i] = INT_MAX;
    maxX[i] = INT_MAX;
    minX[i] = INT_MAX;
  }

  rep(i, 0, n) cin >> v[i].x >> v[i].y >> v[i].d;

  sort(v.begin(), v.end(), asc);
  rep(i, 0, n) {
    update(i, v[i].x * -1, maxX);
    update(i, v[i].x, minX);
    update(i, v[i].y * -1, maxY);
    update(i, v[i].y, minY);
  }

  // rep(i, 0, n) cout << v[i].x << " " << v[i].y << " " << v[i].d << endl;;

  // rep(i, 0, nn * 2 - 1) cout << minX[i] << endl;

  ll ld = v[0].d, lp = 0;
  rep(i, 1, n) {
    if (v[i].d - ld > d) {
      rep(j, lp, i + 1) {
        if (v[i].d - v[j].d <= d) {
          lp = j;
          ld = v[lp].d;
          break;
        }
      }
    }
    ll hx = query(lp, i + 1, 0, 0, nn, maxX) * -1;
    ll lx = query(lp, i + 1, 0, 0, nn, minX);
    ll hy = query(lp, i + 1, 0, 0, nn, maxY) * -1;
    ll ly = query(lp, i + 1, 0, 0, nn, minY);
    if (lp != i)
      ans = max(ans, ((hx - lx) * (ll)(hy - ly)));
    // cout << lp << " " << i << endl;
    // cout << hx << " " << lx << " " << hy << " " << ly << endl;
    // cout << ans << endl;
  }

  cout << ans << endl;
}