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

#include <bits/stdc++.h>
#define PB push_back
#define FI first
#define SE second
#define MP make_pair
#define int long long
using namespace std;
static const int inf = 1ll << 60;
static const int MAX_N = 16777216;
typedef pair<int, int> P;

int N, D;
int X[40], Y[40];
int ans = 0;
int dat[MAX_N * 4];

class Segment {
public:
  int n;
  void inti(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 4 * n - 1; ++i)
      dat[i] = -INT_MAX;
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return -INT_MAX;
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

void half(int n, int last, int x, int y, vector<P> &res) {
  if (n == last) {
    res.PB(P(x, y));
    return;
  }
  half(n - 1, last, x + X[n], y + Y[n], res);
  half(n - 1, last, x - X[n], y - Y[n], res);
  half(n - 1, last, x, y, res);
}

signed main() {
  cin >> N >> D;
  Segment seg;
  int n2 = N / 2;
  for (int i = 1; i <= N; ++i) {
    cin >> X[i] >> Y[i];
  }
  vector<P> p1;
  vector<P> p2;
  p1.reserve(MAX_N);
  half(n2, 0, 0, 0, p1);
  p2.reserve(MAX_N);
  half(N, n2, 0, 0, p2);
  sort(p2.begin(), p2.end());
  seg.inti(p2.size());
  for (int i = 0; i < p2.size(); ++i)
    seg.update(i, p2[i].SE);
  for (int i = 0; i < p1.size(); ++i) {
    int x = p1[i].FI + D;
    int y = p1[i].FI - D;
    int r = lower_bound(p2.begin(), p2.end(), MP(y, -inf)) - p2.begin();
    int l = upper_bound(p2.begin(), p2.end(), MP(x, inf)) - p2.begin();
    if (r == p2.size())
      continue;
    if (r >= l)
      continue;
    ans = max(ans, abs(seg.query(r, l, 0, 0, seg.n) - p1[i].SE));
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define FI first
#define SE second
#define MP make_pair
#define int long long
using namespace std;
static const int inf = 1ll << 60;
static const int MAX_N = 1 << 20;
typedef pair<int, int> P;

int N, D;
int X[40], Y[40];
int ans = 0;
int dat[MAX_N * 4];

class Segment {
public:
  int n;
  void inti(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 4 * n - 1; ++i)
      dat[i] = -INT_MAX;
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return -INT_MAX;
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

void half(int n, int last, int x, int y, vector<P> &res) {
  if (n == last) {
    res.PB(P(x, y));
    return;
  }
  half(n - 1, last, x + X[n], y + Y[n], res);
  half(n - 1, last, x - X[n], y - Y[n], res);
  half(n - 1, last, x, y, res);
}

signed main() {
  cin >> N >> D;
  Segment seg;
  int n2 = N / 2;
  for (int i = 1; i <= N; ++i) {
    cin >> X[i] >> Y[i];
  }
  vector<P> p1;
  vector<P> p2;
  p1.reserve(MAX_N);
  half(n2, 0, 0, 0, p1);
  p2.reserve(MAX_N);
  half(N, n2, 0, 0, p2);
  sort(p2.begin(), p2.end());
  seg.inti(p2.size());
  for (int i = 0; i < p2.size(); ++i)
    seg.update(i, p2[i].SE);
  for (int i = 0; i < p1.size(); ++i) {
    int x = p1[i].FI + D;
    int y = p1[i].FI - D;
    int r = lower_bound(p2.begin(), p2.end(), MP(y, -inf)) - p2.begin();
    int l = upper_bound(p2.begin(), p2.end(), MP(x, inf)) - p2.begin();
    if (r == p2.size())
      continue;
    if (r >= l)
      continue;
    ans = max(ans, abs(seg.query(r, l, 0, 0, seg.n) - p1[i].SE));
  }
  cout << ans << endl;
}