Problem p00297 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define N 200000
#define F first
#define S second
#define MP(i, j) make_pair((i), (j))
#define int long long
typedef pair<int, int> P;
typedef pair<int, P> Pi;
int cnt;
void init(int n_, int *dat) {
  cnt = 1;
  while (cnt < n_)
    cnt *= 2;
  for (int i = 0; i < 2 * cnt - 1; i++)
    dat[i] = INT_MAX;
}
void update(int k, int a, int *dat) {
  k += cnt - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
int query(int a, int b, int k, int l, int r, int *dat) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int v1 = query(a, b, k * 2 + 1, l, (l + r) / 2, dat);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r, dat);
    return min(v1, vr);
  }
}

int i, j, n, d, ax[2 * N], bx[2 * N], ay[2 * N], by[2 * N], ans;
Pi stars[N];

signed main() {
  cin >> n >> d;
  for (i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    stars[i] = MP(c, MP(a, b));
  }
  sort(stars, stars + n);
  init(n, ax);
  init(n, bx);
  init(n, ay);
  init(n, by);
  for (i = 0; i < n; i++) {
    update(i, stars[i].S.F, ax);
    update(i, -stars[i].S.F, bx);
    update(i, stars[i].S.S, ay);
    update(i, -stars[i].S.S, by);
  }
  i = 0;
  j = 0;
  for (;;) {
    while (j < n) {
      if (stars[j].F - stars[i].F <= d)
        j++;
      else
        break;
    }
    ans = max(ans,
              (-query(i, j, 0, 0, cnt, bx) - query(i, j, 0, 0, cnt, ax)) *
                  (-query(i, j, 0, 0, cnt, by) - query(i, j, 0, 0, cnt, ay)));
    if (j == n)
      break;
    i++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define N 200000
#define F first
#define S second
#define MP(i, j) make_pair((i), (j))
#define int long long
typedef pair<int, int> P;
typedef pair<int, P> Pi;
int cnt;
void init(int n_, int *dat) {
  cnt = 1;
  while (cnt < n_)
    cnt *= 2;
  for (int i = 0; i < 2 * cnt - 1; i++)
    dat[i] = INT_MAX;
}
void update(int k, int a, int *dat) {
  k += cnt - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
int query(int a, int b, int k, int l, int r, int *dat) {
  if (r <= a || b <= l)
    return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int v1 = query(a, b, k * 2 + 1, l, (l + r) / 2, dat);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r, dat);
    return min(v1, vr);
  }
}

int i, j, n, d, ax[3 * N], bx[3 * N], ay[3 * N], by[3 * N], ans;
Pi stars[N];

signed main() {
  cin >> n >> d;
  for (i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    stars[i] = MP(c, MP(a, b));
  }
  sort(stars, stars + n);
  init(n, ax);
  init(n, bx);
  init(n, ay);
  init(n, by);
  for (i = 0; i < n; i++) {
    update(i, stars[i].S.F, ax);
    update(i, -stars[i].S.F, bx);
    update(i, stars[i].S.S, ay);
    update(i, -stars[i].S.S, by);
  }
  i = 0;
  j = 0;
  for (;;) {
    while (j < n) {
      if (stars[j].F - stars[i].F <= d)
        j++;
      else
        break;
    }
    ans = max(ans,
              (-query(i, j, 0, 0, cnt, bx) - query(i, j, 0, 0, cnt, ax)) *
                  (-query(i, j, 0, 0, cnt, by) - query(i, j, 0, 0, cnt, ay)));
    if (j == n)
      break;
    i++;
  }
  cout << ans << endl;
  return 0;
}