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

#include <bits/stdc++.h>
using namespace std;

using LL = long long;
enum { W, V };
using P = tuple<LL, LL>;

int main() {
  LL N, D;
  cin >> N >> D;

  vector<LL> X(N), Y(N);
  for (auto i = 0; i < N; ++i)
    cin >> X[i] >> Y[i];

  vector<P> former, latter;
  function<void(int, int, P, vector<P> &)> dfs = [&](int l, int r, P p,
                                                     vector<P> &c) {
    if (l == r) {
      c.emplace_back(p);
    } else {
      dfs(l + 1, r, p, c);
      dfs(l + 1, r, P(get<W>(p) - X[l], get<V>(p) - Y[l]), c);
      dfs(l + 1, r, P(get<W>(p) + X[l], get<V>(p) + Y[l]), c);
    }
  };
  dfs(0, N / 2, P(0, 0), former);
  dfs(N / 2, N, P(0, 0), latter);
  sort(begin(former), end(former));
  sort(begin(latter), end(latter));

  LL ans = 0;
  for (auto *p : {&former, &latter})
    for (auto i : *p)
      if (abs(get<W>(i)) <= D)
        ans = max(ans, abs(get<V>(i)));

  multiset<LL> S;
  int l, r;
  l = r = upper_bound(begin(former), end(former), -get<W>(latter[0]) + D,
                      [](auto v, auto i) { return v < get<W>(i); }) -
          begin(former);
  for (auto p : latter) {
    while (l && -get<W>(p) - D <= get<W>(former[l - 1]))
      S.insert(get<V>(former[--l]));
    while (r && -get<W>(p) + D < get<W>(former[r - 1]))
      S.erase(get<V>(former[--r]));
    if (!S.empty())
      ans = max(ans, abs(get<V>(p) + *begin(S)));
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using LL = long long;
enum { W, V };
using P = tuple<LL, LL>;

int pow3(int n) {
  static vector<int> memo = {1};
  while (memo.size() <= n)
    memo.push_back(memo.back() * 3);
  return memo[n];
}

int main() {
  LL N, D;
  cin >> N >> D;

  vector<LL> X(N), Y(N);
  for (auto i = 0; i < N; ++i)
    cin >> X[i] >> Y[i];

  vector<P> former, latter;
  auto N1 = N / 2;
  auto N2 = N - N1;
  for (auto trit = 0; trit < pow3(N1); ++trit) {
    LL w = 0, v = 0;
    for (auto i = 0; i < N1; ++i) {
      auto d = (trit / pow3(i)) % 3;
      if (d == 1)
        w -= X[i], v -= Y[i];
      if (d == 2)
        w += X[i], v += Y[i];
    }
    former.emplace_back(w, v);
  }
  for (auto trit = 0; trit < pow3(N2); ++trit) {
    LL w = 0, v = 0;
    for (auto i = 0; i < N2; ++i) {
      auto d = (trit / pow3(i)) % 3;
      if (d == 1)
        w -= X[N1 + i], v -= Y[N1 + i];
      if (d == 2)
        w += X[N1 + i], v += Y[N1 + i];
    }
    latter.emplace_back(w, v);
  }
  sort(begin(former), end(former));
  sort(begin(latter), end(latter));
  former.erase(unique(begin(former), end(former)), end(former));
  latter.erase(unique(begin(latter), end(latter)), end(latter));

  LL ans = 0;
  for (auto *p : {&former, &latter})
    for (auto i : *p)
      if (abs(get<W>(i)) <= D)
        ans = max(ans, abs(get<V>(i)));

  multiset<LL> S;
  int l, r;
  l = r = upper_bound(begin(former), end(former), -get<W>(latter[0]) + D,
                      [](auto v, auto i) { return v < get<W>(i); }) -
          begin(former);
  for (auto p : latter) {
    while (l && -get<W>(p) - D <= get<W>(former[l - 1]))
      S.insert(get<V>(former[--l]));
    while (r && -get<W>(p) + D < get<W>(former[r - 1]))
      S.erase(get<V>(former[--r]));
    if (!S.empty())
      ans = max(ans, abs(get<V>(p) + *begin(S)));
  }

  cout << ans << endl;
}