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

#include <bits/stdc++.h>

using namespace std;

#define FOE(i, s, t) for (int i = s; i <= t; i++)
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define FOD(i, s, t) for (int i = s; i >= t; i--)

#define LL long long
#define mp make_pair
#define pb push_back

#define K 500001
#define R (1 << 11)

int n, v[K], w[K];

vector<pair<int, int>> dect[K];
set<pair<int, int>> C[K];

int finish[K];

int solve(int a, int p) {
  if (p == 0)
    return 0;
  if (a == 0)
    return 0;

  int ret;

  if (a >= R) {
    return max(solve(a / 2, p),
               (p - w[a] >= 0 ? solve(a / 2, p - w[a]) + v[a] : 0));
  } else {
    if (!finish[a]) {
      finish[a] = 1;
      sort(dect[a].begin(), dect[a].end());

      int last = 0;
      FOR(j, 0, dect[a].size()) {
        int we = dect[a][j].first;
        int val = -dect[a][j].second;

        if (val > last) {
          last = val;
          C[a].insert(mp(-we, val));
          //	printf("%d: insert %d %d\\n", a, -we, val);
        }
      }
    }

    if (C[a].lower_bound(mp(-p, -1)) == C[a].end()) {
      return 0;
    } else {
      pair<int, int> T = *(C[a].lower_bound(mp(-p, -1)));

      //	printf("T = %d %d %d\\n", T.first, T.second,
      //(*(C[a].lower_bound(mp(-p, -1)))).second * -1);
      return (*(C[a].lower_bound(mp(-p, -1)))).second;
    }
  }
}

void precompute(int u, int val, int we) {
  if (u >= R)
    return;
  if (we > 100000)
    return;

  dect[u].pb(mp(we, -val));
  precompute(u * 2, val, we);
  precompute(u * 2 + 1, val, we);

  val += v[u];
  we += w[u];

  dect[u].pb(mp(we, -val));
  precompute(u * 2, val, we);
  precompute(u * 2 + 1, val, we);
}

int main() {
  //	freopen("1.in", "r", stdin);
  scanf("%d", &n);

  FOE(i, 1, n) { scanf("%d%d", &v[i], &w[i]); }

  precompute(1, 0, 0);
  cerr << clock() << endl;

  int q;
  scanf("%d", &q);

  int ret = 0;
  while (q--) {
    int q, p;
    scanf("%d%d", &q, &p);

    // ret += solve(q, p);
    printf("%d\\n", solve(q, p));
  }
  //	cout << ret << endl;
  cerr << clock() << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOE(i, s, t) for (int i = s; i <= t; i++)
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define FOD(i, s, t) for (int i = s; i >= t; i--)

#define LL long long
#define mp make_pair
#define pb push_back

#define K 500001
#define R (1 << 12)

int n, v[K], w[K];

vector<pair<int, int>> dect[K];
set<pair<int, int>> C[K];

int finish[K];

int solve(int a, int p) {
  if (p == 0)
    return 0;
  if (a == 0)
    return 0;

  int ret;

  if (a >= R) {
    return max(solve(a / 2, p),
               (p - w[a] >= 0 ? solve(a / 2, p - w[a]) + v[a] : 0));
  } else {
    if (!finish[a]) {
      finish[a] = 1;
      sort(dect[a].begin(), dect[a].end());

      int last = 0;
      FOR(j, 0, dect[a].size()) {
        int we = dect[a][j].first;
        int val = -dect[a][j].second;

        if (val > last) {
          last = val;
          C[a].insert(mp(-we, val));
          //	printf("%d: insert %d %d\\n", a, -we, val);
        }
      }
    }

    if (C[a].lower_bound(mp(-p, -1)) == C[a].end()) {
      return 0;
    } else {
      pair<int, int> T = *(C[a].lower_bound(mp(-p, -1)));

      //	printf("T = %d %d %d\\n", T.first, T.second,
      //(*(C[a].lower_bound(mp(-p, -1)))).second * -1);
      return (*(C[a].lower_bound(mp(-p, -1)))).second;
    }
  }
}

void precompute(int u, int val, int we) {
  if (u >= R)
    return;

  dect[u].pb(mp(we, -val));
  precompute(u * 2, val, we);
  precompute(u * 2 + 1, val, we);

  val += v[u];
  we += w[u];

  dect[u].pb(mp(we, -val));
  precompute(u * 2, val, we);
  precompute(u * 2 + 1, val, we);
}

int main() {
  //	freopen("1.in", "r", stdin);
  scanf("%d", &n);

  FOE(i, 1, n) { scanf("%d%d", &v[i], &w[i]); }

  precompute(1, 0, 0);
  cerr << clock() << endl;

  int q;
  scanf("%d", &q);

  int ret = 0;
  while (q--) {
    int q, p;
    scanf("%d%d", &q, &p);

    printf("%d\\n", solve(q, p));
  }
  cerr << clock() << endl;
}