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

#define _USE_MATH_DEFINES
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
// #include <bits/stdc++.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <emmintrin.h>
#include <mmintrin.h>
#include <nmmintrin.h>

using namespace std;

#define dprint(Exp, ...)                                                       \\
  if (Exp) {                                                                   \\
    fprintf(stderr, __VA_ARGS__);                                              \\
  }
#define printe(...) fprintf(stderr, __VA_ARGS__);
// #define printe(...) void();
#define printE(...) fprintf(stderr, __VA_ARGS__);
// #define printE(...) void();

#define PrtExp(_Exp) cerr << #_Exp << " = " << (_Exp)
#define PrtExpN(_Exp) cerr << #_Exp << " = " << (_Exp) << "\\n"

#define SINT(n) scanf("%d", &n)
#define SINT2(n, m) scanf("%d %d", &n, &m)
#define SINT3(n, m, o) scanf("%d %d %d", &n, &m, &o)
#define SINT4(n, m, o, P) scanf("%d %d %d %d", &n, &m, &o, &P)
#define SINT5(n, m, o, P, q) scanf("%d %d %d %d %d", &n, &m, &o, &P, &q)
#define SLL(n) scanf("%lld", &n)
#define SLL2(n, m) scanf("%lld %lld", &n, &m)
#define SLL3(n, m, o) scanf("%lld %lld %lld", &n, &m, &o)
#define SST(s) scanf("%s", s)
#define SCH(c) scanf("%c", &c)

#define GC() getchar()

#define PINT(n) printf("%d", (int)(n))
#define PINT2(n, m) printf("%d %d", (int)(n), (int)(m))
#define PINT3(n, m, l) printf("%d %d %d", (int)(n), (int)(m), (int)(l))
#define PLL(n) printf("%lld", (long long)(n))
#define PST(s) printf("%s", (s))
#define PCH(s) printf("%c", (s))

#define PINTN(n) printf("%d\\n", (int)(n))
#define PINT2N(n, m) printf("%d %d\\n", (int)(n), (int)(m))
#define PINT3N(n, m, l) printf("%d %d %d\\n", (int)(n), (int)(m), (int)(l))
#define PLLN(n) printf("%lld\\n", (long long)(n))
#define PSTN(s) printf("%s\\n", (s))
#define PCHN(s) printf("%c\\n", (s))

#define PSP() printf(" ")
#define PN() printf("\\n")

#define PC(c) putchar(c)
#define CSP (' ')
#define SN ("\\n")

#define rep(i, a) for (int i = 0; i < a; i++)
#define reP(i, a) for (int i = 0; i <= a; i++)
#define Rep(i, a) for (int i = a - 1; i >= 0; i--)
#define ReP(i, a) for (int i = a; i >= 0; i--)

#define rEp(i, a) for (i = 0; i < a; i++)
#define rEP(i, a) for (i = 0; i <= a; i++)
#define REp(i, a) for (i = a - 1; i >= 0; i--)
#define REP(i, a) for (i = a; i >= 0; i--)

#define repft(i, a, b) for (int i = a; i < b; i++)
#define repfT(i, a, b) for (int i = a; i <= b; i++)
#define Repft(i, a, b) for (int i = a - 1; i >= b; i--)
#define RepfT(i, a, b) for (int i = a; i >= b; i--)

#define foreach(a, it) for (auto it = a.begin(); it != a.end(); ++it)

#define FILL(a, v) fill(begin(a), end(a), v)
#define FILL0(a) memset(a, 0, sizeof(a))
#define FILL1(a) memset(a, -1, sizeof(a))

typedef long long ll;

typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

const int INF = 1'010'000'000;         // 0x3C33'6080
const ll INFLL = 0x1f1f1f1f1f1f1f1fLL; // 2,242,545,357,980,376,863

template <class A, class B>
inline ostream &operator<<(ostream &st, const pair<A, B> &P) {
  return st << "(" << P.first << "," << P.second << ")";
};
template <class A, class B>
inline pair<A, B> operator+(const pair<A, B> &P, const pair<A, B> &Q) {
  return pair<A, B>(P.first + Q.first, P.second + Q.second);
};
template <class A, class B>
inline pair<A, B> operator-(const pair<A, B> &P, const pair<A, B> &Q) {
  return pair<A, B>(P.first - Q.first, P.second - Q.second);
};

inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

const int ALOG = 11;
const int A = (1 << ALOG);
int n;
int v[1 << 19];
int w[1 << 19];

vector<Pi> x[100005]; // 重さ(以下), 価値

