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

#include <bits/stdc++.h>
#define rep(i, a, ...)                                                         \\
  for (int i = (a) * (strlen(#__VA_ARGS__) != 0);                              \\
       i < (int)(strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)); ++i)
#define per(i, a, ...)                                                         \\
  for (int i = (strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)) - 1;                 \\
       i >= (int)(strlen(#__VA_ARGS__) ? (a) : 0); --i)
#define foreach(i, n) for (auto &i : (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define method(name, res, ...)                                                 \\
  function<res(__VA_ARGS__)> name = [&](__VA_ARGS__) -> res
using ll = long long;
// const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9 + 7;
const ll INFLL = (ll)1e18;
using namespace std;
template <class t> using vvector = vector<vector<t>>;
template <class t> using vvvector = vector<vector<vector<t>>>;
template <class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template <class t, class u> bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> bool chmin(t &a, u b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#ifdef DEBUG
#define debug(x)                                                               \\
  cout << "LINE " << __LINE__ << ": " << #x << " = " << x << endl;
#else
#define debug(x) (void)0
#endif

namespace templates {
ll modpow(ll x, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    b >>= 1;
  }
  return res;
}

ll modinv(ll x) { return modpow(x, MOD - 2); }

bool was_output = false;
template <class t> void output(t a) {
  if (was_output)
    cout << " ";
  cout << a;
  was_output = true;
}
void outendl() {
  was_output = false;
  cout << endl;
}
ll in() {
  ll res;
  scanf("%lld", &res);
  return res;
}

template <class t> istream &operator>>(istream &is, vector<t> &x) {
  for (auto &i : x)
    is >> i;
  return is;
}

template <class t, class u> istream &operator>>(istream &is, pair<t, u> &x) {
  is >> x.first >> x.second;
  return is;
}

template <class t> void in(t &x) { cin >> x; }

template <class t> t in() {
  t res;
  cin >> res;
  return res;
}

template <class t> void out(t x) { cout << x; }

template <class t>
vector<t> sorted(
    vector<t> line,
    function<bool(t, t)> comp = [](t a, t b) { return a < b; }) {
  sort(line.begin(), line.end(), comp);
  return line;
}

template <class t> vector<t> reversed(vector<t> line) {
  reverse(line.begin(), line.end());
  return line;
}
} // namespace templates

using namespace templates;

int main() {
  int n;
  scanf("%d", &n);
  vector<pii> line(n + 1);
  rep(i, 1, n + 1) { scanf("%d%d", &line[i].second, &line[i].first); }

  method(out2, vector<pii>, int from) {
    if (from == 0) {
      vector<pii> res;
      res.emplace_back(0, 0);
      return res;
    }
    vector<pii> left = out2(from >> 2);
    vector<pii> right(left.size());
    int ls = left.size();
    rep(i, ls) {
      right[i].first = left[i].first + line[from].first;
      right[i].second = left[i].second + line[from].second;
    }

    left.emplace_back(INF, INF);
    right.emplace_back(INF, INF);
    int cnt = 0;
    {
      int l = 0;
      int r = 0;
      int last_val = -1;
      rep(i, ls * 2) {
        if (left[l].first < right[r].first) {
          if (last_val < left[l].second) {
            ++cnt;
            last_val = left[l].second;
          }
          ++l;
        } else {
          if (last_val < right[r].second) {
            ++cnt;
            last_val = right[r].second;
          }
          ++r;
        }
      }
    }

    vector<pii> res(cnt);
    int l = 0;
    int r = 0;
    int last_val = -1;
    int itr = 0;
    rep(i, ls * 2) {
      if (left[l].first < right[r].first) {
        if (last_val < left[l].second) {
          res[itr++] = left[l];
          last_val = left[l].second;
        }
        ++l;
      } else {
        if (last_val < right[r].second) {
          res[itr++] = right[r];
          last_val = right[r].second;
        }
        ++r;
      }
    }
    return res;
  };

  method(func, ll, int from, ll use) {
    vector<pii> lu = out2(from);
    vector<pii> ru = out2(from >> 1);
    ll res = 0;
    int ls = lu.size();
    int rs = ru.size();
    int r = 0;
    per(i, ls) {
      while (r + 1 < rs and lu[i].first + ru[r + 1].first <= use)
        ++r;
      if (lu[i].first + ru[r].first <= use) {
        chmax(res, lu[i].second + ru[r].second);
      }
    }
    return res;
  };

  int q;
  scanf("%d", &q);
  rep(i, q) {
    int v;
    int l;
    scanf("%d%d", &v, &l);
    cout << func(v, l) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, ...)                                                         \\
  for (int i = (a) * (strlen(#__VA_ARGS__) != 0);                              \\
       i < (int)(strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)); ++i)
#define per(i, a, ...)                                                         \\
  for (int i = (strlen(#__VA_ARGS__) ? __VA_ARGS__ : (a)) - 1;                 \\
       i >= (int)(strlen(#__VA_ARGS__) ? (a) : 0); --i)
#define foreach(i, n) for (auto &i : (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define method(name, res, ...)                                                 \\
  function<res(__VA_ARGS__)> name = [&](__VA_ARGS__) -> res
using ll = long long;
// const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9 + 7;
const ll INFLL = (ll)1e18;
using namespace std;
template <class t> using vvector = vector<vector<t>>;
template <class t> using vvvector = vector<vector<vector<t>>>;
template <class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template <class t, class u> bool chmax(t &a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u> bool chmin(t &a, u b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#ifdef DEBUG
#define debug(x)                                                               \\
  cout << "LINE " << __LINE__ << ": " << #x << " = " << x << endl;
#else
#define debug(x) (void)0
#endif

namespace templates {
ll modpow(ll x, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    b >>= 1;
  }
  return res;
}

ll modinv(ll x) { return modpow(x, MOD - 2); }

bool was_output = false;
template <class t> void output(t a) {
  if (was_output)
    cout << " ";
  cout << a;
  was_output = true;
}
void outendl() {
  was_output = false;
  cout << endl;
}
ll in() {
  ll res;
  scanf("%lld", &res);
  return res;
}

template <class t> istream &operator>>(istream &is, vector<t> &x) {
  for (auto &i : x)
    is >> i;
  return is;
}

template <class t, class u> istream &operator>>(istream &is, pair<t, u> &x) {
  is >> x.first >> x.second;
  return is;
}

template <class t> void in(t &x) { cin >> x; }

template <class t> t in() {
  t res;
  cin >> res;
  return res;
}

template <class t> void out(t x) { cout << x; }

template <class t>
vector<t> sorted(
    vector<t> line,
    function<bool(t, t)> comp = [](t a, t b) { return a < b; }) {
  sort(line.begin(), line.end(), comp);
  return line;
}

template <class t> vector<t> reversed(vector<t> line) {
  reverse(line.begin(), line.end());
  return line;
}
} // namespace templates

using namespace templates;

int main() {
  int n = in();
  vector<pll> line(n + 1);
  rep(i, 1, n + 1) {
    line[i].second = in();
    line[i].first = in();
  }

  method(got, vector<pll>, int from) {
    vector<pll> res;
    while (from) {
      res.emplace_back(line[from]);
      from /= 2;
    }
    return res;
  };

  method(out2, vector<pll>, int from) {
    if (from == 0) {
      vector<pll> res;
      res.emplace_back(0, 0);
      return res;
    }
    vector<pll> left = out2(from / 4);
    vector<pll> right(left.size());
    int ls = left.size();
    rep(i, ls) {
      right[i].first = left[i].first + line[from].first;
      right[i].second = left[i].second + line[from].second;
    }

    left.emplace_back(INF, INF);
    right.emplace_back(INF, INF);

    vector<pll> res;
    int l = 0;
    int r = 0;
    int last_val = -1;
    rep(i, ls * 2) {
      if (left[l].first < right[r].first) {
        if (last_val < left[l].second) {
          res.emplace_back(left[l]);
          last_val = left[l].second;
        }
        ++l;
      } else {
        if (last_val < right[r].second) {
          res.emplace_back(right[r]);
          last_val = right[r].second;
        }
        ++r;
      }
    }
    return res;
  };

  method(out, vector<pll>, vector<pll> line) {
    vector<pll> res;
    int n = line.size();
    rep(i, bit(n)) {
      pll sum(0, 0);
      rep(j, n) {
        if (!(bit(j) & i))
          continue;
        sum.first += line[j].first;
        sum.second += line[j].second;
      }
      res.emplace_back(sum);
    }
    sort(all(res));
    vector<pll> res2;
    res2.emplace_back(0, 0);
    int last_val = 0;
    foreach (i, res) {
      if (last_val >= i.second)
        continue;
      res2.emplace_back(i);
      last_val = i.second;
    }
    return res2;
  };

  method(func, ll, int from, ll use) {
    vector<pll> lu = out2(from);
    vector<pll> ru = out2(from / 2);
    reverse(all(lu));
    reverse(all(ru));
    ll res = 0;
    int r = ru.size() - 1;
    foreach (i, lu) {
      while (r > 0 and i.first + ru[r - 1].first <= use)
        --r;
      if (i.first + ru[r].first <= use) {
        chmax(res, i.second + ru[r].second);
      }
    }
    return res;
  };

  int q = in();
  rep(i, q) {
    int v = in();
    int l = in();
    cout << func(v, l) << endl;
  }
  return 0;
}