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

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

ll N;
vector<ll> V, W;
void input() {
  fast_io();
  cin >> N;
  V.push_back(0LL);
  W.push_back(0LL);
  for (int i = 0; i < N; i++) {
    ll v, w;
    cin >> v >> w;
    V.push_back(v);
    W.push_back(w);
  }
}

template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}

ll N_solve(int N, vector<ll> v, vector<ll> w, ll L) {
  ll ans = 0;
  int n1 = N / 2, n2 = N - n1;
  vector<pair<int, ll>> a;
  for (int i = 0; i < (1 << n1); i++) {
    ll t = 0, u = 0;
    for (int j = 0; j < n1; j++)
      if ((i >> j) & 1)
        t += v[j], u += w[j];
    if (u <= L)
      a.push_back(make_pair((int)(L - u), t));
  }
  sort(a.begin(), a.end());
  vector<ll> dp(a.size() + 1);
  for (int i = a.size() - 1; i >= 0; --i)
    dp[i] = max(dp[i + 1], a[i].second);

  for (int i = 0; i < (1 << n2); i++) {
    ll t = 0, u = 0;
    for (int j = 0; j < n2; j++)
      if ((i >> j) & 1)
        t += v[n1 + j], u += w[n1 + j];
    if (u <= L) {
      int k =
          lower_bound(a.begin(), a.end(), make_pair((int)u, 0LL)) - a.begin();
      amax(ans, t + dp[k]);
    }
  }
  return ans;
}

ll N_solveA(int N, vector<ll> v, vector<ll> w, ll L, vector<pair<int, ll>> A) {
  ll ans = 0;
  int n1 = 0, n2 = N - n1;
  vector<pair<int, ll>> a;
  for (auto &x : A) {
    x.first += L;
    if (x.first >= 0)
      a.push_back(x);
  }
  vector<ll> dp(a.size() + 1);
  for (int i = a.size() - 1; i >= 0; --i)
    dp[i] = max(dp[i + 1], a[i].second);

  for (int i = 0; i < (1 << n2); i++) {
    ll t = 0, u = 0;
    for (int j = 0; j < n2; j++)
      if ((i >> j) & 1)
        t += v[n1 + j], u += w[n1 + j];
    if (u <= L) {
      int k =
          lower_bound(a.begin(), a.end(), make_pair((int)u, 0LL)) - a.begin();
      amax(ans, t + dp[k]);
    }
  }
  return ans;
}

int TLEsolve() {
  // dp.assign(N+10,vector<ll>(110000,0LL));
  int Q;
  cin >> Q;
  vector<vector<pair<int, ll>>> A(sqrt(N) + 10);

  for (int t = 0; t < sqrt(N); t++) {
    vector<pair<int, ll>> a;
    vector<ll> v, w;
    int idx = t;
    while (idx) {
      v.push_back(V[idx]);
      w.push_back(W[idx]);
      idx >>= 1;
    }
    int n1 = v.size();
    for (int i = 0; i < (1 << n1); i++) {
      ll t = 0, u = 0;
      for (int j = 0; j < n1; j++)
        if ((i >> j) & 1)
          t += v[j], u += w[j];
      a.push_back(make_pair((int)(-u), t));
    }
    sort(a.begin(), a.end());
    A[t] = a;
  }

  for (int q = 0; q < Q; q++) {
    ll I, L;
    cin >> I >> L;
    // I--;
    vector<ll> v, w;
    if (I <= sqrt(N)) {
      while (I) {
        v.push_back(V[I]);
        w.push_back(W[I]);
        I /= 2;
      }
      int M = v.size();
      cout << N_solve(M, v, w, L) << "\\n";
    } else {
      while (I > sqrt(N)) {
        v.push_back(V[I]);
        w.push_back(W[I]);
        I /= 2;
      }
      int M = v.size();

      cout << N_solveA(M, v, w, L, A[I]) << "\\n";
    }

    // cout << rec(v,L) << "\\n";
  }

  return 0;
}

// ll dp[1<<19][110000];
vector<vector<ll>> dp;
// TLE
ll rec(int i, int l) {
  if (l < 0LL)
    return -(ll)(1e7);
  if (i <= sqrt(N))
    if (dp[i][l] != -1)
      return dp[i][l];
  if (i == 1) {
    if (l < W[i])
      return 0LL;
    else
      return V[i];
  }
  // return dp[i][l] = max(rec(i/2,l),rec(i/2,l-W[i])+V[i]);
  return max(rec(i / 2, l), rec(i / 2, l - W[i]) + V[i]);
}
int solve() {

  int Q;
  cin >> Q;
  int Lmax = 0;
  vector<int> vq, Lq;
  for (int q = 0; q < Q; q++) {
    int v, L;
    cin >> v >> L;
    // v--;
    vq.push_back(v);
    Lq.push_back(L);
    amax(Lmax, L);
  }

  dp.assign((int)sqrt(N) + 10, vector<ll>(Lmax + 10, -1));
  // memset(dp,-1,sizeof(dp));
  for (int l = 0; l < Lmax + 1; l++) {
    if (l < W[1])
      dp[1][l] = 0LL;
    else
      dp[1][l] = V[1];
  }

  for (int i = 2; i <= (int)sqrt(N); i++) {
    for (int l = 0; l < Lmax + 1; l++) {
      if (l - W[i] >= 0)
        dp[i][l] = max(dp[i / 2][l], dp[i / 2][l - W[i]] + V[i]);
      else
        dp[i][l] = dp[i / 2][l];
    }
  }

  DBG(dp)
  for (int q = 0; q < Q; q++) {
    cout << rec(vq[q], Lq[q]) << "\\n";
  }

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

ll N;
vector<ll> V, W;
void input() {
  fast_io();
  cin >> N;
  V.push_back(0LL);
  W.push_back(0LL);
  for (int i = 0; i < N; i++) {
    ll v, w;
    cin >> v >> w;
    V.push_back(v);
    W.push_back(w);
  }
}

template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}