void dfs(int i, int remDep, const vector<Pi> &a) {
  if (i > n)
    return;

  x[i].reserve(a.size() * 2);
  int W = w[i];
  int V = v[i];

  for (const auto p : a) {
    x[i].emplace_back(p);
    x[i].emplace_back(p.first + W, p.second + V);
  }
  if (remDep > 0 && i * 2 <= n) {
    dfs(i * 2 + 0, remDep - 1, x[i]);
    dfs(i * 2 + 1, remDep - 1, x[i]);
  }

  sort(x[i].begin(), x[i].end());
  int ma = x[i][0].second;
  for (auto &p : x[i]) {
    p.second = ma = max(ma, p.second);
  }
}

int main() {
  cin >> n;

  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  int q;
  cin >> q;
  vector<int> qv(q);
  vector<int> ql(q);
  rep(i, q) { cin >> qv[i] >> ql[i]; }

  {
    x[0].emplace_back(0, 0);
    dfs(1, ALOG, x[0]);
  }

  vector<Pi> a, b;
  a.reserve(1 << 12);
  b.reserve(1 << 12);

  /*
  repfT(i, 1, A * 2) {
      cout << "# " << i << endl;
      for (auto p : x[i]) cout << p << endl;
  }
  */

  rep(i, q) {
    int vv = qv[i];
    int L = ql[i];
    a.clear();
    a.emplace_back(0, 0);

    int j = vv;
    for (; j >= A * 2; j >>= 1) {
      b.clear();
      int W = w[j];
      int V = v[j];
      for (const auto p : a) {
        b.emplace_back(p);
        b.emplace_back(p.first + W, p.second + V);
      }
      a.swap(b);
    }
    int ret = 0;
    for (const auto p : a) {
      if (p.first > L)
        continue;

      const Pi F{L - p.first, INF};
      auto it = upper_bound(x[j].begin(), x[j].end(), F);

      /*
      cerr << p << F;
      if (it == x[j].end()) {
          cerr << "x" << endl;
      } else {
          cerr << *it << endl;
      }
      /*
      if (it == x[j].begin()) {
          cerr << "x" << endl;
      } else {
          auto itt = it; --itt;
          cerr << *itt << endl;
      }
      */

      if (it == x[j].begin())
        continue;
      --it;
      ret = max(ret, p.second + it->second);
    }
    cout << ret << endl;
  }

  return 0;
}

/*

2**18 = 262,144
2** 9 = 512


512 * 512
100000 * 512 * 9


a + b = log(n)

2^a + a * 2^b


a = 10 b = 8    256 000 000
a = 12 b = 6     76 800 000



*/


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
// #include <bits/stdc++.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <emmintrin.h>
#include <mmintrin.h>
#include <nmmintrin.h>

using namespace std;

#define dprint(Exp, ...)                                                       \\
  if (Exp) {                                                                   \\
    fprintf(stderr, __VA_ARGS__);                                              \\
  }
#define printe(...) fprintf(stderr, __VA_ARGS__);
// #define printe(...) void();
#define printE(...) fprintf(stderr, __VA_ARGS__);
// #define printE(...) void();

#define PrtExp(_Exp) cerr << #_Exp << " = " << (_Exp)
#define PrtExpN(_Exp) cerr << #_Exp << " = " << (_Exp) << "\\n"

#define SINT(n) scanf("%d", &n)
#define SINT2(n, m) scanf("%d %d", &n, &m)
#define SINT3(n, m, o) scanf("%d %d %d", &n, &m, &o)
#define SINT4(n, m, o, P) scanf("%d %d %d %d", &n, &m, &o, &P)
#define SINT5(n, m, o, P, q) scanf("%d %d %d %d %d", &n, &m, &o, &P, &q)
#define SLL(n) scanf("%lld", &n)
#define SLL2(n, m) scanf("%lld %lld", &n, &m)
#define SLL3(n, m, o) scanf("%lld %lld %lld", &n, &m, &o)
#define SST(s) scanf("%s", s)
#define SCH(c) scanf("%c", &c)

#define GC() getchar()

#define PINT(n) printf("%d", (int)(n))
#define PINT2(n, m) printf("%d %d", (int)(n), (int)(m))
#define PINT3(n, m, l) printf("%d %d %d", (int)(n), (int)(m), (int)(l))
#define PLL(n) printf("%lld", (long long)(n))
#define PST(s) printf("%s", (s))
#define PCH(s) printf("%c", (s))

