Problem p00297 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P2;

struct RBQ {
  int n = (1 << 18), data[(1 << 19)];
  RBQ() { memset(data, 0, sizeof(data)); }
  void update(int k, int x) {
    k += n - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = max(data[k * 2 + 1], data[k * 2 + 2]);
    }
  }
  int find(int a, int b, int k = 0, int l = 0, int r = (1 << 18)) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return data[k];
    int v1 = find(a, b, k * 2 + 1, l, (l + r) / 2);
    int v2 = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(v1, v2);
  }
};

struct RLQ {
  int n = (1 << 18), data[(1 << 19)];
  RLQ() { fill(data, data + (1 << 19), INT_MAX); }
  void update(int k, int x) {
    k += n - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[k * 2 + 1], data[k * 2 + 2]);
    }
  }
  int find(int a, int b, int k = 0, int l = 0, int r = (1 << 18)) {
    if (r <= a || b <= l)
      return INT_MAX;
    if (a <= l && r <= b)
      return data[k];
    int v1 = find(a, b, k * 2 + 1, l, (l + r) / 2);
    int v2 = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(v1, v2);
  }
};

signed main() {
  int n, D;
  cin >> n >> D;
  RBQ X, Y;
  RLQ x, y;
  vector<P2> v;
  r(i, n) {
    int x, y, z;
    cin >> x >> y >> z;
    v.push_back(P2(z, P(x, y)));
  }
  sort(v.begin(), v.end());
  int l = 0, ans = 0;
  r(r, n) {
    int d = v[r].first;
    int nx = v[r].second.first;
    int ny = v[r].second.second;
    x.update(r, nx);
    X.update(r, nx);
    y.update(r, ny);
    Y.update(r, ny);
    while (d - v[l].first > D)
      l++;
    int min_x = x.find(l, r + 1);
    int max_x = X.find(l, r + 1);
    int min_y = y.find(l, r + 1);
    int max_y = Y.find(l, r + 1);
    ans = max(ans, (max_x - min_x) * (max_y - min_y));
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P2;

struct RBQ {
  int n = (1 << 16), data[(1 << 17)];
  RBQ() { memset(data, 0, sizeof(data)); }
  void update(int k, int x) {
    k += n - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = max(data[k * 2 + 1], data[k * 2 + 2]);
    }
  }
  int find(int a, int b, int k = 0, int l = 0, int r = (1 << 16)) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return data[k];
    int v1 = find(a, b, k * 2 + 1, l, (l + r) / 2);
    int v2 = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(v1, v2);
  }
};

struct RLQ {
  int n = (1 << 16), data[(1 << 17)];
  RLQ() { fill(data, data + (1 << 17), INT_MAX); }
  void update(int k, int x) {
    k += n - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[k * 2 + 1], data[k * 2 + 2]);
    }
  }
  int find(int a, int b, int k = 0, int l = 0, int r = (1 << 16)) {
    if (r <= a || b <= l)
      return INT_MAX;
    if (a <= l && r <= b)
      return data[k];
    int v1 = find(a, b, k * 2 + 1, l, (l + r) / 2);
    int v2 = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(v1, v2);
  }
};

int main() {
  int n, D;
  cin >> n >> D;
  RBQ X, Y;
  RLQ x, y;
  vector<P2> v;
  r(i, n) {
    int x, y, z;
    cin >> x >> y >> z;
    v.push_back(P2(z, P(x, y)));
  }
  sort(v.begin(), v.end());
  int l = 0, ans = 0;
  r(r, n) {
    int d = v[r].first;
    int nx = v[r].second.first;
    int ny = v[r].second.second;
    x.update(r, nx);
    X.update(r, nx);
    y.update(r, ny);
    Y.update(r, ny);
    while (d - v[l].first > D)
      l++;
    int min_x = x.find(l, r + 1);
    int max_x = X.find(l, r + 1);
    int min_y = y.find(l, r + 1);
    int max_y = Y.find(l, r + 1);
    ans = max(ans, (max_x - min_x) * (max_y - min_y));
  }
  cout << ans << endl;
}