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

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cinttypes>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    is >> *i;
  return is;
}
struct fast_io {
  fast_io &operator>>(int &target) {
    scanf("%" PRId32, &target);
    return *this;
  }
  fast_io &operator>>(unsigned int &target) {
    scanf("%" PRIu32, &target);
    return *this;
  }
  fast_io &operator>>(long long &target) {
    scanf("%" PRId64, &target);
    return *this;
  }
  fast_io &operator>>(unsigned long long &target) {
    scanf("%" PRIu64, &target);
    return *this;
  }
  template <typename T> fast_io &operator>>(vector<T> &target) {
    for (auto &value : target) {
      *this >> value;
    }
    return *this;
  }
  fast_io &operator<<(const int value) {
    printf("%" PRId32, value);
    return *this;
  }
  fast_io &operator<<(const unsigned int value) {
    printf("%" PRIu32, value);
    return *this;
  }
  fast_io &operator<<(const long long value) {
    printf("%" PRId64, value);
    return *this;
  }
  fast_io &operator<<(const unsigned long long value) {
    printf("%" PRIu64, value);
    return *this;
  }
  fast_io &operator<<(const string &value) {
    printf("%s", value.c_str());
    return *this;
  }
  template <typename T> fast_io &operator<<(const vector<T> &target) {
    for (auto &value : target) {
      *this << value << " ";
    }
    return *this;
  }
};
fast_io fio;

template <class ForwardIt, class UnaryPredicate>
ForwardIt first_true(ForwardIt first, ForwardIt last,
                     UnaryPredicate predicate) {
  ForwardIt it;
  typename std::iterator_traits<ForwardIt>::difference_type count, step;
  count = std::distance(first, last);
  while (count > 0) {
    it = first;
    step = count / 2;
    std::advance(it, step);
    if (!predicate(*it)) {
      first = ++it;
      count -= step + 1;
    } else
      count = step;
  }
  return first;
}

#define MOD 1000000007
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define sort_all(x) sort(all(x))
#define make_unique(x) (x).erase(unique(all(x)), (x).end())

vector<int> G[1 << 19];
vector<int> v, w;
vector<pii> Ls[1 << 19];
vector<int> ans;

vector<pii> step(vector<pii> &orig, int w, int v) {
  vector<pii> orig2;
  orig2.push_back({w, v});
  for (auto x : orig) {
    orig2.push_back({x.first + w, x.second + v});
  }
  vector<pii> ans;
  std::merge(orig.begin(), orig.end(), orig2.begin(), orig2.end(),
             std::back_inserter(ans));
  vector<pii> ans2;
  int max_v = 0;
  for (auto &x : ans) {
    if (x.second < max_v)
      continue;
    ans2.push_back(x);
    max_v = max(max_v, x.second);
  }
  return ans2;
}

int handle_ans(int i, int L, vector<pii> &first, vector<pii> &second) {
  int max_ans = 0;
  for (auto &x : first) {
    if (x.first <= L) {
      max_ans = max(max_ans, x.second);
    }
  }

  if (second.size() != 0) {
    for (auto &x : second) {
      if (x.first <= L)
        max_ans = max(max_ans, x.second);
      if (x.first > L)
        continue;
      int rem = L - x.first;
      if (rem <= 0)
        continue;

      auto it = first_true(first.begin(), first.end(),
                           [rem](pii &a) { return a.first > rem; });
      if (it != first.begin()) {
        it--;
        max_ans = max(max_ans, x.second + it->second);
      }
    }
  }
  return max_ans;
}

void dfs(int i, int level, vector<pii> &first, vector<pii> &second) {
  if (level <= 8) {
    vector<pii> new_first = step(first, w[i], v[i]);
    for (auto x : Ls[i]) {
      ans[x.first] = handle_ans(i, x.second, new_first, second);
    }
    for (int u : G[i]) {
      dfs(u, level + 1, new_first, second);
    }
  } else {
    vector<pii> new_second = step(second, w[i], v[i]);
    for (auto x : Ls[i]) {
      ans[x.first] = handle_ans(i, x.second, first, new_second);
    }
    for (int u : G[i]) {
      dfs(u, level + 1, first, new_second);
    }
  }
}

