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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<pll>::iterator itr;
#define MP make_pair
#define PB push_back
#define ALL(a) a.begin(), a.end()
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
#define DEBUG(...)
#define L 25
ll *rmq[L];
// ll src(int a, int b, int c){
// 	int p=b/(1<<a), q=c/(1<<a);
// 	ll ret=LLONG_MIN;
// 	if(b<c){
// 		FOR(i, p+(a>0), q){
// 			LAR(ret, rmq[a][i]);
// 		}
// 		if(a){
// 			if(p==q){
// 				LAR(ret, src(a-1, b, c));
// 			}else{
// 				LAR(ret, src(a-1, b, (p+1)*(1<<a)));
// 				LAR(ret, src(a-1, q*(1<<a), c));
// 			}
// 		}
// 	}
// 	return ret;
// }

vector<pll> u, v;
ll x[30], y[30];
int main() {
  REP(i, L) {
    int j = 1 << (L - i - 1);
    rmq[i] = new ll[j];
    fill(rmq[i], rmq[i] + j, LLONG_MIN);
  }
  int n;
  ll d;
  scanf("%d%lld", &n, &d);
  REP(i, n) scanf("%lld%lld", x + i, y + i);

  int k = n / 2, m = n - k;
  int f;

  f = 1;
  REP(i, k) f *= 3;
  REP(i, f) {
    ll p = 0, q = 0;
    int l = i;
    REP(j, k) {
      if (l % 3 == 0) { // Anna
        p += x[j];
        q -= y[j];
      } else if (l % 3 == 1) { // Bruno
        p -= x[j];
        q += y[j];
      }
      l /= 3;
    }
    u.PB(MP(p, q));
  }

  f = 1;
  REP(i, m) f *= 3;
  REP(i, f) {
    ll p = 0, q = 0;
    int l = i;
    REP(j, m) {
      if (l % 3 == 0) { // Anna
        p += x[k + j];
        q -= y[k + j];
      } else if (l % 3 == 1) { // Bruno
        p -= x[k + j];
        q += y[k + j];
      }
      l /= 3;
    }
    v.PB(MP(p, q));
  }

  sort(u.begin(), u.end());
  sort(v.begin(), v.end());
  // REP(i, u.size()) printf("%2d %5lld %5lld\\n", i, u[i].first, u[i].second);
  // REP(i, v.size()) printf("%2d %5lld %5lld\\n", i, v[i].first, v[i].second);
  REP(i, v.size()) {
    ll a = v[i].second;
    REP(j, L) { LAR(rmq[j][i >> j], a); }
  }
  ll ans = 0;
  int r = v.size(), s = v.size();

  for (pll &pr : u) {
    ll &p = pr.first, &q = pr.second;
    while (r && v[r - 1].first >= -d - p)
      r--;
    while (s && v[s - 1].first > d - p)
      s--;
    // printf("[%d, %d)\\n", r, s);
    if (r == s)
      continue;
    // printf("(%lld, %lld)->[%lld, %lld]=[%d, %d)\\n", p, q, -d-p, d-p, r, s);
    ll ret = LLONG_MIN;
    // ret=src(L-1, r, s);
    REP(a, L) {
      int x = (r + (1 << a) - 1) >> a, y = s >> a;
      if (((x + 1) << a) <= s) {
        // printf(" a[%d, %d)\\n", x<<a, (x+1)<<a);
        LAR(ret, rmq[a][x]);
      }
      if (r <= ((y - 1) << a)) {
        // printf(" b[%d, %d)\\n", (y-1)<<a, y<<a);
        LAR(ret, rmq[a][y - 1]);
      }
    }
    LAR(ans, q + ret);
  }
  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<pll>::iterator itr;
#define MP make_pair
#define PB push_back
#define ALL(a) a.begin(), a.end()
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
#define DEBUG(...)
#define L 25
ll *rmq[L];
ll src(int a, int b, int c) {
  int p = b / (1 << a), q = c / (1 << a);
  ll ret = LLONG_MIN;
  if (b < c) {
    FOR(i, p + (a > 0), q) { LAR(ret, rmq[a][i]); }
    if (a) {
      if (p == q) {
        LAR(ret, src(a - 1, b, c));
      } else {
        LAR(ret, src(a - 1, b, (p + 1) * (1 << a)));
        LAR(ret, src(a - 1, q * (1 << a), c));
      }
    }
  }
  return ret;
}

vector<pll> u, v;
ll x[30], y[30];
int main() {
  REP(i, L) {
    int j = 1 << (L - i - 1);
    rmq[i] = new ll[j];
    fill(rmq[i], rmq[i] + j, LLONG_MIN);
  }
  int n;
  ll d;
  scanf("%d%lld", &n, &d);
  REP(i, n) scanf("%lld%lld", x + i, y + i);

  int k = n / 2, m = n - k;
  int f;

  f = 1;
  REP(i, k) f *= 3;
  REP(i, f) {
    ll p = 0, q = 0;
    int l = i;
    REP(j, k) {
      if (l % 3 == 0) { // Anna
        p += x[j];
        q -= y[j];
      } else if (l % 3 == 1) { // Bruno
        p -= x[j];
        q += y[j];
      }
      l /= 3;
    }
    u.PB(MP(p, q));
  }

  f = 1;
  REP(i, m) f *= 3;
  REP(i, f) {
    ll p = 0, q = 0;
    int l = i;
    REP(j, m) {
      if (l % 3 == 0) { // Anna
        p += x[k + j];
        q -= y[k + j];
      } else if (l % 3 == 1) { // Bruno
        p -= x[k + j];
        q += y[k + j];
      }
      l /= 3;
    }
    v.PB(MP(p, q));
  }

  sort(u.begin(), u.end());
  sort(v.begin(), v.end());
  // REP(i, u.size()) printf("%2d %5lld %5lld\\n", i, u[i].first, u[i].second);
  // REP(i, v.size()) printf("%2d %5lld %5lld\\n", i, v[i].first, v[i].second);
  REP(i, v.size()) {
    ll a = v[i].second;
    REP(j, L) { LAR(rmq[j][i >> j], a); }
  }
  ll ans = 0;
  int r = v.size(), s = v.size();

  for (pll &pr : u) {
    ll &p = pr.first, &q = pr.second;
    while (r && v[r - 1].first >= -d - p)
      r--;
    while (s && v[s - 1].first > d - p)
      s--;
    if (r == s)
      continue;
    // printf("(%lld, %lld)->[%lld, %lld]=[%d, %d)\\n", p, q, -d-p, d-p, r, s);
    LAR(ans, q + src(L - 1, r, s));
  }
  printf("%lld\\n", ans);
}