#define PINTN(n) printf("%d\\n", (int)(n))
#define PINT2N(n, m) printf("%d %d\\n", (int)(n), (int)(m))
#define PINT3N(n, m, l) printf("%d %d %d\\n", (int)(n), (int)(m), (int)(l))
#define PLLN(n) printf("%lld\\n", (long long)(n))
#define PSTN(s) printf("%s\\n", (s))
#define PCHN(s) printf("%c\\n", (s))

#define PSP() printf(" ")
#define PN() printf("\\n")

#define PC(c) putchar(c)
#define CSP (' ')
#define SN ("\\n")

#define rep(i, a) for (int i = 0; i < a; i++)
#define reP(i, a) for (int i = 0; i <= a; i++)
#define Rep(i, a) for (int i = a - 1; i >= 0; i--)
#define ReP(i, a) for (int i = a; i >= 0; i--)

#define rEp(i, a) for (i = 0; i < a; i++)
#define rEP(i, a) for (i = 0; i <= a; i++)
#define REp(i, a) for (i = a - 1; i >= 0; i--)
#define REP(i, a) for (i = a; i >= 0; i--)

#define repft(i, a, b) for (int i = a; i < b; i++)
#define repfT(i, a, b) for (int i = a; i <= b; i++)
#define Repft(i, a, b) for (int i = a - 1; i >= b; i--)
#define RepfT(i, a, b) for (int i = a; i >= b; i--)

#define foreach(a, it) for (auto it = a.begin(); it != a.end(); ++it)

#define FILL(a, v) fill(begin(a), end(a), v)
#define FILL0(a) memset(a, 0, sizeof(a))
#define FILL1(a) memset(a, -1, sizeof(a))

typedef long long ll;

typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

const int INF = 1'010'000'000;         // 0x3C33'6080
const ll INFLL = 0x1f1f1f1f1f1f1f1fLL; // 2,242,545,357,980,376,863

template <class A, class B>
inline ostream &operator<<(ostream &st, const pair<A, B> &P) {
  return st << "(" << P.first << "," << P.second << ")";
};
template <class A, class B>
inline pair<A, B> operator+(const pair<A, B> &P, const pair<A, B> &Q) {
  return pair<A, B>(P.first + Q.first, P.second + Q.second);
};
template <class A, class B>
inline pair<A, B> operator-(const pair<A, B> &P, const pair<A, B> &Q) {
  return pair<A, B>(P.first - Q.first, P.second - Q.second);
};

inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

Pi ps[1 << 16];

int solve(const vector<Pi> &vw, const int W) {

  // cerr << "Solve " << W << endl;
  // for (const auto& p : vw) cout << p << endl;

  const int N = vw.size();
  const int n = N / 2;
  for (int i = 0; i < 1 << n; i++) {
    int sw = 0, sv = 0;
    for (int j = 0; j < n; j++) {
      if ((i >> j) & 1) {
        sw += vw[j].second;
        sv += vw[j].first;
      }
    }
    ps[i] = Pi(sw, sv);
  }
  sort(ps, ps + (1 << n));
  int m = 1;
  for (int i = 1; i < 1 << n; i++) {
    if (ps[m - 1].second < ps[i].second)
      ps[m++] = ps[i];
  }
  int res = 0;
  for (int i = 0; i < 1 << (N - n); i++) {
    int sw = 0, sv = 0;
    for (int j = 0; j < N - n; j++) {
      if ((i >> j) & 1) {
        sw += vw[n + j].second;
        sv += vw[n + j].first;
      }
    }
    if (sw <= W) {
      int tv = (lower_bound(ps, ps + m, make_pair(W - sw, INF)) - 1)->second;
      res = max(res, sv + tv);
    }
  }
  return res;
}

int n;
int v[1 << 20];
int w[1 << 20];

int main() {
  cin >> n;

  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  int q;
  cin >> q;
  vector<int> qv(q);
  vector<int> ql(q);
  rep(i, q) { cin >> qv[i] >> ql[i]; }
  vector<Pi> vw;
  vw.reserve(20);
  rep(i, q) {
    vw.clear();

    for (int j = qv[i]; j >= 1; j >>= 1) {
      vw.emplace_back(v[j], w[j]);
    }
    int ret = solve(vw, ql[i]);
    cout << ret << endl;
  }

  return 0;
}

/*

2**18 = 262,144
2** 9 = 512


512 * 512
100000 * 512 * 9


a + b = log(n)

2^a + a * 2^b


a = 10 b = 8    256 000 000
a = 12 b = 6     76 800 000



*/