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

/*
    author:  Maksim1744
    created: 13.06.2020 15:29:39
*/

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

#define mp make_pair
#define pb push_back
#define eb emplace_back

#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template <typename T> vector<T> &operator--(vector<T> &v) {
  for (auto &i : v)
    --i;
  return v;
}
template <typename T> vector<T> &operator++(vector<T> &v) {
  for (auto &i : v)
    ++i;
  return v;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &i : v)
    os << i << ' ';
  return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
  return mp(a.first - b.first, a.second - b.second);
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
  return mp(a.first + b.first, a.second + b.second);
}
template <typename T, typename U> void umin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void umax(T &a, U b) {
  if (a < b)
    a = b;
}

#ifdef HOME
#define TAG_LENGTH 25
#define LR_LEFT left
#define LR_RIGHT right
#define LR_VALUE value
#define LR_SECOND_VALUE mn
#include "C:/C++ libs/print.cpp"
const int H = 2;
#else
#define showl 42;
#define shows 42;
#define show(...) 42;
#define showm(...) 42;
#define showt(...) 42;
#define printTree(...) 42;
#define printGraph(...) 42;
#define printLRTree(...) 42;
#define printMatrix(...) 42;
#define printWGraph(...) 42;
#define debug if (false)
const int H = 11;
#endif

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  vector<int> h(n + 1);
  h[1] = 1;
  for (int i = 2; i <= n; ++i)
    h[i] = h[i / 2] + 1;
  vector<vector<pair<int, int>>> pos(n + 1);
  pos[0].eb(0, 0);
  for (int i = 1; i <= n && h[i] <= H; ++i) {
    pos[i] = pos[i / 2];
    for (auto wwvv : pos[i / 2]) {
      int ww, vv;
      tie(ww, vv) = wwvv;
      pos[i].eb(ww + w[i], vv + v[i]);
    }
    sort(pos[i].begin(), pos[i].end());
    for (int j = 1; j < pos[i].size(); ++j)
      pos[i][j].second = max(pos[i][j].second, pos[i][j - 1].second);
    pos[i].erase(unique(pos[i].begin(), pos[i].end()), pos[i].end());
  }
  // for (int i = 1; i <= n; ++i) {
  //     pos[i] = vector<pair<int, int>>(prepos[i].begin(), prepos[i].end());
  //     for (int j = 1; j < pos[i].size(); ++j)
  //         pos[i][j].second = max(pos[i][j].second, pos[i][j - 1].second);
  // }

  // if (n == 15) {
  //     show(pos[1]);
  //     show(pos[2]);
  //     show(pos[4]);
  //     show(pos[9]);
  // }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int u, L;
    cin >> u >> L;
    if (h[u] <= H) {
      int ind = lowb(pos[u], make_pair(L + 1, -1));
      if (ind == pos[u].size() || pos[u][ind].first > L)
        --ind;
      cout << pos[u][ind].second << '\\n';
      continue;
    }
    vector<pair<int, int>> vars;
    int x = u;
    while (h[x] > H) {
      vars.eb(w[x], v[x]);
      x /= 2;
    }
    vector<pair<int, int>> vv;
    vv.eb(0, 0);
    for (auto wv1 : vars) {
      int wi, vi;
      tie(wi, vi) = wv1;
      auto mm1 = vv;
      for (auto wwvv : vv) {
        int ww, vv;
        tie(ww, vv) = wwvv;
        mm1.eb(ww + wi, vv + vi);
      }
      swap(vv, mm1);
    }
    sort(vv.begin(), vv.end());
    for (int j = 1; j < vv.size(); ++j)
      vv[j].second = max(vv[j].second, vv[j - 1].second);
    vv.erase(unique(vv.begin(), vv.end()), vv.end());
    int ans = 0;
    int ind = pos[x].size() - 1;
    for (auto wv : vv) {
      int w, v;
      tie(w, v) = wv;
      if (w > L)
        break;
      int ind = lowb(pos[x], make_pair(L - w + 1, -1));
      if (ind == pos[x].size() || pos[x][ind].first > L - w)
        --ind;
      ans = max(ans, pos[x][ind].second + v);
    }
    cout << ans << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

