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

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int maxn = 1 << 18;
constexpr int maxm = 1 << 10;
int V[maxn], W[maxn], v[maxm], w[maxm], Log[maxn], sv[maxm], sw[maxm];
map<int, int> mp[maxm];
int test = 0;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N = (1 << 18) - 1;
  for (int i = 2; i < maxn; i += 1)
    Log[i] = Log[i / 2] + 1;
  if (not test)
    cin >> N;
  mp[0][0] = 0;
  for (int i = 1; i <= N; i += 1) {
    if (not test)
      cin >> V[i] >> W[i];
    else {
      V[i] = rand() % maxn;
      W[i] = rand() % maxn;
    }
    if (i < maxm) {
      for (auto p : mp[i / 2]) {
        mp[i][p.first] = max(mp[i][p.first], p.second);
        mp[i][p.first + W[i]] = max(mp[i][p.first + W[i]], p.second + V[i]);
      }
      int m = 0;
      for (auto &p : mp[i])
        m = p.second = max(m, p.second);
    }
  }
  int Q = 100000;
  if (not test)
    cin >> Q;
  for (int _ = 1, u = 0, F = 0; _ <= Q; _ += 1) {
    if (not test)
      cin >> u >> F;
    else {
      u = rand() % N + 1;
      F = rand() % (maxn * 18);
    }
    int n = 0;
    for (; u >= maxm; u >>= 1, n += 1) {
      v[n] = V[u];
      w[n] = W[u];
    }
    for (int i = 1; i < (1 << n); i += 1) {
      sv[i] = sv[i - (1 << Log[i])] + v[Log[i]];
      sw[i] = sw[i - (1 << Log[i])] + w[Log[i]];
    }
    int ans = 0;
    for (int i = 0; i < (1 << n); i += 1) {
      auto it = mp[u].upper_bound(F - sw[i]);
      if (it == mp[u].begin())
        continue;
      ans = max(ans, prev(it)->second + sv[i]);
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int maxn = 1 << 18;
int A[maxn];
int V[maxn], W[maxn];
int v[18], w[18];
pair<int, int> L[1 << 9], R[1 << 9];
int Log[1 << 9];
int test = 0;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N = (1 << 18) - 1;
  for (int i = 2; i < (1 << 9); i += 1)
    Log[i] = Log[i / 2] + 1;
  if (not test)
    cin >> N;
  for (int i = 1; i <= N; i += 1)
    if (not test)
      cin >> V[i] >> W[i];
  int Q = 100000;
  if (not test)
    cin >> Q;
  for (int _ = 1, u = 0, F = 0; _ <= Q; _ += 1) {
    if (not test)
      cin >> u >> F;
    fill(v, v + 18, 0);
    fill(w, w + 18, 0);
    for (int i = 0; u; u >>= 1, i += 1) {
      v[i] = V[u];
      w[i] = W[u];
    }
    for (int i = 1; i < (1 << 9); i += 1) {
      L[i] = L[i - (1 << Log[i])];
      L[i].first += w[Log[i]];
      L[i].second += v[Log[i]];
      R[i] = R[i - (1 << Log[i])];
      R[i].first += w[Log[i] + 9];
      R[i].second += v[Log[i] + 9];
    }
    sort(L, L + (1 << 9));
    sort(R, R + (1 << 9));
    for (int i = 1; i < (1 << 9); i += 1) {
      L[i].second = max(L[i].second, L[i - 1].second);
      R[i].second = max(R[i].second, R[i - 1].second);
    }
    int ans = 0;
    for (int i = 0, j = (1 << 9) - 1; i < (1 << 9); i += 1) {
      while (j >= 0 and L[i].first + R[j].first > F)
        j -= 1;
      if (j < 0)
        break;
      ans = max(ans, L[i].second + R[j].second);
    }
    cout << ans << "\\n";
  }
  return 0;
}