int main() {

#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  fio >> n;

  for (int i = 1; i < n; i++) {
    int parent = (i + 1) / 2 - 1;
    G[parent].push_back(i);
  }

  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; i++) {
    fio >> v[i] >> w[i];
  }

  int q;
  fio >> q;
  ans.resize(q);
  for (int i = 0; i < q; i++) {
    int v, L;
    fio >> v >> L;
    v--;
    Ls[v].push_back({i, L});
  }

  vector<pii> a, b;
  dfs(0, 0, a, b);
  for (int x : ans)
    fio << x << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cinttypes>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    is >> *i;
  return is;
}
struct fast_io {
  fast_io &operator>>(int &target) {
    scanf("%" PRId32, &target);
    return *this;
  }
  fast_io &operator>>(unsigned int &target) {
    scanf("%" PRIu32, &target);
    return *this;
  }
  fast_io &operator>>(long long &target) {
    scanf("%" PRId64, &target);
    return *this;
  }
  fast_io &operator>>(unsigned long long &target) {
    scanf("%" PRIu64, &target);
    return *this;
  }
  template <typename T> fast_io &operator>>(vector<T> &target) {
    for (auto &value : target) {
      *this >> value;
    }
    return *this;
  }
  fast_io &operator<<(const int value) {
    printf("%" PRId32, value);
    return *this;
  }
  fast_io &operator<<(const unsigned int value) {
    printf("%" PRIu32, value);
    return *this;
  }
  fast_io &operator<<(const long long value) {
    printf("%" PRId64, value);
    return *this;
  }
  fast_io &operator<<(const unsigned long long value) {
    printf("%" PRIu64, value);
    return *this;
  }
  fast_io &operator<<(const string &value) {
    printf("%s", value.c_str());
    return *this;
  }
  template <typename T> fast_io &operator<<(const vector<T> &target) {
    for (auto &value : target) {
      *this << value << " ";
    }
    return *this;
  }
};
fast_io fio;

template <class ForwardIt, class UnaryPredicate>
ForwardIt first_true(ForwardIt first, ForwardIt last,
                     UnaryPredicate predicate) {
  ForwardIt it;
  typename std::iterator_traits<ForwardIt>::difference_type count, step;
  count = std::distance(first, last);
  while (count > 0) {
    it = first;
    step = count / 2;
    std::advance(it, step);
    if (!predicate(*it)) {
      first = ++it;
      count -= step + 1;
    } else
      count = step;
  }
  return first;
}

#define MOD 1000000007
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define sort_all(x) sort(all(x))
#define make_unique(x) (x).erase(unique(all(x)), (x).end())

vector<int> G[1 << 19];
vector<int> v, w;
vector<pii> Ls[1 << 19];
vector<int> ans;

vector<pii> step(vector<pii> &orig, int w, int v) {
  vector<pii> orig2;
  orig2.push_back({w, v});
  for (auto x : orig) {
    orig2.push_back({x.first + w, x.second + v});
  }
  vector<pii> ans;
  std::merge(orig.begin(), orig.end(), orig2.begin(), orig2.end(),
             std::back_inserter(ans));
  vector<pii> ans2;
  int max_v = 0;
  for (auto &x : ans) {
    if (x.second < max_v)
      continue;
    ans2.push_back(x);
    max_v = max(max_v, x.second);
  }
  return ans2;
}

int handle_ans(int i, int L, vector<pii> &first, vector<pii> &second) {
  int max_ans = 0;
  for (auto &x : first) {
    if (x.first <= L) {
      max_ans = max(max_ans, x.second);
    }
  }
  if (second.size() != 0) {
    for (auto &x : second) {
      if (x.first <= L)
        max_ans = max(max_ans, x.second);
      if (x.first > L)
        continue;
      int rem = L - x.first;
      if (rem <= 0)
        continue;

      auto it = first_true(first.begin(), first.end(),
                           [&](pii &a) { return a.first > rem; });
      if (it != first.begin()) {
        it--;
        max_ans = max(max_ans, x.second + it->second);
      }
    }
  }
  return max_ans;
}

void dfs(int i, int level, vector<pii> &first, vector<pii> &second) {
  if (i <= 8) {
    vector<pii> new_first = step(first, w[i], v[i]);
    for (auto x : Ls[i]) {
      ans[x.first] = handle_ans(i, x.second, new_first, second);
    }
    for (int u : G[i]) {
      dfs(u, level + 1, new_first, second);
    }
  } else {
    vector<pii> new_second = step(second, w[i], v[i]);
    for (auto x : Ls[i]) {
      ans[x.first] = handle_ans(i, x.second, first, new_second);
    }
    for (int u : G[i]) {
      dfs(u, level + 1, first, new_second);
    }
  }
}

int main() {

#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  fio >> n;

  for (int i = 1; i < n; i++) {
    int parent = (i + 1) / 2 - 1;
    G[parent].push_back(i);
  }

  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; i++) {
    fio >> v[i] >> w[i];
  }

  int q;
  fio >> q;
  ans.resize(q);
  for (int i = 0; i < q; i++) {
    int v, L;
    fio >> v >> L;
    v--;
    Ls[v].push_back({i, L});
  }

  vector<pii> a, b;
  dfs(0, 0, a, b);
  for (int x : ans)
    fio << x << "\\n";

  return 0;
}