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

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \\
  do {                                                                         \\
    cout << "[";                                                               \\
    for (int count = (a); count < (b); ++count)                                \\
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \\
    cout << "]\\n";                                                             \\
  } while (0)
#define p_map(map, it)                                                         \\
  do {                                                                         \\
    cout << "{";                                                               \\
    for (auto(it) = map.begin();; ++(it)) {                                    \\
      if ((it) == map.end()) {                                                 \\
        cout << "}\\n";                                                         \\
        break;                                                                 \\
      } else                                                                   \\
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \\
    }                                                                          \\
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\\n';

int main() {
  int n, M = 1024;
  cin >> n;
  // vector<vector<ll>> dp(19,vector<ll>(100010,0));
  vector<int> v(n, 0), w(n, 1e5);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  vector<vector<P>> a(n);
  a[0] = {{0, 0}, {w[0], v[0]}};
  for (int i = 1; i < min(n, M - 1); ++i) {
    a[i] = a[(i - 1) / 2];
    for (P j : a[(i - 1) / 2]) {
      if (j.first + w[i] > 1e5)
        break;
      a[i].push_back({j.first + w[i], j.second + v[i]});
    }
    sort(a[i].begin(), a[i].end());
    for (int j = 1; j < a[i].size(); ++j) {
      if (a[i][j - 1].first == a[i][j].first)
        a[i][j - 1] = a[i][j];
      else if (a[i][j - 1].second > a[i][j].second)
        a[i][j] = a[i][j - 1];
    }
    // sort(a[i].begin(),a[i].end());
    a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
  }
  for (int i = M - 1; i < min(n, M * 2 - 1); ++i)
    a[i] = {{0, 0}, {w[i], v[i]}};
  for (int i = M * 2 - 1; i < n; ++i) {
    a[i] = a[(i - 1) / 2];
    for (P j : a[(i - 1) / 2]) {
      if (j.first + w[i] > 1e5)
        break;
      a[i].push_back({j.first + w[i], j.second + v[i]});
    }
    sort(a[i].begin(), a[i].end());
    for (int j = 1; j < a[i].size(); ++j) {
      if (a[i][j - 1].first == a[i][j].first)
        a[i][j - 1] = a[i][j];
      else if (a[i][j - 1].second > a[i][j].second)
        a[i][j] = a[i][j - 1];
    }
    // sort(a[i].begin(),a[i].end());
    a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
  }
  int q;
  cin >> q;
  while (q--) {
    int b, l;
    cin >> b >> l;
    b--;
    if (b < M - 1) {
      cout << prev(upper_bound(a[b].begin(), a[b].end(), P(l, 1e9)))->second
           << newl;
    } else {
      int c = b;
      while (c >= M - 1)
        c = (c - 1) / 2;
      int j = a[c].size() - 1;
      int ans = 0;
      for (int i = 0; i < a[b].size(); ++i) {
        if (a[b][i].first > l)
          break;
        while (a[b][i].first + a[c][j].first > l)
          j--;
        ans = max(ans, a[b][i].second + a[c][j].second);
      }
      cout << ans << newl;
    }
  }
  // for (int i = 0;i < n;++i) p_ary(a[i],0,a[i].size());
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \\
  do {                                                                         \\
    cout << "[";                                                               \\
    for (int count = (a); count < (b); ++count)                                \\
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \\
    cout << "]\\n";                                                             \\
  } while (0)
#define p_map(map, it)                                                         \\
  do {                                                                         \\
    cout << "{";                                                               \\
    for (auto(it) = map.begin();; ++(it)) {                                    \\
      if ((it) == map.end()) {                                                 \\
        cout << "}\\n";                                                         \\
        break;                                                                 \\
      } else                                                                   \\
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \\
    }                                                                          \\
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\\n';

int main() {
  int n, M = 512;
  cin >> n;
  // vector<vector<ll>> dp(19,vector<ll>(100010,0));
  vector<int> v(n, 0), w(n, 1e5);
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  vector<vector<P>> a(n);
  a[0] = {{0, 0}, {w[0], v[0]}};
  for (int i = 1; i < min(n, M - 1); ++i) {
    a[i] = a[(i - 1) / 2];
    for (P j : a[(i - 1) / 2]) {
      if (j.first + w[i] > 1e5)
        break;
      a[i].push_back({j.first + w[i], j.second + v[i]});
    }
    sort(a[i].begin(), a[i].end());
    for (int j = 1; j < a[i].size(); ++j) {
      if (a[i][j - 1].first == a[i][j].first)
        a[i][j - 1] = a[i][j];
      else if (a[i][j - 1].second > a[i][j].second)
        a[i][j] = a[i][j - 1];
    }
    // sort(a[i].begin(),a[i].end());
    a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
  }
  for (int i = M - 1; i < min(n, M * 2 - 1); ++i)
    a[i] = {{0, 0}, {w[i], v[i]}};
  for (int i = M * 2 - 1; i < n; ++i) {
    a[i] = a[(i - 1) / 2];
    for (P j : a[(i - 1) / 2]) {
      if (j.first + w[i] > 1e5)
        break;
      a[i].push_back({j.first + w[i], j.second + v[i]});
    }
    sort(a[i].begin(), a[i].end());
    for (int j = 1; j < a[i].size(); ++j) {
      if (a[i][j - 1].first == a[i][j].first)
        a[i][j - 1] = a[i][j];
      else if (a[i][j - 1].second > a[i][j].second)
        a[i][j] = a[i][j - 1];
    }
    // sort(a[i].begin(),a[i].end());
    a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
  }
  int q;
  cin >> q;
  while (q--) {
    int b, l;
    cin >> b >> l;
    b--;
    if (b < M - 1) {
      cout << prev(upper_bound(a[b].begin(), a[b].end(), P(l, 1e9)))->second
           << newl;
    } else {
      int c = b;
      while (c >= M - 1)
        c = (c - 1) / 2;
      int j =
          upper_bound(a[c].begin(), a[c].end(), P(l + 1, 0)) - a[c].begin() - 1;
      int ans = 0;
      for (int i = 0; i < a[b].size(); ++i) {
        if (a[b][i].first > l)
          break;
        while (a[b][i].first + a[c][j].first > l)
          j--;
        ans = max(ans, a[b][i].second + a[c][j].second);
      }
      cout << ans << newl;
    }
  }
  // for (int i = 0;i < n;++i) p_ary(a[i],0,a[i].size());
}