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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
const int MAXN = 262144, MAXM = 30000000;
int P;
#define L 1048576
#define real long double
#define eps 1e-9
char _buf[L], *S, *T, _buf1[L], *S1 = _buf1;
inline char gc() {
  if (S == T) {
    T = (S = _buf) + fread(_buf, 1, L, stdin);
    if (S == T)
      return 0;
  }
  return *S++;
}
template <class T> void get(T &x) {
  char c;
  for (c = gc(); (c < '0' || c > '9') && c != '-'; c = gc())
    ;
  bool b = 0;
  if (c == '-') {
    b = 1;
    c = gc();
  }
  for (x = c ^ '0', c = gc(); c >= '0' && c <= '9'; c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  if (b)
    x = -x;
}
inline void pc(char c) {
  if (S1 == _buf1 + L) {
    fwrite(_buf1, 1, L, stdout);
    S1 = _buf1;
  }
  *S1++ = c;
}
void out(ll x) {
  if (x > 9)
    out(x / 10);
  pc(x % 10 ^ '0');
}
int Pow(int x, int y) {
  int s = 1;
  for (; y; y >>= 1, x = (ll)x * x % P)
    if (y & 1)
      s = (ll)s * x % P;
  return s;
}
int A(int x) { return x < 0 ? -x : x; }
int n, m, N, N0, i, j, ans, q, a[MAXN], b[MAXN], u[20], v[20], o[512], d[MAXN];
struct node {
  int x, y;
  bool operator<(const node &b) const {
    if (x == b.x)
      return y < b.y;
    return x < b.x;
  }
} f[4096][4096], g[4096], t;
int main() {
  get(n);
  for (i = 1; i <= n; i++) {
    get(a[i]);
    get(b[i]);
    d[i] = d[i >> 1] + 1;
  }
  for (i = 0; i < 9; i++)
    o[1 << i] = i;
  N0 = min(4095, n);
  for (i = 1; i <= N0; i++)
    for (j = 0; j < 1 << d[i]; j++) {
      f[i][j] = f[i >> 1][j >> 1];
      if (j & 1) {
        f[i][j].x += b[i];
        f[i][j].y += a[i];
      }
    }
  for (i = 1; i <= N0; i++) {
    sort(f[i], f[i] + (1 << d[i]));
    for (j = 1; j < 1 << d[i]; j++)
      f[i][j].y = max(f[i][j].y, f[i][j - 1].y);
  }
  get(q);
  while (q--) {
    get(j);
    get(m);
    for (N = 0; j > N0; j >>= 1) {
      u[N] = b[j];
      v[N++] = a[j];
    }
    t.x = m + 1;
    t.y = 0;
    ans = (lower_bound(f[j], f[j] + (1 << d[j]), t) - 1)->y;
    g[0].x = g[0].y = 0;
    for (i = 1; i < 1 << N; i++) {
      g[i].x = g[i ^ (i & -i)].x + u[o[i & -i]];
      g[i].y = g[i ^ (i & -i)].y + v[o[i & -i]];
      if (g[i].x <= m) {
        t.x = m + 1 - g[i].x;
        t.y = 0;
        ans = max(ans,
                  g[i].y + (lower_bound(f[j], f[j] + (1 << d[j]), t) - 1)->y);
      }
    }
    out(ans);
    pc('\\n');
  }
  fwrite(_buf1, 1, S1 - _buf1, stdout);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
const int MAXN = 262144, MAXM = 30000000;
int P;
#define L 1048576
#define real long double
#define eps 1e-9
char _buf[L], *S, *T, _buf1[L], *S1 = _buf1;
inline char gc() {
  if (S == T) {
    T = (S = _buf) + fread(_buf, 1, L, stdin);
    if (S == T)
      return 0;
  }
  return *S++;
}
template <class T> void get(T &x) {
  char c;
  for (c = gc(); (c < '0' || c > '9') && c != '-'; c = gc())
    ;
  bool b = 0;
  if (c == '-') {
    b = 1;
    c = gc();
  }
  for (x = c ^ '0', c = gc(); c >= '0' && c <= '9'; c = gc())
    x = (x << 3) + (x << 1) + (c ^ '0');
  if (b)
    x = -x;
}
inline void pc(char c) {
  if (S1 == _buf1 + L) {
    fwrite(_buf1, 1, L, stdout);
    S1 = _buf1;
  }
  *S1++ = c;
}
void out(ll x) {
  if (x > 9)
    out(x / 10);
  pc(x % 10 ^ '0');
}
int Pow(int x, int y) {
  int s = 1;
  for (; y; y >>= 1, x = (ll)x * x % P)
    if (y & 1)
      s = (ll)s * x % P;
  return s;
}
int A(int x) { return x < 0 ? -x : x; }
int n, m, N, N0, N1, i, j, ans, q, a[MAXN], b[MAXN], u[20], v[20], o[512];
struct node {
  int x, y;
  bool operator<(const node &b) const {
    if (x == b.x)
      return y < b.y;
    return x < b.x;
  }
} f[512], g[512], t;
int main() {
  get(n);
  for (i = 1; i <= n; i++) {
    get(a[i]);
    get(b[i]);
  }
  for (i = 0; i < 9; i++)
    o[1 << i] = i;
  get(q);
  while (q--) {
    get(i);
    get(m);
    for (N = 0; i; i >>= 1) {
      u[N] = b[i];
      v[N++] = a[i];
    }
    N0 = N >> 1;
    N1 = N + 1 >> 1;
    f[0].x = f[0].y = 0;
    for (i = 1; i < 1 << N0; i++) {
      f[i].x = f[i ^ (i & -i)].x + u[o[i & -i]];
      f[i].y = f[i ^ (i & -i)].y + v[o[i & -i]];
    }
    sort(f, f + (1 << N0));
    for (i = 1; i < 1 << N0; i++)
      f[i].y = max(f[i].y, f[i - 1].y);
    for (i = ans = 0; i < 1 << N0; i++)
      if (f[i].x <= m)
        ans = f[i].y;
    g[0].x = g[0].y = 0;
    for (i = 1; i < 1 << N1; i++) {
      g[i].x = g[i ^ (i & -i)].x + u[o[i & -i] + N0];
      g[i].y = g[i ^ (i & -i)].y + v[o[i & -i] + N0];
      t.x = m - g[i].x + 1;
      t.y = 0;
      if (g[i].x <= m)
        ans = max(ans, g[i].y + f[lower_bound(f, f + (1 << N0), t) - f - 1].y);
    }
    out(ans);
    pc('\\n');
  }
  fwrite(_buf1, 1, S1 - _buf1, stdout);
  return 0;
}