Problem p00297 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct SegTree {
  static const int N = 1 << 18;
  int *dat;

  SegTree() {
    dat = new int[N * 2 - 1];
    for (int i = 0; i < N * 2 - 1; i++)
      dat[i] = INT_MAX;
  }

  void update(int k, int x) {
    k += N - 1;
    dat[k] = min(dat[k], x);
    while (k) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k], x);
    }
  }

  int getMin(int a, int b, int k = 0, int l = 0, int r = N) {
    if (r <= a || b <= l)
      return INT_MAX;
    if (a <= l && r <= b)
      return dat[k];
    int m = (l + r) / 2;
    int vl = getMin(a, b, k * 2 + 1, l, m);
    int vr = getMin(a, b, k * 2 + 2, m, r);
    return min(vl, vr);
  }
};
typedef pair<int, int> P;
typedef pair<int, P> PP;
#define fi first
#define se second
typedef long long ll;

int N, D;
PP Stars[200000];
SegTree minx, miny, maxx, maxy;

int main() {
  scanf("%d%d", &N, &D);
  for (int i = 0; i < N; i++) {
    int x, y, b;
    scanf("%d%d%d", &x, &y, &b);
    Stars[i] = PP(b, P(x, y));
  }

  sort(Stars, Stars + N);

  for (int i = 0; i < N; i++) {
    minx.update(i, Stars[i].se.fi);
    miny.update(i, Stars[i].se.se);
    maxx.update(i, -Stars[i].se.fi);
    maxy.update(i, -Stars[i].se.se);
  }

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    int j = upper_bound(Stars, Stars + N,
                        PP(Stars[i].fi + D, P(INT_MAX, INT_MAX))) -
            Stars;
    ll w = -maxx.getMin(i, j) - minx.getMin(i, j);
    ll h = -maxy.getMin(i, j) - miny.getMin(i, j);
    ans = max(ans, h * w);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

struct SegTree {
  static const int N = 1 << 18;
  int *dat;

  SegTree() {
    dat = new int[N * 2 - 1];
    for (int i = 0; i < N * 2 - 1; i++)
      dat[i] = INT_MAX;
  }

  void update(int k, int x) {
    k += N - 1;
    dat[k] = min(dat[k], x);
    while (k) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k], x);
    }
  }

  int getMin(int a, int b, int k = 0, int l = 0, int r = N) {
    if (r <= a || b <= l)
      return INT_MAX;
    if (r - l <= 1)
      return dat[k];
    int m = (l + r) / 2;
    int vl = getMin(a, b, k * 2 + 1, l, m);
    int vr = getMin(a, b, k * 2 + 2, m, r);
    return min(vl, vr);
  }
};
typedef pair<int, int> P;
typedef pair<int, P> PP;
#define fi first
#define se second
typedef long long ll;

int N, D;
PP Stars[200000];
SegTree minx, miny, maxx, maxy;

int main() {
  scanf("%d%d", &N, &D);
  for (int i = 0; i < N; i++) {
    int x, y, b;
    scanf("%d%d%d", &x, &y, &b);
    Stars[i] = PP(b, P(x, y));
  }

  sort(Stars, Stars + N);

  for (int i = 0; i < N; i++) {
    minx.update(i, Stars[i].se.fi);
    miny.update(i, Stars[i].se.se);
    maxx.update(i, -Stars[i].se.fi);
    maxy.update(i, -Stars[i].se.se);
  }

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    int j = upper_bound(Stars, Stars + N,
                        PP(Stars[i].fi + D, P(INT_MAX, INT_MAX))) -
            Stars;
    ll w = -maxx.getMin(i, j) - minx.getMin(i, j);
    ll h = -maxy.getMin(i, j) - miny.getMin(i, j);
    ans = max(ans, h * w);
  }

  cout << ans << endl;

  return 0;
}