ll N_solve(int N, vector<ll> v, vector<ll> w, ll L) {
  ll ans = 0;
  int n1 = N / 2, n2 = N - n1;
  vector<pair<int, ll>> a;
  for (int i = 0; i < (1 << n1); i++) {
    ll t = 0, u = 0;
    for (int j = 0; j < n1; j++)
      if ((i >> j) & 1)
        t += v[j], u += w[j];
    if (u <= L)
      a.push_back(make_pair((int)(L - u), t));
  }
  sort(a.begin(), a.end());
  vector<ll> dp(a.size() + 1);
  for (int i = a.size() - 1; i >= 0; --i)
    dp[i] = max(dp[i + 1], a[i].second);

  for (int i = 0; i < (1 << n2); i++) {
    ll t = 0, u = 0;
    for (int j = 0; j < n2; j++)
      if ((i >> j) & 1)
        t += v[n1 + j], u += w[n1 + j];
    if (u <= L) {
      int k =
          lower_bound(a.begin(), a.end(), make_pair((int)u, 0LL)) - a.begin();
      amax(ans, t + dp[k]);
    }
  }
  return ans;
}

ll N_solveA(int N, vector<ll> v, vector<ll> w, ll L, vector<pair<int, ll>> A) {
  ll ans = 0;
  int n1 = 0, n2 = N - n1;
  vector<pair<int, ll>> a;
  for (auto &x : A) {
    x.first += L;
    if (x.first >= 0)
      a.push_back(x);
  }
  vector<ll> dp(a.size() + 1);
  for (int i = a.size() - 1; i >= 0; --i)
    dp[i] = max(dp[i + 1], a[i].second);

  for (int i = 0; i < (1 << n2); i++) {
    ll t = 0, u = 0;
    for (int j = 0; j < n2; j++)
      if ((i >> j) & 1)
        t += v[n1 + j], u += w[n1 + j];
    if (u <= L) {
      int k =
          lower_bound(a.begin(), a.end(), make_pair((int)u, 0LL)) - a.begin();
      amax(ans, t + dp[k]);
    }
  }
  return ans;
}

int TLEsolve() {
  // dp.assign(N+10,vector<ll>(110000,0LL));
  int Q;
  cin >> Q;
  vector<vector<pair<int, ll>>> A(sqrt(N) + 10);

  for (int t = 0; t < sqrt(N); t++) {
    vector<pair<int, ll>> a;
    vector<ll> v, w;
    int idx = t;
    while (idx) {
      v.push_back(V[idx]);
      w.push_back(W[idx]);
      idx >>= 1;
    }
    int n1 = v.size();
    for (int i = 0; i < (1 << n1); i++) {
      ll t = 0, u = 0;
      for (int j = 0; j < n1; j++)
        if ((i >> j) & 1)
          t += v[j], u += w[j];
      a.push_back(make_pair((int)(-u), t));
    }
    sort(a.begin(), a.end());
    A[t] = a;
  }

  for (int q = 0; q < Q; q++) {
    ll I, L;
    cin >> I >> L;
    // I--;
    vector<ll> v, w;
    if (I <= sqrt(N)) {
      while (I) {
        v.push_back(V[I]);
        w.push_back(W[I]);
        I /= 2;
      }
      int M = v.size();
      cout << N_solve(M, v, w, L) << "\\n";
    } else {
      while (I > sqrt(N)) {
        v.push_back(V[I]);
        w.push_back(W[I]);
        I /= 2;
      }
      int M = v.size();

      cout << N_solveA(M, v, w, L, A[I]) << "\\n";
    }

    // cout << rec(v,L) << "\\n";
  }

  return 0;
}

// ll dp[1<<19][110000];
vector<vector<ll>> dp;
// TLE
ll rec(int i, int l) {
  if (l < 0LL)
    return -(ll)(1e7);
  if (dp[i][l] != -1)
    return dp[i][l];
  if (i == 1) {
    if (l < W[i])
      return 0LL;
    else
      return V[i];
  }
  // return dp[i][l] = max(rec(i/2,l),rec(i/2,l-W[i])+V[i]);
  return dp[i][l] = max(rec(i / 2, l), rec(i / 2, l - W[i]) + V[i]);
}
int solve() {

  int Q;
  cin >> Q;
  int Lmax = 0;
  vector<int> vq, Lq;
  for (int q = 0; q < Q; q++) {
    int v, L;
    cin >> v >> L;
    // v--;
    vq.push_back(v);
    Lq.push_back(L);
    amax(Lmax, L);
  }

  dp.assign(N + 10, vector<ll>(Lmax + 10, -1));
  // memset(dp,-1,sizeof(dp));
  for (int l = 0; l < Lmax + 1; l++) {
    if (l < W[1])
      dp[1][l] = 0LL;
    else
      dp[1][l] = V[1];
  }

  for (int i = 2; i <= sqrt(N); i++) {
    for (int l = 0; l < Lmax + 1; l++) {
      if (l - W[i] >= 0)
        dp[i][l] = max(dp[i / 2][l], dp[i / 2][l - W[i]] + V[i]);
      else
        dp[i][l] = dp[i / 2][l];
    }
  }

  DBG(dp)
  for (int q = 0; q < Q; q++) {
    cout << rec(vq[q], Lq[q]) << "\\n";
  }

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}