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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 301000;
int n, q, x, l;
int v[N], w[N];
PII ml[5010], mr[5010];

int main() {
  scanf("%d", &n);
  rep(i, 1, n + 1) scanf("%d%d", w + i, v + i);
  // rep(i,1,n+1) w[i]=rand()%10+1,v[i]=rand()%10+1;
  scanf("%d", &q);
  rep(i, 0, q) {
    scanf("%d%d", &x, &l);
    vector<PII> fl, fr;
    int ty = 0;
    while (x) {
      if (ty == 0)
        fl.pb({v[x], w[x]});
      else
        fr.pb({v[x], w[x]});
      x /= 2;
      ty ^= 1;
    }
    int sl = 1, sr = 1;

    for (auto x : fl) {
      int s = sl;
      rep(j, 0, s) ml[sl++] = mp(ml[j].fi + x.fi, ml[j].se + x.se);
      inplace_merge(ml, ml + s, ml + sl);
    }
    // printf("%d\\n",sl);
    for (auto x : fr) {
      int s = sr;
      rep(j, 0, s) mr[sr++] = mp(mr[j].fi + x.fi, mr[j].se + x.se);
      inplace_merge(mr, mr + s, mr + sr);
    }
    for (int i = 1; i < sr; i++)
      mr[i].se = max(mr[i - 1].se, mr[i].se);
    int sz = sr - 1;
    int ans = 0;
    rep(i, 0, sl) {
      auto x = ml[i];
      while (sz >= 0 && x.fi + mr[sz].fi > l)
        --sz;
      if (sz >= 0)
        ans = max(ans, x.se + mr[sz].se);
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 301000;
int n, q, x, l;
int v[N], w[N];

int main() {
  scanf("%d", &n);
  rep(i, 1, n + 1) scanf("%d%d", w + i, v + i);
  scanf("%d", &q);
  rep(i, 0, q) {
    scanf("%d%d", &x, &l);
    vector<PII> fl, fr, ml, mr;
    int ty = 0;
    while (x) {
      if (ty == 0)
        fl.pb({v[x], w[x]});
      else
        fr.pb({v[x], w[x]});
      x /= 2;
    }
    ml.pb({0, 0});
    mr.pb({0, 0});
    for (auto x : fl) {
      int s = SZ(ml);
      rep(j, 0, s) ml.pb({ml[j].fi + x.fi, ml[j].se + x.se});
      inplace_merge(ml.begin(), ml.begin() + s, ml.end());
    }
    for (auto x : fr) {
      int s = SZ(mr);
      rep(j, 0, s) mr.pb({mr[j].fi + x.fi, mr[j].se + x.se});
      inplace_merge(mr.begin(), mr.begin() + s, mr.end());
    }
    /*for (auto x:ml) {
            printf("ml %d %d\\n",x.fi,x.se);
    }
    for (auto x:mr) {
            printf("mr %d %d\\n",x.fi,x.se);
    }*/
    for (int i = 1; i < SZ(mr); i++)
      mr[i].se = max(mr[i - 1].se, mr[i].se);
    int sz = SZ(mr) - 1;
    int ans = 0;
    for (auto x : ml) {
      while (sz >= 0 && x.fi + mr[sz].fi > l)
        --sz;
      if (sz >= 0)
        ans = max(ans, x.se + mr[sz].se);
    }
    printf("%d\\n", ans);
  }
}