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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> Pi;
typedef long long int lld;
struct SegmentTree {
  static const int INF = 1 << 30;

  vector<int> small, big, sum, add;
  int sz;

  SegmentTree(int n) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    small.assign(2 * sz - 1, INF);
    big.assign(2 * sz - 1, -INF);
    sum.assign(2 * sz - 1, 0);
    add.assign(2 * sz - 1, 0);
  }
  inline void Merge(int k) {
    small[k] = min(small[2 * k + 1] + add[2 * k + 1],
                   small[2 * k + 2] + add[2 * k + 2]);
    big[k] =
        max(big[2 * k + 1] + add[2 * k + 1], big[2 * k + 2] + add[2 * k + 2]);
    sum[k] = sum[2 * k + 1] + add[2 * k + 1] + sum[2 * k + 2] + add[2 * k + 2];
  }
  inline int RangeMaximumQuery(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l)
      return (-INF);
    if (a <= l && r <= b)
      return (big[k] + add[k]);
    int L = RangeMaximumQuery(a, b, 2 * k + 1, l, (l + r) >> 1);
    int R = RangeMaximumQuery(a, b, 2 * k + 2, (l + r) >> 1, r);
    return (max(L, R) + add[k]);
  }
  inline int RangeMinimumQuery(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l)
      return (INF);
    if (a <= l && r <= b)
      return (small[k] + add[k]);
    int L = RangeMinimumQuery(a, b, 2 * k + 1, l, (l + r) >> 1);
    int R = RangeMinimumQuery(a, b, 2 * k + 2, (l + r) >> 1, r);
    return (min(L, R) + add[k]);
  }
  inline int RangeSumQuery(int a, int b, int k, int l, int r) {
    if (a >= r || b <= l)
      return (INF);
    if (a <= l && r <= b)
      return (sum[k] + add[k]);
    int L = RangeSumQuery(a, b, 2 * k + 1, l, (l + r) >> 1);
    int R = RangeSumQuery(a, b, 2 * k + 2, (l + r) >> 1, r);
    return (L + R + add[k]);
  }
  void RangeAdd(int a, int b, int x, int k, int l, int r) {
    if (a >= r || b <= l)
      return;
    if (a <= l && r <= b) {
      add[k] += x;
      return;
    }
    RangeAdd(a, b, x, 2 * k + 1, l, (l + r) >> 1);
    RangeAdd(a, b, x, 2 * k + 2, (l + r) >> 1, r);
    Merge(k);
  }

  void Update(int k, int x) {
    k += sz - 1;
    small[k] = big[k] = sum[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      Merge(k);
    }
  }
  int RangeMaximumQuery(int a, int b) {
    return (RangeMaximumQuery(a, b, 0, 0, sz));
  }
  int RangeMinimumQuery(int a, int b) {
    return (RangeMinimumQuery(a, b, 0, 0, sz));
  }
  int RangeSumQuery(int a, int b) { return (RangeSumQuery(a, b, 0, 0, sz)); }
  void RangeAdd(int a, int b, int x) { return (RangeAdd(a, b, x, 0, 0, sz)); }
};

int n, d;
Pi star[200000];
SegmentTree X(200010), Y(200010);

int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> star[i].second.first >> star[i].second.second >> star[i].first;
  }
  sort(star, star + n);
  for (int i = 0; i < n; i++) {
    X.Update(i, star[i].second.first);
    Y.Update(i, star[i].second.second);
  }
  int idx = 0;
  lld ans = -1;
  for (int i = 0; i < n; i++) {
    while (idx < n && star[idx].first - star[i].first <= d)
      idx++;
    lld maxx = X.RangeMaximumQuery(i, idx);
    lld minx = X.RangeMinimumQuery(i, idx);
    lld maxy = Y.RangeMaximumQuery(i, idx);
    lld miny = Y.RangeMinimumQuery(i, idx);
    ans = max(ans, (maxx - minx) * (maxy - miny));
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef pair<long long int, long long int> P;
typedef pair<long long int, P> Pii;
Pii star[200001];
int main() {
  long long int n, d;
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    Pii t;
    t.first = c;
    t.second.first = a;
    t.second.second = b;
    star[i] = t;
  }
  sort(star, star + n);
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    long long int now = star[i].first;
    long long int maxx = star[i].second.first;
    long long int minx = star[i].second.first;
    long long int maxy = star[i].second.second;
    long long int miny = star[i].second.second;
    for (int j = i + 1; j < n; j++) {
      if (now + d >= star[j].first) {
        maxx = max(maxx, star[j].second.first);
        minx = min(minx, star[j].second.first);
        maxy = max(maxy, star[j].second.second);
        miny = min(miny, star[j].second.second);
      } else
        break;
    }
    ans = max(ans, (maxx - minx) * (maxy - miny));
  }
  cout << ans << endl;
}