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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << ", " << a.second << ")";
};
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cerr << *i << " ";
  cerr << endl;
}
template <class T> bool chmin(T &t, const T &f) {
  if (t > f) {
    t = f;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &t, const T &f) {
  if (t < f) {
    t = f;
    return true;
  }
  return false;
}

constexpr int MAX_N = (1 << 18) + 10;

int N;
int V[MAX_N], W[MAX_N];

int usLen;
int us[110];
int esLen, fsLen;
pair<int, int> es[1010], fs[1010];

int main() {
  for (; ~scanf("%d", &N);) {
    for (int u = 1; u <= N; ++u) {
      scanf("%d%d", &V[u], &W[u]);
    }
    int Q;
    scanf("%d", &Q);
    for (int q = 0; q < Q; ++q) {
      int X, L;
      scanf("%d%d", &X, &L);
      usLen = 0;
      for (int u = X; u; u >>= 1) {
        us[usLen++] = u;
      }
      const int half = usLen / 2;
      esLen = 1 << half;
      fsLen = 1 << (usLen - half);
      for (int i = 0; i < half; ++i) {
        for (int h = 0; h < 1 << i; ++h) {
          es[h | 1 << i].first = es[h].first + W[us[i]];
          es[h | 1 << i].second = es[h].second + V[us[i]];
        }
        inplace_merge(es, es + (1 << i), es + (1 << (i + 1)));
      }
      for (int i = 0; i < usLen - half; ++i) {
        for (int h = 0; h < 1 << i; ++h) {
          fs[h | 1 << i].first = fs[h].first + W[us[half + i]];
          fs[h | 1 << i].second = fs[h].second + V[us[half + i]];
        }
        inplace_merge(fs, fs + (1 << i), fs + (1 << (i + 1)));
      }
      int ans = 0;
      int mx = 0;
      for (int g = 0, h = fsLen; h--;) {
        for (; g < esLen && es[g].first + fs[h].first <= L; ++g) {
          chmax(mx, es[g].second);
        }
        if (g > 0) {
          chmax(ans, mx + fs[h].second);
        }
      }
      printf("%d\\n", ans);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << ", " << a.second << ")";
};
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cerr << *i << " ";
  cerr << endl;
}
template <class T> bool chmin(T &t, const T &f) {
  if (t > f) {
    t = f;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &t, const T &f) {
  if (t < f) {
    t = f;
    return true;
  }
  return false;
}

constexpr int MAX_N = (1 << 18) + 10;

int N;
int V[MAX_N], W[MAX_N];

int usLen;
int us[110];
int esLen, fsLen;
pair<int, int> es[1010], fs[1010];

int main() {
  for (; ~scanf("%d", &N);) {
    for (int u = 1; u <= N; ++u) {
      scanf("%d%d", &V[u], &W[u]);
    }
    int Q;
    scanf("%d", &Q);
    for (int q = 0; q < Q; ++q) {
      int X, L;
      scanf("%d%d", &X, &L);
      usLen = 0;
      for (int u = X; u; u >>= 1) {
        us[usLen++] = u;
      }
      const int half = usLen / 2;
      esLen = 1 << half;
      fsLen = 1 << (usLen - half);
      for (int i = 0; i < half; ++i) {
        for (int h = 0; h < 1 << i; ++h) {
          es[h | 1 << i].first = es[h].first + W[us[i]];
          es[h | 1 << i].second = es[h].second + V[us[i]];
        }
      }
      for (int i = 0; i < usLen - half; ++i) {
        for (int h = 0; h < 1 << i; ++h) {
          fs[h | 1 << i].first = fs[h].first + W[us[half + i]];
          fs[h | 1 << i].second = fs[h].second + V[us[half + i]];
        }
      }
      sort(es, es + esLen);
      sort(fs, fs + fsLen);
      int ans = 0;
      int mx = 0;
      for (int g = 0, h = fsLen; h--;) {
        for (; g < esLen && es[g].first + fs[h].first <= L; ++g) {
          chmax(mx, es[g].second);
        }
        if (g > 0) {
          chmax(ans, mx + fs[h].second);
        }
      }
      printf("%d\\n", ans);
    }
  }
  return 0;
}