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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef array<int, 2> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  rep(i, 0, n) cin >> v[i] >> w[i];
  vector<P> zen[1 << 10];
  rep(i, 0, 1 << 10) {
    if (i > n)
      break;
    int x = i;
    vector<P> a;
    while (x) {
      a.push_back({v[x - 1], w[x - 1]});
      x >>= 1;
    }
    int p = a.size();
    rep(mask, 0, 1 << p) {
      P add = {};
      rep(i, 0, p) if (mask >> i & 1) {
        add[0] += a[i][1];
        add[1] -= a[i][0];
      }
      zen[i].push_back(add);
    }
    sort(ALL(zen[i]));
    rep(j, 0, zen[i].size() - 1) chmin(zen[i][j + 1][1], zen[i][j][1]);
  }
  int q;
  cin >> q;
  rep(_, 0, q) {
    int x, y;
    cin >> x >> y;
    vector<P> a;
    while (x >= (1 << 10)) {
      a.push_back({v[x - 1], w[x - 1]});
      x >>= 1;
    }
    int m = a.size();
    vector<P> kou;
    rep(mask, 0, 1 << m) {
      P add = {};
      rep(i, 0, m) if (mask >> i & 1) {
        add[0] += a[i][1];
        add[1] += a[i][0];
      }
      kou.push_back(add);
    }
    int res = 0;
    for (auto [w, v] : kou) {
      auto it = lower_bound(ALL(zen[x]), P{y - w + 1, -inf});
      if (it == zen[x].begin())
        continue;
      it--;
      chmax(res, v - (*it)[1]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef array<int, 2> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  rep(i, 0, n) cin >> v[i] >> w[i];
  vector<P> zen[1 << 10];
  rep(i, 0, 1 << 10) {
    if (i > n)
      break;
    int x = i;
    vector<P> a;
    while (x) {
      a.push_back({v[x - 1], w[x - 1]});
      x >>= 1;
    }
    int p = a.size();
    rep(mask, 0, 1 << p) {
      P add = {};
      rep(i, 0, p) if (mask >> i & 1) {
        add[0] += a[i][1];
        add[1] += a[i][0];
      }
      zen[i].push_back(add);
    }
  }
  int q;
  cin >> q;
  rep(_, 0, q) {
    int x, y;
    cin >> x >> y;
    vector<P> a;
    while (x >= (1 << 10)) {
      a.push_back({v[x - 1], w[x - 1]});
      x >>= 1;
    }
    int m = a.size();
    vector<P> kou;
    rep(mask, 0, 1 << m) {
      P add = {};
      rep(i, 0, m) if (mask >> i & 1) {
        add[0] += a[i][1];
        add[1] -= a[i][0];
      }
      kou.push_back(add);
    }
    sort(ALL(kou));
    rep(i, 0, kou.size() - 1) chmin(kou[i + 1][1], kou[i][1]);
    int res = 0;
    for (auto [w, v] : zen[x]) {
      auto it = lower_bound(ALL(kou), P{y - w + 1, -inf});
      if (it == kou.begin())
        continue;
      it--;
      chmax(res, v - (*it)[1]);
    }
    cout << res << endl;
  }
  return 0;
}