Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

ll N;
vll V, W;
ll half = (1LL << 9);

vvpll each_table(1LL << 8, vpll(1LL << 9, make_pair(0, 0)));
ll each_table_size = 1;

vpll table1(half + 10);
vpll table2(half + 10);
vll table2_value_max(half);

void init(void) {
  FOR(h, 1LL << 8, 1LL << 9) {
    ll v = h;
    ll tmp = h - (1LL << 8);
    ll t_size = 1;
    while (v > 0) {
      REP(i, t_size) {
        each_table[tmp][t_size + i].first = each_table[tmp][i].first + W[v - 1];
        each_table[tmp][t_size + i].second =
            each_table[tmp][i].second + V[v - 1];
      }
      t_size *= 2;
      each_table_size = t_size;
      v /= 2;
    }
    sort(each_table[tmp].begin(), each_table[tmp].begin() + t_size);
  }
  return;
}

void solve(ll v, ll L) {
  ll ans = 0;

  table1[0] = make_pair(0, 0);
  table2[0] = make_pair(0, 0);

  ll table1_size = 1;
  ll table2_size = 1;

  if (v >= half) {
    while (v >= half) {
      REP(i, table2_size) {
        table2[table2_size + i].first = table2[i].first + W[v - 1];
        table2[table2_size + i].second = table2[i].second + V[v - 1];
      }
      table2_size *= 2;
      v /= 2;
    }
    sort(table2.begin(), table2.begin() + table2_size);

    table1 = each_table[v - (1LL << 8)];
    table1_size = each_table_size;
  } else {
    while (v > 0) {
      REP(i, table1_size) {
        table1[table1_size + i].first = table1[i].first + W[v - 1];
        table1[table1_size + i].second = table1[i].second + V[v - 1];
      }
      table1_size *= 2;
      v /= 2;
    }
    sort(table1.begin(), table1.begin() + table1_size);
  }

  table2_value_max[0] = table2[0].second;
  REP(i, table2_size) {
    table2_value_max[i + 1] = max(table2_value_max[i], table2[i + 1].second);
  }

  ll end = table2_size - 1;
  REP(i, table1_size) {
    while (table1[i].first + table2[end].first > L) {
      end--;
      if (end < 0)
        break;
    }
    if (end < 0)
      break;

    chmax(ans, table1[i].second + table2_value_max[end]);
  }

  OUT(ans);
};

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  IN(N);
  V.resize(N);
  W.resize(N);
  REP(i, N) { IN(V[i], W[i]); }

  init();

  ll Q;
  IN(Q);
  ll v, L;

  REP(q, Q) {
    IN(v, L);
    solve(v, L);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

ll N;
vll V, W;
ll half = (1LL << 9);

vpll table1(half);
vpll table2(half);
vll table2_value_max(half);

void solve(ll v, ll L) {
  ll ans = 0;

  table1[0] = make_pair(0, 0);
  table2[0] = make_pair(0, 0);

  ll table1_size = 1;
  ll table2_size = 1;

  while (v > 0) {
    if (v >= half) {
      REP(i, table2_size) {
        table2[table2_size + i].first = table2[i].first + W[v - 1];
        table2[table2_size + i].second = table2[i].second + V[v - 1];
      }
      table2_size *= 2;
    } else {
      REP(i, table1_size) {
        table1[table1_size + i].first = table1[i].first + W[v - 1];
        table1[table1_size + i].second = table1[i].second + V[v - 1];
      }
      table1_size *= 2;
    }
    v /= 2;
  }

  sort(table1.begin(), table1.begin() + table1_size);
  sort(table2.begin(), table2.begin() + table2_size);

  table2_value_max[0] = table2[0].second;
  REP(i, table2_size) {
    table2_value_max[i + 1] = max(table2_value_max[i], table2[i + 1].second);
  }

  ll end = table2_size - 1;
  REP(i, table1_size) {
    while (table1[i].first + table2[end].first > L) {
      end--;
      if (end < 0)
        break;
    }
    if (end < 0)
      break;

    chmax(ans, table1[i].second + table2_value_max[end]);
  }

  OUT(ans);
};

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  IN(N);
  V.resize(N);
  W.resize(N);
  REP(i, N) { IN(V[i], W[i]); }

  ll Q;
  IN(Q);
  ll v, L;

  REP(q, Q) {
    IN(v, L);
    solve(v, L);
  }

  return 0;
}