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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define W first
#define V second
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

class SegmentTree {
  int n;
  vector<ll> node;

public:
  SegmentTree(vector<ll> a) {
    int n_ = a.size();
    n = 1;
    while (n < n_)
      n *= 2;
    node.resize(2 * n - 1, 1e18);
    for (int i = 0; i < n_; i++)
      node[n - 1 + i] = a[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
  }

  // return maximum value in [a,b). ( [l,r) is interval in which k is.)
  ll RMQ(int a, int b, int k = 0, int l = 0, int r = 0) {
    if (l >= r)
      r = n;
    if (r <= a || b <= l)
      return 1e18;
    if (a <= l && r <= b)
      return node[k];
    ll vl = RMQ(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = RMQ(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int main() {
  ll n, d;
  cin >> n >> d;

  ll sumw = 0, sumv = 0;
  vector<ll> w(n), v(n);
  rep(i, n) {
    cin >> w[i] >> v[i];
    sumw += w[i];
    sumv += v[i];
  }

  ll p = n / 2, s = n - p;
  vector<pll> pre, suf;

  ll P = pow(3, p);
  rep(bit, P) {
    pll cur(0, 0);
    ll tmp = bit;
    rep(i, p) {
      if (tmp % 3 == 0) {
        cur.W += 2 * w[i];
        cur.V += 2 * v[i];
      } else if (tmp % 3 == 1) {
        cur.W += w[i];
        cur.V += v[i];
      }
      tmp /= 3;
    }
    pre.push_back(cur);
  }

  ll S = pow(3, s);
  rep(bit, S) {
    pll cur(0, 0);
    ll tmp = bit;
    rep(i, s) {
      if (tmp % 3 == 0) {
        cur.W += 2 * w[p + i];
        cur.V += 2 * v[p + i];
      } else if (tmp % 3 == 1) {
        cur.W += w[p + i];
        cur.V += v[p + i];
      }
      tmp /= 3;
    }
    suf.push_back(cur);
  }

  sort(all(suf));
  vector<ll> val(S);
  rep(i, S) val[i] = suf[i].V;
  SegmentTree st(val);

  ll res = 1e18;
  rep(i, P) {
    ll X = pre[i].W, Y = pre[i].V;
    ll lval = sumw - d - X, rval = sumw + d - X + 1;
    int lpos = lower_bound(all(suf), pll(lval, 0)) - suf.begin();
    int rpos = lower_bound(all(suf), pll(rval, 0)) - suf.begin();

    if (lpos < rpos) {
      res = min(res, Y + st.RMQ(lpos, rpos));
    }
  }

  cout << sumv - res << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define W first
#define V second
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

struct SparseTable {
  vector<ll> val;
  vi MSB;
  vector<vi> table;
  SparseTable(const vector<ll> a) : val(a) {
    int n = a.size();
    MSB = vi(n + 1, 0);
    int k = 2;
    rep(i, n) {
      if (i + 1 == k)
        MSB[i + 1] = MSB[i] + 1, k <<= 1;
      else
        MSB[i + 1] = MSB[i];
    }
    table = vector<vi>(n, vi(MSB[n] + 1, 0));
    rep(i, n) table[i][0] = i;

    k = 1;
    rep(j, MSB[n]) {
      rep(i, n) {
        int id1 = table[i][j], id2 = (i + k < n) ? table[i + k][j] : id1;
        table[i][j + 1] = (val[id1] <= val[id2]) ? id1 : id2;
      }
      k <<= 1;
    }
  }

  int RMQi(int l, int r) {
    int ln = MSB[r - l];
    int id1 = table[l][ln], id2 = table[r - (1 << ln)][ln];
    return (val[id1] <= val[id2]) ? id1 : id2;
  }

  ll RMQ(int l, int r) { return val[RMQi(l, r)]; }
};

int main() {
  ll n, d;
  cin >> n >> d;

  ll sumw = 0, sumv = 0;
  vector<ll> w(n), v(n);
  rep(i, n) {
    cin >> w[i] >> v[i];
    sumw += w[i];
    sumv += v[i];
  }

  ll p = n / 2, s = n - p;
  vector<pll> pre, suf;

  ll P = pow(3, p);
  rep(bit, P) {
    pll cur(0, 0);
    ll tmp = bit;
    rep(i, p) {
      if (tmp % 3 == 0) {
        cur.W += 2 * w[i];
        cur.V += 2 * v[i];
      } else if (tmp % 3 == 1) {
        cur.W += w[i];
        cur.V += v[i];
      }
      tmp /= 3;
    }
    pre.push_back(cur);
  }

  ll S = pow(3, s);
  rep(bit, S) {
    pll cur(0, 0);
    ll tmp = bit;
    rep(i, s) {
      if (tmp % 3 == 0) {
        cur.W += 2 * w[p + i];
        cur.V += 2 * v[p + i];
      } else if (tmp % 3 == 1) {
        cur.W += w[p + i];
        cur.V += v[p + i];
      }
      tmp /= 3;
    }
    suf.push_back(cur);
  }

  sort(all(suf));
  vector<ll> val(S);
  rep(i, S) val[i] = suf[i].V;
  SparseTable st(val);

  ll res = 1e18;
  rep(i, P) {
    ll X = pre[i].W, Y = pre[i].V;
    ll lval = sumw - d - X, rval = sumw + d - X + 1;
    int lpos = lower_bound(all(suf), pll(lval, 0)) - suf.begin();
    int rpos = lower_bound(all(suf), pll(rval, 0)) - suf.begin();

    if (lpos < rpos) {
      res = min(res, Y + st.RMQ(lpos, rpos));
    }
  }

  cout << sumv - res << endl;
}