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

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define size_of(a) (int)(a).size()

typedef long long lint;
typedef pair<lint, lint> pii;

template <typename DATA> class SegmentTree {
private:
  int size__;
  vector<DATA> data;

  inline int left_t(int k) { return 2 * k + 1; }
  inline int right_t(int k) { return 2 * k + 2; }

public:
  SegmentTree(int n, DATA ini) {
    for (size__ = 1; size__ < n; size__ *= 2)
      ;
    data.assign(2 * size__ - 1, ini);
  }

  inline DATA calc(DATA d1, DATA d2) { return max(d1, d2); }

  void update(int k, DATA a) {
    k += size__ - 1;
    data[k] = a;

    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = calc(data[left_t(k)], data[right_t(k)]);
    }
  }

  DATA query(int a, int b) { return query(a, b, 0, 0, size__); }

  DATA query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return -(1LL << 60);
    if (a <= l && r <= b)
      return data[k];
    return calc(query(a, b, left_t(k), l, (l + r) / 2),
                query(a, b, right_t(k), (l + r) / 2, r));
  }
};

int N;
lint D, X[33], Y[33];

void rec(int i, int end, lint subX, lint subY, vector<pii> &vec) {
  if (i == end) {
    vec.push_back(pii(subX, subY));
    return;
  }

  rec(i + 1, end, subX, subY, vec);
  rec(i + 1, end, subX + X[i], subY - Y[i], vec);
  rec(i + 1, end, subX - X[i], subY + Y[i], vec);
}

int main() {
  cin >> N >> D;
  for_(i, 0, N) cin >> X[i] >> Y[i];

  vector<pii> vf, vs;

  rec(0, N / 2, 0, 0, vf);
  rec(N / 2, N, 0, 0, vs);

  sort(vs.begin(), vs.end());

  SegmentTree<lint> segtree(size_of(vs), -(1LL << 60));

  for_(i, 0, size_of(vs)) segtree.update(i, vs[i].second);

  vs.push_back(pii(1LL << 60, 0));
  sort(vf.begin(), vf.end(), greater<pii>());

  int indexL = 0, indexU = 0;
  lint ans = 0;

  for_(i, 0, size_of(vf)) {
    lint subX = vf[i].first;
    lint upper = D - subX, lower = -(D + subX);

    while (vs[indexL].first < lower)
      ++indexL;
    while (vs[indexU].first <= upper)
      ++indexU;

    ans = max(ans, vf[i].second + segtree.query(indexL, indexU));
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define size_of(a) (int)(a).size()

typedef long long lint;
typedef pair<lint, lint> pii;

template <typename DATA> class SegmentTree {
private:
  int size__;
  vector<DATA> data;

  inline int left_t(int k) { return 2 * k + 1; }
  inline int right_t(int k) { return 2 * k + 2; }

public:
  SegmentTree(int n, DATA ini) {
    for (size__ = 1; size__ < n; size__ *= 2)
      ;
    data.assign(2 * size__ - 1, ini);
  }

  inline DATA calc(DATA d1, DATA d2) { return max(d1, d2); }

  void update(int k, DATA a) {
    k += size__ - 1;
    data[k] = a;

    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = calc(data[left_t(k)], data[right_t(k)]);
    }
  }

  DATA query(int a, int b) { return query(a, b, 0, 0, size__); }

  DATA query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return -(1LL << 60);
    if (a <= l && r <= b)
      return data[k];
    return calc(query(a, b, left_t(k), l, (l + r) / 2),
                query(a, b, right_t(k), (l + r) / 2, r));
  }
};

int N;
lint D, X[33], Y[33];

void rec(int i, int end, lint subX, lint subY, vector<pii> &vec) {
  if (i == end) {
    vec.push_back(pii(subX, subY));
    return;
  }

  rec(i + 1, end, subX, subY, vec);
  rec(i + 1, end, subX + X[i], subY - Y[i], vec);
  rec(i + 1, end, subX - X[i], subY + Y[i], vec);
}

int main() {
  cin >> N >> D;
  for_(i, 0, N) cin >> X[i] >> Y[i];

  vector<pii> vf, vs;

  rec(0, N / 2, 0, 0, vf);
  rec(N / 2, N, 0, 0, vs);

  sort(vs.begin(), vs.end());

  SegmentTree<lint> segtree(size_of(vs), -(1LL << 60));

  for_(i, 0, size_of(vs)) segtree.update(i, vs[i].second);

  sort(vf.begin(), vf.end(), greater<pii>());
  int indexL = 0, indexU = 0;
  lint ans = 0;

  for_(i, 0, size_of(vf)) {
    lint subX = vf[i].first;
    lint upper = D - subX, lower = -(D + subX);

    while (vs[indexL].first < lower)
      ++indexL;
    while (vs[indexU].first <= upper)
      ++indexU;

    ans = max(ans, vf[i].second + segtree.query(indexL, indexU));
  }

  cout << ans << endl;
}