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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \\
  fprintf(stderr, "Running on Line %d in Function %s\\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
#define N 300005
const int B = 11;
int v[N], w[N], n, tot;
int dep[N];
map<int, int> f[2048];
map<int, int> tmp;
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    v[i] = read(), w[i] = read();
  dep[1] = 1;
  for (int i = 2; i <= n; i++)
    dep[i] = dep[i / 2] + 1;
  for (int i = 1; i <= n; i++) {
    if (dep[i] <= B) {
      f[i] = f[i / 2];
      for (auto [x, y] : f[i / 2]) {
        if (x + w[i] <= 100000)
          f[i][x + w[i]] = max(f[i][x + w[i]], y + v[i]);
      }
      f[i][w[i]] = max(f[i][w[i]], v[i]);
      int cur = 0;
      vector<map<int, int>::iterator> v;
      for (auto j = f[i].begin(); j != f[i].end(); ++j) {
        cur = max(cur, (*j).sec);
        if ((*j).sec != cur)
          v.pb(j);
      }
      for (auto j : v)
        f[i].erase(j);
    }
  }
  // for(int i=1;i<=n;i++)
  // {
  // if(dep[i]>B) break;
  // cout<<i<<" :\\n";
  // for(auto [x,y]:f[i]) printf("%d %d\\n",x,y);
  // }
  int Q = read();
  while (Q--) {
    int u = read(), L = read();
    int r = dep[u];
    if (r < B) {
      auto x = f[u].upper_bound(L);
      if (x == f[u].begin())
        printf("0\\n");
      else
        x--, printf("%d\\n", (*x).sec);
    } else {
      // map<int,int> res; res.clear();
      int fa = u;
      while (dep[fa] > B)
        fa /= 2;
      vector<pii> t;
      int tmp = u;
      while (tmp != fa) {
        t.eb(v[tmp], w[tmp]);
        // map<int,int> qaq=res;
        // for(auto [x,y]:res)
        // {
        // if(x+w[tmp]<=100000) qaq[x+w[tmp]]=max(qaq[x+w[tmp]],y+v[tmp]);
        // }
        // qaq[w[tmp]]=max(qaq[w[tmp]],v[tmp]);
        // res=qaq;
        // int cur=0; vector<map<int,int>::iterator> v;
        // for(auto j=res.begin();j!=res.end();++j)
        // {
        // cur=max(cur,(*j).sec);
        // if((*j).sec!=cur) v.pb(j);
        // }
        // for(auto j:v) res.erase(j);
        tmp /= 2;
      }
      // for(auto [x,y]:f[fa]) printf("%d %d\\n",x,y); cout<<"\\n";
      // for(auto [x,y]:res) printf("%d %d\\n",x,y); cout<<"\\n";
      int ans = 0;
      auto x = f[fa].upper_bound(L);
      if (x == f[fa].begin())
        ans = 0;
      else
        x--, ans = (*x).sec;
      for (int i = 0; i < (1 << (int)t.size()); i++) {
        int x = 0, y = 0;
        for (int j = 0; j < (int)t.size(); j++) {
          if (i & (1 << j))
            x += t[j].sec, y += t[j].fir;
        }
        int ned = L - x;
        if (ned < 0)
          continue;
        auto rr = f[fa].upper_bound(ned);
        if (rr == f[fa].begin())
          ans = max(ans, y);
        else
          rr--, ans = max(ans, y + (*rr).sec);
      }
      printf("%d\\n", ans);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \\
  fprintf(stderr, "Running on Line %d in Function %s\\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
#define N 300005
const int B = 10;
int v[N], w[N], n, tot;
int dep[N];
map<int, int> f[1024];
map<int, int> tmp;
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    v[i] = read(), w[i] = read();
  dep[1] = 1;
  for (int i = 2; i <= n; i++)
    dep[i] = dep[i / 2] + 1;
  for (int i = 1; i <= n; i++) {
    if (dep[i] <= B) {
      f[i] = f[i / 2];
      for (auto [x, y] : f[i / 2]) {
        if (x + w[i] <= 100000)
          f[i][x + w[i]] = max(f[i][x + w[i]], y + v[i]);
      }
      f[i][w[i]] = max(f[i][w[i]], v[i]);
      int cur = 0;
      vector<map<int, int>::iterator> v;
      for (auto j = f[i].begin(); j != f[i].end(); ++j) {
        cur = max(cur, (*j).sec);
        if ((*j).sec != cur)
          v.pb(j);
      }
      for (auto j : v)
        f[i].erase(j);
    }
  }
  // for(int i=1;i<=n;i++)
  // {
  // if(dep[i]>B) break;
  // cout<<i<<" :\\n";
  // for(auto [x,y]:f[i]) printf("%d %d\\n",x,y);
  // }
  int Q = read();
  while (Q--) {
    int u = read(), L = read();
    int r = dep[u];
    if (r < B) {
      auto x = f[u].upper_bound(L);
      if (x == f[u].begin())
        printf("0\\n");
      else
        x--, printf("%d\\n", (*x).sec);
    } else {
      map<int, int> res;
      res.clear();
      int fa = u;
      while (dep[fa] > B)
        fa /= 2;
      int tmp = u;
      while (tmp != fa) {
        map<int, int> qaq = res;
        for (auto [x, y] : res) {
          if (x + w[tmp] <= 100000)
            qaq[x + w[tmp]] = max(qaq[x + w[tmp]], y + v[tmp]);
        }
        qaq[w[tmp]] = max(qaq[w[tmp]], v[tmp]);
        res = qaq;
        int cur = 0;
        vector<map<int, int>::iterator> v;
        for (auto j = res.begin(); j != res.end(); ++j) {
          cur = max(cur, (*j).sec);
          if ((*j).sec != cur)
            v.pb(j);
        }
        for (auto j : v)
          res.erase(j);
        tmp /= 2;
      }
      // for(auto [x,y]:f[fa]) printf("%d %d\\n",x,y); cout<<"\\n";
      // for(auto [x,y]:res) printf("%d %d\\n",x,y); cout<<"\\n";
      int ans = 0;
      auto x = f[fa].upper_bound(L);
      if (x == f[fa].begin())
        ans = 0;
      else
        x--, ans = (*x).sec;
      for (auto [x, y] : res) {
        int ned = L - x;
        if (ned < 0)
          break;
        auto rr = f[fa].upper_bound(ned);
        if (rr == f[fa].begin())
          ans = max(ans, y);
        else
          rr--, ans = max(ans, y + (*rr).sec);
      }
      printf("%d\\n", ans);
    }
  }
  return 0;
}