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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

template <typename T> struct SegTreeMin {
private:
  ll N = 1;
  vector<T> node;

public:
  SegTreeMin(vector<T> vec) {
    ll size = vec.size();

    while (N < size) {
      N *= 2;
    }

    node.resize(2 * N - 1, iINF);

    REP(i, size) { node[i + N - 1] = vec[i]; }

    for (int i = N - 2; 0 <= i; i--) {
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  void update(int i, T value) {
    i += (N - 1);

    node[i] = value;

    while (i > 0) {
      i = (i - 1) / 2;

      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  T getMin(int a, int b, int i = 0, int l = 0, int r = -1) {
    if (r < 0) {
      r = N;
    }

    if (r <= a || b <= l) {
      return iINF;
    }

    if (a <= l && r <= b) {
      return node[i];
    }

    T vl = getMin(a, b, 2 * i + 1, l, (l + r) / 2);

    T vr = getMin(a, b, 2 * i + 2, (l + r) / 2, r);

    return min(vl, vr);
  }
};

template <typename T> struct SegTreeMax {
private:
  ll N = 1;
  vector<T> node;

public:
  SegTreeMax(vector<T> vec) {
    ll size = vec.size();

    while (N < size) {
      N *= 2;
    }

    node.resize(2 * N - 1, -iINF);

    REP(i, size) { node[i + N - 1] = vec[i]; }

    for (int i = N - 2; 0 <= i; i--) {
      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  void update(int i, T value) {
    i += (N - 1);

    node[i] = value;

    while (i > 0) {
      i = (i - 1) / 2;

      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  T getMax(int a, int b, int i = 0, int l = 0, int r = -1) {
    if (r < 0) {
      r = N;
    }

    if (r <= a || b <= l) {
      return -iINF;
    }

    if (a <= l && r <= b) {
      return node[i];
    }

    T vl = getMax(a, b, 2 * i + 1, l, (l + r) / 2);

    T vr = getMax(a, b, 2 * i + 2, (l + r) / 2, r);

    return max(vl, vr);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, d;
  cin >> N >> d;

  vector<tuple<ll, ll, ll>> vec(N);

  SegTreeMax<ll> xMax(vector<ll>(N, -iINF)), yMax(vector<ll>(N, -iINF));
  SegTreeMin<ll> xMin(vector<ll>(N, iINF)), yMin(vector<ll>(N, iINF));

  ll ans = 0;

  REP(i, N) {
    ll x, y, d;
    cin >> x >> y >> d;

    vec[i] = make_tuple(d, x, y);
  }

  sort(ALL(vec));

  REP(i, N) {
    ll x, y, d;
    tie(d, x, y) = vec[i];

    xMax.update(i, x);
    xMin.update(i, x);
    yMax.update(i, y);
    yMin.update(i, y);
  }

  REP(i, N) {
    ll start = get<0>(vec[i]);

    auto itr =
        upper_bound(vec.begin(), vec.end(), make_tuple(d + start, iINF, iINF));

    if (itr == vec.begin())
      continue;

    ll now = itr - vec.begin();

    ll x = xMax.getMax(i, now), y = yMax.getMax(i, now);
    ll _x = xMin.getMin(i, now), _y = yMin.getMin(i, now);

    ans = max(ans, (x - _x) * (y - _y));
  }

  Cout(ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, d;
  cin >> N >> d;

  vector<tuple<ll, ll, ll>> vec(N);

  vector<ll> minx(N), miny(N), maxx(N), maxy(N);

  REP(i, N) {
    ll x, y, b;
    cin >> x >> y >> b;
    vec[i] = make_tuple(b, x, y);
  }

  sort(ALL(vec));

  ll ans = -1;

  REP(i, N) {
    ll start = get<0>(vec[i]);

    auto itr =
        upper_bound(vec.begin(), vec.end(), make_tuple(d + start, iINF, iINF));

    if (itr == vec.begin())
      continue;

    // cout << itr - vec.begin() << endl;

    ll maxx = 0, maxy = 0, minx = iINF, miny = iINF;

    FOR(j, i, itr - vec.begin()) {
      ll x = get<1>(vec[j]), y = get<2>(vec[j]);

      maxx = max(maxx, x);
      maxy = max(maxy, y);
      minx = min(minx, x);
      miny = min(miny, y);
    }

    ans = max(ans, (ll)((maxx - minx) * (maxy - miny)));
  }

  Cout(ans);

  return 0;
}