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

#include <bits/stdc++.h>
using namespace std;
#define int LL
typedef long long LL;
const int INF = 2002002002;

struct DATA {
  int minx, miny, maxx, maxy;
};

struct STAR {
  int x, y, b;
  bool operator<(const STAR &t) const { return b < t.b; }
};

int n, d;
STAR star[1 << 19];

int N;
DATA data[1 << 20];

void init(int n) {
  for (N = 1; N < n; N <<= 1)
    ;
  fill(data, data + (1 << 20), (DATA){INF, INF, -INF, -INF});
}

void update(int i, STAR s) {
  i += N - 1;
  data[i] = (DATA){s.x, s.y, s.x, s.y};
  while (i > 0) {
    i = (i - 1) / 2;
    data[i].minx = min(data[i * 2 + 1].minx, data[i * 2 + 2].minx);
    data[i].miny = min(data[i * 2 + 1].miny, data[i * 2 + 2].miny);
    data[i].maxx = max(data[i * 2 + 1].maxx, data[i * 2 + 2].maxx);
    data[i].maxy = max(data[i * 2 + 1].maxy, data[i * 2 + 2].maxy);
  }
}

DATA query(int a, int b, int k, int l, int r) {
  // printf("%d %d %d %d %d\\n", a, b, k, l, r);
  if (r <= a || b <= l)
    return (DATA){INF, INF, -INF, -INF};
  if (a <= l && r <= b)
    return data[k];
  DATA vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  DATA vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return (DATA){min(vl.minx, vr.minx), min(vl.miny, vr.miny),
                max(vl.maxx, vr.maxx), max(vl.maxy, vr.maxy)};
}

signed main() {
  scanf("%d %d", &n, &d);
  init(n);

  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &star[i].x, &star[i].y, &star[i].b);
    // star[i].x; star[i].y;
  }
  for (int i = n; i < 1 << 19; i++) {
    star[i].b = INF;
  }

  sort(star, star + n);

  for (int i = 0; i < n; i++) {
    update(i, star[i]);
  }

  // printf("%lld\\n", (LL)INF * INF);

  int pr = 0;
  LL res = 0;
  for (int l = 0; star[l].b != INF;) {
    int r = pr;
    while (star[r].b - star[l].b <= d)
      r++;
    if (r == pr) {
      l++;
      continue;
    }
    pr = r;

    DATA q = query(l, r, 0, 0, N);
    LL t = (LL)(q.maxx - q.minx) * (q.maxy - q.miny);
    res = max(res, t);

    do {
      l++;
    } while (star[l - 1].b == star[l].b);
  }

  printf("%lld\\n", res);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int LL
typedef long long LL;
const int INF = 2002002002;

struct DATA {
  int minx, miny, maxx, maxy;
};

struct STAR {
  int x, y, b;
  bool operator<(const STAR &t) const { return b < t.b; }
};

int n, d;
STAR star[1 << 19];

int N;
DATA data[1 << 20];

void init(int n) {
  for (N = 1; N < n; N <<= 1)
    ;
  fill(data, data + (1 << 20), (DATA){INF, INF, -INF, -INF});
}

void update(int i, STAR s) {
  i += N - 1;
  data[i] = (DATA){s.x, s.y, s.x, s.y};
  while (i > 0) {
    i = (i - 1) / 2;
    data[i].minx = min(data[i * 2 + 1].minx, data[i * 2 + 2].minx);
    data[i].miny = min(data[i * 2 + 1].miny, data[i * 2 + 2].miny);
    data[i].maxx = max(data[i * 2 + 1].maxx, data[i * 2 + 2].maxx);
    data[i].maxy = max(data[i * 2 + 1].maxy, data[i * 2 + 2].maxy);
  }
}

DATA query(int a, int b, int k, int l, int r) {
  // printf("%d %d %d %d %d\\n", a, b, k, l, r);
  if (r <= a || b <= l)
    return (DATA){INF, INF, -INF, -INF};
  if (a <= l && r <= b)
    return data[k];
  DATA vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  DATA vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return (DATA){min(vl.minx, vr.minx), min(vl.miny, vr.miny),
                max(vl.maxx, vr.maxx), max(vl.maxy, vr.maxy)};
}

signed main() {
  scanf("%d %d", &n, &d);
  init(n);

  for (int i = 0; i < n; i++) {
    scanf("%lld %lld %lld", &star[i].x, &star[i].y, &star[i].b);
    // star[i].x; star[i].y;
  }
  for (int i = n; i < 1 << 19; i++) {
    star[i].b = INF;
  }

  sort(star, star + n);

  for (int i = 0; i < n; i++) {
    update(i, star[i]);
  }

  // printf("%lld\\n", (LL)INF * INF);

  int pr = 0;
  LL res = 0;
  for (int l = 0; star[l].b != INF;) {
    int r = l;
    while (star[r].b - star[l].b <= d)
      r++;
    if (r == pr) {
      l++;
      continue;
    }
    pr = r;

    DATA q = query(l, r, 0, 0, N);
    LL t = (LL)(q.maxx - q.minx) * (q.maxy - q.miny);
    res = max(res, t);

    do {
      l++;
    } while (star[l - 1].b == star[l].b);
  }

  printf("%lld\\n", res);

  return 0;
}