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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll kai(ll x, ll y, ll m) {
  ll res = 1;
  for (ll i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= m;
  }
  return res;
}
ll mod_pow(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

ll comb(ll x, ll y, ll m) {
  if (y > x)
    return 0;
  return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

int n, q, x[300010], w[300010];
int d[520][100010];
int p[10];

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> w[i];
  for (int i = 1; i <= min(n, 511); i++) {
    int k = i / 2;
    for (int j = 0; j <= 100000; j++)
      d[i][j] = d[k][j];
    for (int j = 0; j <= 100000 - w[i]; j++)
      d[i][j + w[i]] = max(d[i][j + w[i]], d[k][j] + x[i]);
    for (int j = 1; j <= 100000; j++)
      d[i][j] = max(d[i][j], d[i][j - 1]);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    int ans = 0;
    int m = 0;
    while (512 <= v) {
      p[m] = v;
      m++;
      v /= 2;
    }
    for (int j = 0; j < (1 << m); j++) {
      int cnt1 = 0, cnt2 = 0;
      for (int k = 0; k < m; k++)
        if (j & (1 << k))
          cnt1 += x[p[k]], cnt2 += w[p[k]];
      if (l < cnt2)
        continue;
      ans = max(ans, d[v][l - cnt2] + cnt1);
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll kai(ll x, ll y, ll m) {
  ll res = 1;
  for (ll i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= m;
  }
  return res;
}
ll mod_pow(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

ll comb(ll x, ll y, ll m) {
  if (y > x)
    return 0;
  return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

int n, x[300010], w[300010], q;
int p[20];
vector<pair<int, int>> vec;

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> w[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    int ans = 0;
    vec.clear();
    int m = 0;
    while (v) {
      p[m] = v;
      m++;
      v /= 2;
    }
    vec.reserve(1 << (m / 2));
    for (int j = 0; j < (1 << (m / 2)); j++) {
      int cnt1 = 0, cnt2 = 0;
      for (int k = 0; k < m / 2; k++)
        if (j & (1 << k))
          cnt1 += x[p[k]], cnt2 += w[p[k]];
      if (cnt2 <= l)
        vec.push_back(make_pair(cnt2, cnt1));
    }
    sort(all(vec));
    for (int j = 1; j < (int)vec.size(); j++)
      vec[j].second = max(vec[j].second, vec[j - 1].second);
    for (int j = 0; j < (1 << (m - m / 2)); j++) {
      int cnt1 = 0, cnt2 = 0;
      for (int k = 0; k < m - m / 2; k++)
        if (j & (1 << k))
          cnt1 += x[p[m / 2 + k]], cnt2 += w[p[m / 2 + k]];
      if (l < cnt2)
        continue;
      int x =
          lower_bound(all(vec), make_pair(l - cnt2 + 1, 0)) - vec.begin() - 1;
      ans = max(ans, vec[x].second + cnt1);
    }
    cout << ans << endl;
  }
}