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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P; // Pre val
#define fi first
#define se second

const int SIZE = 16777216;
int dat[SIZE * 2 - 1];
const int INF = LLONG_MAX / 100;
void update(int k, int x) {
  k += SIZE - 1;
  dat[k] = x;
  while (k) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int getMax(int a, int b, int k = 0, int l = 0, int r = SIZE) {
  if (a <= l && b >= r)
    return dat[k];
  if (r <= a || b <= l)
    return -INF;

  int m = (l + r) / 2;
  int vl = getMax(a, b, k * 2 + 1, l, m);
  int vr = getMax(a, b, k * 2 + 2, m, r);
  return max(vl, vr);
}

int N, D;
int X[30], Y[30];

vector<P> A, B;
void Latte(int cur, int last, int val, int pre, bool after) {
  if (cur == last) {
    if (after)
      B.push_back(P(pre, val));
    else
      A.push_back(P(pre, val));
    return;
  }

  Latte(cur + 1, last, val + X[cur], pre + Y[cur], after);
  Latte(cur + 1, last, val - X[cur], pre - Y[cur], after);
  Latte(cur + 1, last, val, pre, after);
}

signed main() {
  cin >> N >> D;
  for (int i = 0; i < N; i++)
    cin >> Y[i] >> X[i];

  Latte(0, N / 2, 0, 0, false);
  Latte(N / 2, N, 0, 0, true);

  sort(A.rbegin(), A.rend());
  sort(B.begin(), B.end());
  fill_n(dat, SIZE * 2 - 1, -INF);
  for (int i = 0; i < B.size(); i++) {
    update(i, B[i].second);
  }

  int ans = -INF;
  int lb = 0, ub = 0;
  for (int i = 0; i < A.size(); i++) {
    while (lb < B.size() && -D - A[i].fi > B[lb].fi)
      lb++;
    while (ub < B.size() && D - A[i].fi >= B[ub].fi)
      ub++;
    ans = max(ans, A[i].se + getMax(lb, ub));
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P; // Pre val
#define fi first
#define se second

const int SIZE = 16777216;
int dat[SIZE * 2 - 1];
const int INF = LLONG_MAX / 100;
void update(int k, int x) {
  k += SIZE - 1;
  dat[k] = x;
  while (k) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int getMax(int a, int b, int k = 0, int l = 0, int r = SIZE) {
  if (a <= l && b >= r)
    return dat[k];
  if (r <= a || b <= l)
    return -INF;

  int m = (l + r) / 2;
  int vl = getMax(a, b, k * 2 + 1, l, m);
  int vr = getMax(a, b, k * 2 + 2, m, r);
  return max(vl, vr);
}

int N, D;
int X[30], Y[30];

vector<P> A, B;
void Latte(int cur, int last, int val, int pre, bool after) {
  if (cur == last) {
    if (after)
      B.push_back(P(pre, val));
    else
      A.push_back(P(pre, val));
    return;
  }

  Latte(cur + 1, last, val + X[cur], pre - Y[cur], after);
  Latte(cur + 1, last, val - X[cur], pre + Y[cur], after);
  Latte(cur + 1, last, val, pre, after);
}

signed main() {
  cin >> N >> D;
  for (int i = 0; i < N; i++)
    cin >> Y[i] >> X[i];

  Latte(0, N / 2, 0, 0, false);
  Latte(N / 2, N, 0, 0, true);

  sort(B.begin(), B.end());
  fill_n(dat, SIZE * 2 - 1, -INF);
  for (int i = 0; i < B.size(); i++) {
    update(i, B[i].second);
  }

  int ans = -INF;
  for (int i = 0; i < A.size(); i++) {
    int lb = lower_bound(B.begin(), B.end(), P(-D - A[i].fi, -INF)) - B.begin();
    int ub = upper_bound(B.begin(), B.end(), P(D - A[i].fi, INF)) - B.begin();
    ans = max(ans, A[i].se + getMax(lb, ub));
  }
  cout << ans << endl;
  return 0;
}