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

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

#define INF (1LL << 60)
typedef pair<long long, long long> P;

int N;
long long D;
long long X[30], Y[30];
int n2;
long long seg[1 << 25];
vector<P> M1, M2;

void dfs(int i, int until, vector<P> &p, long long a, long long b) {
  if (i >= until) {
    p.push_back(P(a, b));
    return;
  }
  dfs(i + 1, until, p, a, b);
  dfs(i + 1, until, p, a - X[i], b - Y[i]);
  dfs(i + 1, until, p, a + X[i], b + Y[i]);
}

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

long long query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return -INF;
  if (a <= l && r <= b)
    return seg[k];
  return max(query(a, b, k * 2 + 1, l, (l + r) / 2),
             query(a, b, k * 2 + 2, (l + r) / 2, r));
}

int main() {
  cin >> N >> D;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
  }
  dfs(0, N / 2, M1, 0, 0);
  dfs(N / 2, N, M2, 0, 0);
  sort(M1.begin(), M1.end());
  sort(M2.begin(), M2.end());

  long long ans = 0;
  n2 = 1;
  while (n2 < M2.size())
    n2 *= 2;
  for (int i = 0; i < 2 * n2; i++)
    seg[i] = -INF;
  for (int i = 0; i < M2.size(); i++)
    update(i, M2[i].second);

  for (auto p : M1) {
    long long x = p.first, y = p.second;
    int lo = lower_bound(M2.begin(), M2.end(), P(-x - D, -INF)) - M2.begin();
    int hi = lower_bound(M2.begin(), M2.end(), P(-x + D, INF)) - M2.begin();
    if (lo > hi)
      continue;
    ans = max(ans, y + query(lo, hi, 0, 0, n2));
  }
  cout << ans << "\\n";
  return 0;
}

Error: -11

Fixed submission:

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

#define INF (1LL << 60)
typedef pair<long long, long long> P;

int N;
long long D;
long long X[30], Y[30];
int n2;
long long seg[1 << 24];
vector<P> M1, M2;

void dfs(int i, int until, vector<P> &p, long long a, long long b) {
  if (i >= until) {
    p.push_back(P(a, b));
    return;
  }
  dfs(i + 1, until, p, a, b);
  dfs(i + 1, until, p, a - X[i], b - Y[i]);
  dfs(i + 1, until, p, a + X[i], b + Y[i]);
}

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

long long query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return -INF;
  if (a <= l && r <= b)
    return seg[k];
  return max(query(a, b, k * 2 + 1, l, (l + r) / 2),
             query(a, b, k * 2 + 2, (l + r) / 2, r));
}

int main() {
  cin >> N >> D;
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
  }
  dfs(0, N / 2, M1, 0, 0);
  dfs(N / 2, N, M2, 0, 0);
  sort(M1.begin(), M1.end());
  sort(M2.begin(), M2.end());

  long long ans = 0;
  n2 = 1;
  while (n2 < M2.size())
    n2 *= 2;
  for (int i = 0; i < 2 * n2; i++)
    seg[i] = -INF;
  for (int i = 0; i < M2.size(); i++)
    update(i, M2[i].second);

  for (auto p : M1) {
    long long x = p.first, y = p.second;
    int lo = lower_bound(M2.begin(), M2.end(), P(-x - D, -INF)) - M2.begin();
    int hi = lower_bound(M2.begin(), M2.end(), P(-x + D, INF)) - M2.begin();
    if (lo > hi)
      continue;
    ans = max(ans, y + query(lo, hi, 0, 0, n2));
  }
  cout << ans << "\\n";
  return 0;
}