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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int INF = 1e9;
const int MAX_K = 1 << 10;

struct P {
  int v, w;
  P() {}
  P(int v, int w) : v(v), w(w) {}
};
bool operator<(const P &a, const P &b) { return a.w < b.w; }
P operator+(const P &a, const P &b) { return {a.v + b.v, a.w + b.w}; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.w << "," << a.v << ")";
}

vector<P> S[MAX_K];

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<P> nodes(N);
  rep(i, N) {
    int v, w;
    cin >> v >> w;
    nodes[i] = P(v, w);
  }
  S[0].pb(P(0, 0));
  int K = min(MAX_K, N + 1);
  FOR(i, 1, K - 1) {
    int p = i / 2;
    for (P e : S[p]) {
      S[i].pb(e);
      S[i].pb(e + nodes[i - 1]);
    }
    sort(all(S[i]));
    int M = S[i].size();
    int m = 1;
    FOR(j, 1, M - 1) {
      if (S[i][m - 1].v < S[i][j].v) {
        S[i][m++] = S[i][j];
      }
    }
    S[i].erase(S[i].begin() + m, S[i].end());
  }
  // for(P e:nodes){
  //     cout<<e<<" ";
  // }cout<<endl;
  // rep(v,K){
  //     for(P e:S[v]){
  //         cout<<e<<" ";
  //     }cout<<endl;
  // }

  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    vector<P> ps;
    ps.pb(P(0, 0));
    while (v >= K) {
      int M = ps.size();
      rep(i, M) { ps.pb(ps[i] + nodes[v - 1]); }
      v >>= 1;
    }
    int ans = 0;
    for (P e : ps) {
      if (L < e.w)
        continue;
      auto itr = upper_bound(all(S[v]), P(0, L - e.w));
      itr--;
      chmax(ans, e.v + itr->v);
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int INF = 1e9;
const int MAX_K = 1;

struct P {
  int v, w;
  P() {}
  P(int v, int w) : v(v), w(w) {}
};
bool operator<(const P &a, const P &b) { return a.w < b.w; }
P operator+(const P &a, const P &b) { return {a.v + b.v, a.w + b.w}; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.w << "," << a.v << ")";
}

vector<P> S[MAX_K];

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<P> nodes(N);
  rep(i, N) {
    int v, w;
    cin >> v >> w;
    nodes[i] = P(v, w);
  }
  S[0].pb(P(0, 0));
  int K = min(MAX_K, N + 1);
  FOR(i, 1, K - 1) {
    int p = i / 2;
    for (P e : S[p]) {
      S[i].pb(e);
      S[i].pb(e + nodes[i - 1]);
    }
    sort(all(S[i]));
    int M = S[i].size();
    int m = 1;
    FOR(j, 1, M - 1) {
      if (S[i][m - 1].v < S[i][j].v) {
        S[i][m++] = S[i][j];
      }
    }
    S[i].erase(S[i].begin() + m, S[i].end());
  }
  // for(P e:nodes){
  //     cout<<e<<" ";
  // }cout<<endl;
  // rep(v,K){
  //     for(P e:S[v]){
  //         cout<<e<<" ";
  //     }cout<<endl;
  // }

  int Q;
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    vector<P> ps;
    ps.pb(P(0, 0));
    while (v >= K) {
      int M = ps.size();
      rep(i, M) { ps.pb(ps[i] + nodes[v - 1]); }
      v >>= 1;
    }
    int ans = 0;
    for (P e : ps) {
      if (L < e.w)
        continue;
      auto itr = upper_bound(all(S[v]), P(0, L - e.w));
      itr--;
      chmax(ans, e.v + itr->v);
    }
    cout << ans << "\\n";
  }
  return 0;
}