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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, a, n) for (int(i) = (int)(a); (i) < (int)(n); ++(i))
#define repe(i, a, n) for (int(i) = (int)(a); (i) <= (int)(n); ++(i))
#define repr(i, a, n) for (int(i) = (int)(a); (i) > (int)(n); --(i))
#define reper(i, a, n) for (int(i) = (int)(a); (i) >= (int)(n); --(i))
#define SIZE(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define RNG(x, n) &((x)[0]), &((x)[n])
#define SUBIF(a, b, x) ((x) ? (b) : (a))
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define pb push_back
#define EPS (1e-20)
#define INF 2147483647
#define MOD 1000000007
using llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using MII = std::map<int, int>;
using TIII = std::tuple<int, int, int>;
using namespace std;

struct SEG {
  int aryN;
  vector<PII> seg;

  SEG() {}
  SEG(const VI &v) {
    aryN = (int)pow(2, (int)ceil(log2(SIZE(v))));
    seg.resize(2 * aryN - 1, {-1, INF});
    rep(i, 0, SIZE(v)) seg[i + aryN - 1] = {v[i], v[i]};
    reper(i, aryN - 2, 0) seg[i].first =
        max(seg[2 * i + 1].first, seg[2 * i + 2].first),
                          seg[i].second =
                              min(seg[2 * i + 1].second, seg[2 * i + 2].second);
  }
  void update(int x, PII p) {
    x += (aryN - 1);
    seg[x] = p;
    while (x > 0) {
      x = (x - 1) / 2;
      seg[x].first = max(seg[2 * x + 1].first, seg[2 * x + 2].first);
      seg[x].second = min(seg[2 * x + 1].second, seg[2 * x + 2].second);
    }
  }
  int getMax(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = aryN;
    if (r <= a || b <= l)
      return -1;
    if (a <= l && r <= b)
      return seg[k].first;
    int vl = getMax(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getMax(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
  int getMin(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = aryN;
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return seg[k].second;
    int vl = getMin(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getMin(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int N, d;
vector<TIII> s;
int br[200010];
SEG X, Y;

template <int N> VI tupleToVec(vector<TIII> &vt) {
  VI ret;
  for (TIII t : vt)
    ret.pb(get<N>(t));
  return ret;
}

llint solve() {
  llint ans = 0;
  rep(i, 0, N) br[i] = get<0>(s[i]);
  X = SEG(tupleToVec<1>(s));
  Y = SEG(tupleToVec<2>(s));
  rep(i, 0, N) {
    int p = upper_bound(RNG(br, N), br[i] + d) - &br[0];
    llint x1 = X.getMax(i, p), x2 = X.getMin(i, p);
    llint y1 = Y.getMax(i, p), y2 = Y.getMin(i, p);
    ans = max(ans, (x1 - x2) * (y1 - y2));
  }
  return ans;
}

int main() {
  cin >> N >> d;
  s = vector<TIII>(N);
  rep(i, 0, N) cin >> get<1>(s[i]) >> get<2>(s[i]) >> get<0>(s[i]);
  sort(ALL(s));

  cout << solve() << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

pair<pair<int, int>, int> star[200000];

void clear(vector<int> &v) {
  vector<int> emp;
  swap(v, emp);
}

int main() {
  int n;
  int d;

  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> star[i].first.first >> star[i].first.second >> star[i].second;
  }

  sort(star, star + n,
       [](pair<pair<int, int>, int> p1, pair<pair<int, int>, int> p2) {
         return (p1.second < p2.second);
       });

  for (int i = 0; i < n; i++)
    cout << star[i].first.first << ' ' << star[i].first.second << ' '
         << star[i].second << endl;

  vector<int> xlist, ylist;
  int ans = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; star[j + i].second <= star[i].second + d; j++) {
      xlist.push_back(star[j + i].first.first);
      ylist.push_back(star[j + i].first.second);
    }
    int x, y;
    x = ((*max_element(xlist.begin(), xlist.end())) -
         (*min_element(xlist.begin(), xlist.end())));
    y = ((*max_element(ylist.begin(), ylist.end())) -
         (*min_element(ylist.begin(), ylist.end())));
    ans = max(x * y, ans);
    clear(xlist);
    clear(ylist);
  }

  cout << ans << endl;

  return 0;
}