/*
    author:  Maksim1744
    created: 13.06.2020 15:29:39
*/

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

#define mp make_pair
#define pb push_back
#define eb emplace_back

#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template <typename T> vector<T> &operator--(vector<T> &v) {
  for (auto &i : v)
    --i;
  return v;
}
template <typename T> vector<T> &operator++(vector<T> &v) {
  for (auto &i : v)
    ++i;
  return v;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &i : v)
    os << i << ' ';
  return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
  return mp(a.first - b.first, a.second - b.second);
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
  return mp(a.first + b.first, a.second + b.second);
}
template <typename T, typename U> void umin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void umax(T &a, U b) {
  if (a < b)
    a = b;
}

#ifdef HOME
#define TAG_LENGTH 25
#define LR_LEFT left
#define LR_RIGHT right
#define LR_VALUE value
#define LR_SECOND_VALUE mn
#include "C:/C++ libs/print.cpp"
const int H = 2;
#else
#define showl 42;
#define shows 42;
#define show(...) 42;
#define showm(...) 42;
#define showt(...) 42;
#define printTree(...) 42;
#define printGraph(...) 42;
#define printLRTree(...) 42;
#define printMatrix(...) 42;
#define printWGraph(...) 42;
#define debug if (false)
const int H = 11;
#endif

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  vector<int> h(n + 1);
  h[1] = 1;
  for (int i = 2; i <= n; ++i)
    h[i] = h[i / 2] + 1;
  vector<vector<pair<int, int>>> pos(n + 1);
  pos[0].eb(0, 0);
  for (int i = 1; i <= n && h[i] <= H; ++i) {
    pos[i] = pos[i / 2];
    for (auto wwvv : pos[i / 2]) {
      int ww, vv;
      tie(ww, vv) = wwvv;
      pos[i].eb(ww + w[i], vv + v[i]);
    }
    sort(pos[i].begin(), pos[i].end());
    for (int j = 1; j < pos[i].size(); ++j)
      pos[i][j].second = max(pos[i][j].second, pos[i][j - 1].second);
    pos[i].erase(unique(pos[i].begin(), pos[i].end()), pos[i].end());
  }
  // for (int i = 1; i <= n; ++i) {
  //     pos[i] = vector<pair<int, int>>(prepos[i].begin(), prepos[i].end());
  //     for (int j = 1; j < pos[i].size(); ++j)
  //         pos[i][j].second = max(pos[i][j].second, pos[i][j - 1].second);
  // }

  // if (n == 15) {
  //     show(pos[1]);
  //     show(pos[2]);
  //     show(pos[4]);
  //     show(pos[9]);
  // }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int u, L;
    cin >> u >> L;
    if (h[u] <= H) {
      int ind = lowb(pos[u], make_pair(L + 1, -1));
      if (ind == pos[u].size() || pos[u][ind].first > L)
        --ind;
      cout << pos[u][ind].second << '\\n';
      continue;
    }
    vector<pair<int, int>> vars;
    int x = u;
    while (h[x] > H) {
      vars.eb(w[x], v[x]);
      x /= 2;
    }
    map<int, int> mm;
    mm.emplace(0, 0);
    for (auto wv1 : vars) {
      int wi, vi;
      tie(wi, vi) = wv1;
      map<int, int> mm1 = mm;
      for (auto wwvv : mm) {
        int ww, vv;
        tie(ww, vv) = wwvv;
        mm1[ww + wi] = max(mm1[ww + wi], vv + vi);
      }
      swap(mm, mm1);
    }
    vector<pair<int, int>> vv(mm.begin(), mm.end());
    for (int j = 1; j < vv.size(); ++j)
      vv[j].second = max(vv[j].second, vv[j - 1].second);
    int ans = 0;
    int ind = pos[x].size() - 1;
    for (auto wv : vv) {
      int w, v;
      tie(w, v) = wv;
      if (w > L)
        break;
      int ind = lowb(pos[x], make_pair(L - w + 1, -1));
      if (ind == pos[x].size() || pos[x][ind].first > L - w)
        --ind;
      ans = max(ans, pos[x][ind].second + v);
    }
    cout << ans << '\\n';
  }

  return 0;
}