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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define ip pair<ll, ll>
#define ff first
#define ss second
#define MAX 100005
const ll MOD = 1000000007;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;
  ll v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  ll d[n + 1];
  d[1] = 1;
  for (int i = 2; i <= n; i++)
    d[i] = 1 + d[i / 2];
  ll m = 10;

  ll st = (1 << (m - 1));
  vector<vector<ip>> pre;
  int j = st;
  while (j <= n && d[j] == m) {
    vector<ip> r, final;
    r.pb({0, 0});
    ll u = j;
    while (u != 0) {
      ll k = r.size();
      for (int i = 0; i < k; i++) {
        if (r[i].ff + w[u] < MAX)
          r.pb({r[i].ff + w[u], r[i].ss + v[u]});
      }
      u = u / 2;
    }

    sort(r.begin(), r.end());
    ll k = r.size();
    ll prev = 0;
    ll hi = r[0].ss;
    for (int i = 1; i < k; i++) {
      if (r[i].ff == r[prev].ff)
        hi = max(hi, r[i].ss);
      else {
        if (final.size() == 0 || final.back().ss < hi)
          final.pb({r[prev].ff, hi});
        prev = i;
        hi = r[prev].ss;
      }
    }

    if (final.size() == 0 || final.back().ss < hi)
      final.pb({r[prev].ff, hi});

    pre.pb(final);
    ++j;
  }

  // for(vector<ip> x: pre){
  // 	cout<<"New:\\n";
  // 	for(ip z: x)
  // 		cout<<z.ff<<" "<<z.ss<<endl;
  // 	cout<<endl;
  // }

  ll q, x, L;
  cin >> q;
  while (q--) {
    cin >> x >> L;

    if (d[x] < m) {
      vector<ip> r, final;
      r.pb({0, 0});
      ll u = x;
      while (u != 0) {
        ll k = r.size();
        for (int i = 0; i < k; i++)
          r.pb({r[i].ff + w[u], r[i].ss + v[u]});
        u = u / 2;
      }

      // sort(r.begin(),r.end());
      // ll k = r.size();
      // ll prev = 0;
      // ll hi = r[0].ss;
      // for(int i=1;i<k;i++){
      // 	if(r[i].ff == r[prev].ff)
      // 		hi = max(hi,r[i].ss);
      // 	else{
      // 		if(final.size() == 0 || final.back().ss < hi)
      // 			final.pb({r[prev].ff,hi});
      // 		prev = i;
      // 		hi = r[prev].ss;
      // 	}
      // }

      // if(final.size() == 0 || final.back().ss < hi)
      // 	final.pb({r[prev].ff,hi});

      ll best = 0;
      for (int i = 0; i < r.size(); i++) {
        if (r[i].ff <= L)
          best = max(best, r[i].ss);
      }

      cout << best << endl;
    } else {
      vector<ip> r, final;
      r.pb({0, 0});
      ll u = x;
      while (d[u] != m) {
        ll k = r.size();
        for (int i = 0; i < k; i++) {
          if (r[i].ff + w[u] < MAX)
            r.pb({r[i].ff + w[u], r[i].ss + v[u]});
        }

        u = u / 2;
      }

      ll idx = u - st;
      // cout<< idx<<endl;

      sort(r.begin(), r.end());
      ll k = r.size();
      ll prev = 0;
      ll hi = r[0].ss;
      for (int i = 1; i < k; i++) {
        if (r[i].ff == r[prev].ff)
          hi = max(hi, r[i].ss);
        else {
          if (final.size() == 0 || final.back().ss < hi)
            final.pb({r[prev].ff, hi});
          prev = i;
          hi = r[prev].ss;
        }
      }

      if (final.size() == 0 || final.back().ss < hi)
        final.pb({r[prev].ff, hi});

      ll best = 0;
      for (int i = 0; i < final.size(); i++) {
        if (final[i].ff > L)
          break;

        // cout<<final[i].ff<<" "<<final[i].ss<<" ";

        k = pre[idx].size();
        ll lo = 0;
        ll hi = k - 1;
        while (lo < hi) {
          ll mid = (lo + hi + 1) / 2;
          if (pre[idx][mid].ff + final[i].ff > L)
            hi = mid - 1;
          else
            lo = mid;
        }

        // cout<< "lo= " << lo << " ans=" << final[i].ss + pre[idx][lo].ss
        // <<endl;

        best = max(best, final[i].ss + pre[idx][lo].ss);
      }
      cout << best << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define ip pair<ll, ll>
#define ff first
#define ss second
#define MAX 100005
const ll MOD = 1000000007;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;
  ll v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  ll d[n + 1];
  d[1] = 1;
  for (int i = 2; i <= n; i++)
    d[i] = 1 + d[i / 2];
  ll m = (d[n] + 1) / 2;

  ll st = (1 << (m - 1));
  vector<vector<ip>> pre;
  int j = st;
  while (d[j] == m) {
    vector<ip> r, final;
    r.pb({0, 0});
    ll u = j;
    while (u != 0) {
      ll k = r.size();
      for (int i = 0; i < k; i++) {
        if (r[i].ff + w[u] < MAX)
          r.pb({r[i].ff + w[u], r[i].ss + v[u]});
      }
      u = u / 2;
    }

    sort(r.begin(), r.end());
    ll k = r.size();
    ll prev = 0;
    ll hi = r[0].ss;
    for (int i = 1; i < k; i++) {
      if (r[i].ff == r[prev].ff)
        hi = max(hi, r[i].ss);
      else {
        if (final.size() == 0 || final.back().ss < hi)
          final.pb({r[prev].ff, hi});
        prev = i;
        hi = r[prev].ss;
      }
    }

    if (final.size() == 0 || final.back().ss < hi)
      final.pb({r[prev].ff, hi});

    pre.pb(final);
    ++j;
  }

  // for(vector<ip> x: pre){
  // 	cout<<"New:\\n";
  // 	for(ip z: x)
  // 		cout<<z.ff<<" "<<z.ss<<endl;
  // 	cout<<endl;
  // }

  ll q, x, L;
  cin >> q;
  while (q--) {
    cin >> x >> L;

    if (d[x] < m) {
      vector<ip> r, final;
      r.pb({0, 0});
      ll u = x;
      while (u != 0) {
        ll k = r.size();
        for (int i = 0; i < k; i++)
          r.pb({r[i].ff + w[u], r[i].ss + v[u]});
        u = u / 2;
      }

      // sort(r.begin(),r.end());
      // ll k = r.size();
      // ll prev = 0;
      // ll hi = r[0].ss;
      // for(int i=1;i<k;i++){
      // 	if(r[i].ff == r[prev].ff)
      // 		hi = max(hi,r[i].ss);
      // 	else{
      // 		if(final.size() == 0 || final.back().ss < hi)
      // 			final.pb({r[prev].ff,hi});
      // 		prev = i;
      // 		hi = r[prev].ss;
      // 	}
      // }

      // if(final.size() == 0 || final.back().ss < hi)
      // 	final.pb({r[prev].ff,hi});

      ll best = 0;
      for (int i = 0; i < r.size(); i++) {
        if (r[i].ff <= L)
          best = max(best, r[i].ss);
      }

      cout << best << endl;
    } else {
      vector<ip> r, final;
      r.pb({0, 0});
      ll u = x;
      while (d[u] != m) {
        ll k = r.size();
        for (int i = 0; i < k; i++) {
          if (r[i].ff + w[u] < MAX)
            r.pb({r[i].ff + w[u], r[i].ss + v[u]});
        }

        u = u / 2;
      }

      ll idx = u - st;
      // cout<< idx<<endl;

      sort(r.begin(), r.end());
      ll k = r.size();
      ll prev = 0;
      ll hi = r[0].ss;
      for (int i = 1; i < k; i++) {
        if (r[i].ff == r[prev].ff)
          hi = max(hi, r[i].ss);
        else {
          if (final.size() == 0 || final.back().ss < hi)
            final.pb({r[prev].ff, hi});
          prev = i;
          hi = r[prev].ss;
        }
      }

      if (final.size() == 0 || final.back().ss < hi)
        final.pb({r[prev].ff, hi});

      ll best = 0;
      for (int i = 0; i < final.size(); i++) {
        if (final[i].ff > L)
          break;

        // cout<<final[i].ff<<" "<<final[i].ss<<" ";

        k = pre[idx].size();
        ll lo = 0;
        ll hi = k - 1;
        while (lo < hi) {
          ll mid = (lo + hi + 1) / 2;
          if (pre[idx][mid].ff + final[i].ff > L)
            hi = mid - 1;
          else
            lo = mid;
        }

        // cout<< "lo= " << lo << " ans=" << final[i].ss + pre[idx][lo].ss
        // <<endl;

        best = max(best, final[i].ss + pre[idx][lo].ss);
      }
      cout << best << endl;
    }
  }
}