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

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int nextLong() {
  int x;
  scanf("%d", &x);
  return x;
}

const int MAX_N = (1 << 18) + 10;
int V[MAX_N], W[MAX_N];

const int M = 10;
vector<pair<int, int>> half[1 << M];

vector<pair<int, int>> sub(const int _v, const int root = 0) {
  if (_v < (1 << M)) {
    if (half[_v].size())
      return half[_v];
  }

  vector<int> val;
  vector<int> weight;
  for (int v = _v; v > root; v /= 2) {
    val.push_back(V[v]);
    weight.push_back(W[v]);
  }

  vector<pair<int, int>> nap;
  const int S = val.size();

  nap.push_back({0, 0});
  REP(i, S) {
    vector<pair<int, int>> nap2 = nap, merged;
    REP(j, nap2.size()) {
      nap2[j].first += weight[i];
      nap2[j].second += val[i];
    }
    size_t a = 0, b = 0;
    while (a < nap.size() || b < nap2.size()) {
      if (a == nap.size()) {
        merged.push_back(nap2[b++]);
      } else if (b == nap2.size()) {
        merged.push_back(nap[a++]);
      } else if (nap[a] < nap2[b]) {
        merged.push_back(nap[a++]);
      } else {
        merged.push_back(nap2[b++]);
      }
    }

    nap.clear();
    nap.push_back(merged[0]);
    for (int i = 1; i < (int)merged.size(); i++) {
      if (nap.back().first == merged[i].first) {
        nap.back().second = merged[i].second;
      } else if (nap.back().second < merged[i].second) {
        nap.push_back(merged[i]);
      }
    }
  }
  if (_v < (1 << M))
    half[_v] = nap;
  return nap;
}

int main2() {
  CLR(V, 0);
  CLR(W, 0);
  REP(i, 1 << M) half[i].clear();

  int N = nextLong();
  REP(i, N) {
    V[i + 1] = nextLong();
    W[i + 1] = nextLong();
  }

  for (int v = 1 << (M - 1); v < (1 << M); v++) {
    sub(v);
  }

  int Q = nextLong();
  REP(_, Q) {
    int v = nextLong();
    int L = nextLong();

    int ans = 0;
    if (v < (1 << M)) {
      const auto &res = sub(v);
      auto it = upper_bound(ALL(res), pair<int, int>(L, 1001001001));
      if (it == res.begin()) {
        ans = 0;
      } else {
        ans = prev(it)->second;
      }
    } else {

      int root = v;
      while (root >= (1 << M))
        root /= 2;
      const auto &bottom = sub(v, root);
      const auto &up = sub(root);

      auto it = prev(up.end());
      for (auto a = bottom.begin(); a != bottom.end() && a->first <= L; ++a) {
        int remain = L - a->first;
        while (it != up.begin() && it->first > remain)
          --it;
        int val2 = (it->first > remain) ? 0 : it->second;
        chmax(ans, val2 + a->second);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

int main() {
  // int N = 1 << 14;
  // int Q = 10000;
  // cout << N << endl;
  // REP(i, N) {
  //   cout << 1 + rand() % 1000 << ' ' << 1 + rand() % 1000 << endl;
  // }
  // cout << Q << endl;
  // REP(i, Q) {
  //   cout << 1 + rand() % N << " " << 1 + rand() % 100000 << endl;
  // }
  // return 0;
#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int nextLong() {
  int x;
  scanf("%d", &x);
  return x;
}

const int MAX_N = (1 << 18) + 10;
int V[MAX_N], W[MAX_N];

const int M = 10;
vector<pair<int, int>> half[1 << M];

vector<int> val, weight;
void dfs(int d, map<int, int> &nap, int sum_v, int sum_w) {
  if (d < 0) {
    chmax(nap[sum_w], sum_v);
    return;
  }
  dfs(d - 1, nap, sum_v, sum_w);
  dfs(d - 1, nap, sum_v + val[d], sum_w + weight[d]);
}

vector<pair<int, int>> sub(const int _v, const int root = 0) {
  if (_v < (1 << M)) {
    if (half[_v].size())
      return half[_v];
  }

  val.clear();
  weight.clear();
  for (int v = _v; v > root; v /= 2) {
    val.push_back(V[v]);
    weight.push_back(W[v]);
  }

  map<int, int> nap;
  const int S = val.size();
  dfs(S - 1, nap, 0, 0);
  // for (int bs = 0; bs < (1 << S); bs++) {

  //   int sum_v = 0, sum_w = 0;
  //   for (int i = 0; i < S; i++) if (bs >> i & 1) {
  //     sum_v += val[i];
  //     sum_w += weight[i];
  //   }
  //   chmax(nap[sum_w], sum_v);
  // }

  vector<int> keys;
  for (auto p : nap)
    keys.push_back(p.first);
  for (int i = 1; i < (int)keys.size(); i++) {
    chmax(nap[keys[i]], nap[keys[i - 1]]);
  }

  vector<pair<int, int>> result;
  for (auto p : nap)
    result.push_back(p);
  if (_v < (1 << M))
    half[_v] = result;
  return result;
}

int main2() {
  CLR(V, 0);
  CLR(W, 0);
  REP(i, 1 << M) half[i].clear();

  int N = nextLong();
  REP(i, N) {
    V[i + 1] = nextLong();
    W[i + 1] = nextLong();
  }

  for (int v = 1 << (M - 1); v < (1 << M); v++) {
    sub(v);
  }

  int Q = nextLong();
  REP(_, Q) {
    int v = nextLong();
    int L = nextLong();

    int ans = 0;
    if (v < (1 << M)) {
      const auto &res = sub(v);
      auto it = upper_bound(ALL(res), pair<int, int>(L, 1001001001));
      if (it == res.begin()) {
        ans = 0;
      } else {
        ans = prev(it)->second;
      }
    } else {

      int root = v;
      while (root >= (1 << M))
        root /= 2;
      const auto &bottom = sub(v, root);
      const auto &up = sub(root);

      auto it = prev(up.end());
      for (auto a = bottom.begin(); a != bottom.end() && a->first <= L; ++a) {
        int remain = L - a->first;
        while (it != up.begin() && it->first > remain)
          --it;
        int val2 = (it->first > remain) ? 0 : it->second;
        chmax(ans, val2 + a->second);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

int main() {
  // int N = 1 << 14;
  // int Q = 10000;
  // cout << N << endl;
  // REP(i, N) {
  //   cout << 1 + rand() % 1000 << ' ' << 1 + rand() % 1000 << endl;
  // }
  // cout << Q << endl;
  // REP(i, Q) {
  //   cout << 1 + rand() % N << " " << 1 + rand() % 100000 << endl;
  // }
  // return 0;
#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}