Problem p00297 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Rrep(i, m, n) for (int i = (m); i < (n); i++)
#define All(v) v.begin(), v.end()
#define Uniq(v) v.erase(unique(All(v)), v.end())
typedef pair<ll, ll> Pii;
typedef pair<ll, Pii> Pip;
typedef pair<string, int> Psi;
typedef vector<int> Vi;
const int INF = (1 << 30);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int MAX_N = 200001;

ll n, d;
ll datx1[2 * MAX_N - 1];
ll datx2[2 * MAX_N - 1];
ll daty1[2 * MAX_N - 1];
ll daty2[2 * MAX_N - 1];

void init(ll n_, ll dat[]) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;
}

void update(ll k, ll a, ll dat[]) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

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

int main() {
  cin >> n >> d;
  ll N = n;
  init(N + 1, datx1);
  init(N + 1, datx2);
  init(N + 1, daty1);
  init(N + 1, daty2);
  vector<ll> bData;
  vector<Pip> allData;
  for (int i = 0; i < N; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    bData.pb(c);
    allData.pb(Pip(c, Pii(a, b)));
  }

  sort(All(allData));

  for (int i = 0; i < N; i++) {
    ll x = allData[i].sc.fr, y = allData[i].sc.sc;
    update(i, x, datx1);
    update(i, -x, datx2);
    update(i, y, daty1);
    update(i, -y, daty2);
  }

  ll s, t;
  s = t = 0;
  ll ma = 0;
  for (int i = 1; i < N - 1; i++) {
    t = i;
    while (1) {
      if (allData[t].fr - allData[s].fr <= d)
        break;
      s++;
    }
    ll x1 = query(s, t + 1, 0, 0, n, datx1);
    ll x2 = query(s, t + 1, 0, 0, n, datx2);
    ll y1 = query(s, t + 1, 0, 0, n, daty1);
    ll y2 = query(s, t + 1, 0, 0, n, daty2);

    ll tmp = ((-x2) - x1) * ((-y2) - y1);
    if (tmp > ma) {
      ma = tmp;
    }
  }

  cout << ma << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Rrep(i, m, n) for (int i = (m); i < (n); i++)
#define All(v) v.begin(), v.end()
#define Uniq(v) v.erase(unique(All(v)), v.end())
typedef pair<ll, ll> Pii;
typedef pair<ll, Pii> Pip;
typedef pair<string, int> Psi;
typedef vector<int> Vi;
const int INF = (1 << 30);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int MAX_N = 2000001;

ll n, d;
ll datx1[2 * MAX_N - 1];
ll datx2[2 * MAX_N - 1];
ll daty1[2 * MAX_N - 1];
ll daty2[2 * MAX_N - 1];

void init(ll n_, ll dat[]) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = INT_MAX;
}

void update(ll k, ll a, ll dat[]) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

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

int main() {
  cin >> n >> d;
  ll N = n;
  init(N + 1, datx1);
  init(N + 1, datx2);
  init(N + 1, daty1);
  init(N + 1, daty2);
  vector<ll> bData;
  vector<Pip> allData;
  for (int i = 0; i < N; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    bData.pb(c);
    allData.pb(Pip(c, Pii(a, b)));
  }

  sort(All(allData));

  for (int i = 0; i < N; i++) {
    ll x = allData[i].sc.fr, y = allData[i].sc.sc;
    update(i, x, datx1);
    update(i, -x, datx2);
    update(i, y, daty1);
    update(i, -y, daty2);
  }

  ll s, t;
  s = t = 0;
  ll ma = 0;
  for (int i = 1; i < N - 1; i++) {
    t = i;
    while (1) {
      if (allData[t].fr - allData[s].fr <= d)
        break;
      s++;
    }
    ll x1 = query(s, t + 1, 0, 0, n, datx1);
    ll x2 = query(s, t + 1, 0, 0, n, datx2);
    ll y1 = query(s, t + 1, 0, 0, n, daty1);
    ll y2 = query(s, t + 1, 0, 0, n, daty2);

    ll tmp = ((-x2) - x1) * ((-y2) - y1);
    if (tmp > ma) {
      ma = tmp;
    }
  }

  cout << ma << endl;
}