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

#include <algorithm>
#include <cstdio>
#include <limits>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
template <typename Type> class RMQ {
private:
  int size_;
  vector<Type> dat;
  Type query_(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return numeric_limits<Type>::max();
    if (a <= l && r <= b)
      return dat[k];
    int lc = query_(a, b, 2 * k, l, (l + r) / 2);
    int rc = query_(a, b, 2 * k + 1, (l + r) / 2, r);
    return min(lc, rc);
  }

public:
  RMQ(int size__) {
    for (size_ = 1; size_ < size__; size_ <<= 1)
      ;
    dat.resize(size_ * 2, numeric_limits<Type>::max());
  }
  RMQ(vector<Type> v) {
    for (size_ = 1; size_ < dat.size(); size_ <<= 1)
      ;
    dat.resize(size_ * 2, numeric_limits<Type>::max());
    for (int i = 0; i < v.size(); i++)
      dat[i + size_] = v[i];
    for (int i = size_ - 1; i >= 1; i--)
      dat[i] = min(dat[2 * i], dat[2 * i + 1]);
  }
  int size() { return size_; }
  int ndat() { return numeric_limits<Type>::max(); }
  void update(int i, int x) {
    i += size_;
    dat[i] = x;
    while (i > 1)
      i >>= 1, dat[i] = min(dat[2 * i], dat[2 * i + 1]);
  }
  Type query(int l, int r) { return query_(l, r, 1, 0, size_); }
};
int n, d, e[200000];
long long ret;
vector<int> f[200000];
int main() {
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; i++)
    f[i].resize(3), scanf("%d%d%d", &f[i][1], &f[i][2], &f[i][0]);
  sort(f, f + n);
  for (int i = 0; i < n; i++)
    e[i] = f[i][0];
  RMQ<int> r1(n);
  for (int i = 0; i < n; i++)
    r1.update(i, f[i][1]);
  RMQ<int> r2(n);
  for (int i = 0; i < n; i++)
    r2.update(i, -f[i][1]);
  RMQ<int> r3(n);
  for (int i = 0; i < n; i++)
    r3.update(i, f[i][2]);
  RMQ<int> r4(n);
  for (int i = 0; i < n; i++)
    r4.update(i, -f[i][2]);
  for (int i = 0; i < n; i++) {
    int ptr = upper_bound(e, e + n, e[i] + d) - e;
    int wp = -r2.query(i, ptr) - r1.query(i, ptr);
    int hp = -r4.query(i, ptr) - r3.query(i, ptr);
    ret = max(ret, 1LL * wp * hp);
  }
  printf("%lld\\n", ret);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <limits>
#include <vector>
using namespace std;
template <typename Type> class RMQ {
private:
  int size_;
  vector<Type> dat;
  Type query_(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return numeric_limits<Type>::max();
    if (a <= l && r <= b)
      return dat[k];
    int lc = query_(a, b, 2 * k, l, (l + r) / 2);
    int rc = query_(a, b, 2 * k + 1, (l + r) / 2, r);
    return min(lc, rc);
  }

public:
  RMQ(int size__) {
    for (size_ = 1; size_ < size__; size_ <<= 1)
      ;
    dat.resize(size_ * 2, numeric_limits<Type>::max());
  }
  RMQ(vector<Type> v) {
    for (size_ = 1; size_ < dat.size(); size_ <<= 1)
      ;
    dat.resize(size_ * 2, numeric_limits<Type>::max());
    for (int i = 0; i < v.size(); i++)
      dat[i + size_] = v[i];
    for (int i = size_ - 1; i >= 1; i--)
      dat[i] = min(dat[2 * i], dat[2 * i + 1]);
  }
  int size() { return size_; }
  int ndat() { return numeric_limits<Type>::max(); }
  void update(int i, int x) {
    i += size_;
    dat[i] = x;
    while (i > 1)
      i >>= 1, dat[i] = min(dat[2 * i], dat[2 * i + 1]);
  }
  Type query(int l, int r) { return query_(l, r, 1, 0, size_); }
};
int n, d, e[200000];
long long ret;
vector<int> f[200000];
int main() {
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; i++)
    f[i].resize(3), scanf("%d%d%d", &f[i][1], f[i][2], f[i][0]);
  sort(f, f + n);
  for (int i = 0; i < n; i++)
    e[i] = f[i][0];
  RMQ<int> r1(n);
  for (int i = 0; i < n; i++)
    r1.update(i, f[i][1]);
  RMQ<int> r2(n);
  for (int i = 0; i < n; i++)
    r2.update(i, -f[i][1]);
  RMQ<int> r3(n);
  for (int i = 0; i < n; i++)
    r3.update(i, f[i][2]);
  RMQ<int> r4(n);
  for (int i = 0; i < n; i++)
    r4.update(i, -f[i][2]);
  for (int i = 0; i < n; i++) {
    int ptr = upper_bound(e, e + n, e[i] + d) - e;
    int wp = -r2.query(i, ptr) - r1.query(i, ptr);
    int hp = -r4.query(i, ptr) - r3.query(i, ptr);
    ret = max(ret, 1LL * wp * hp);
  }
  printf("%lld\\n", ret);
  return 0;
}