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

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<lint, lint> Pr;
const int N = 1 << 24;
const lint INF = 1ll << 56;

int n;
lint d;
lint x[30], y[30];

vector<Pr> v1, v2;

bool next_pat(int pat[]) {
  pat[0]++;
  for (int i = 0; i < 15; i++) {
    if (pat[i] == 3) {
      pat[i] = 0;
      pat[i + 1]++;
    } else
      return true;
  }
  return false;
}

void makeSeq(vector<Pr> &v, lint x[], lint y[]) {
  v.reserve(14348907);
  int pat[16] = {};
  do {
    Pr p(0, 0);
    for (int i = 0; i < 15; i++) {
      if (pat[i] == 1) {
        p.first += x[i];
        p.second += y[i];
      } else if (pat[i] == 2) {
        p.first -= x[i];
        p.second -= y[i];
      }
    }
    v.push_back(p);
  } while (next_pat(pat));

  sort(v.begin(), v.end());
}

int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  n = 30;

  makeSeq(v1, x, y);
  makeSeq(v2, x + 15, y + 15);
  reverse(v2.begin(), v2.end());

  lint res = 0;
  deque<int> dq;
  int l = 0, r = 0;
  for (int i = 0; i < v1.size(); i++) {
    Pr pl(-v1[i].first + d, INF);
    Pr pr(-v1[i].first - d, -INF);

    while (r < v2.size() && v2[r] > pr) {
      while (dq.size() && v2[dq.back()].second <= v2[r].second) {
        dq.pop_back();
      }
      dq.push_back(r++);
    }

    while (l < v2.size() && v2[l] > pl) {
      if (dq.size() && dq.front() == l) {
        dq.pop_front();
      }
      l++;
    }

    if (dq.size()) {
      res = max(res, v1[i].second + v2[dq.front()].second);
    }
  }

  for (int i = 0; i < v1.size(); i++) {
    if (abs(v1[i].first) <= d)
      res = max(res, v1[i].second);
    if (abs(v2[i].first) <= d)
      res = max(res, v2[i].second);
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<lint, lint> Pr;
const int N = 1 << 24;
const lint INF = 1ll << 56;

int n;
lint d;
lint x[30], y[30];

vector<Pr> v;
lint rmq[N * 2];

void update(int k, lint x) {
  k += N - 1;
  rmq[k] = x;
  while (k) {
    k = (k - 1) / 2;
    rmq[k] = max(rmq[k * 2 + 1], rmq[k * 2 + 2]);
  }
}

lint query(int l, int r, int k = 0, int a = 0, int b = N) {
  if (b <= l || r <= a)
    return -INF;
  if (l <= a && b <= r)
    return rmq[k];
  int m = (a + b) / 2;
  lint vl = query(l, r, k * 2 + 1, a, m);
  lint vr = query(l, r, k * 2 + 2, m, b);
  return max(vl, vr);
}

inline bool next_pat(int pat[]) {
  pat[0]++;
  for (int i = 0; i < 15; i++) {
    if (pat[i] == 3) {
      pat[i] = 0;
      pat[i + 1]++;
    } else
      return true;
  }
  return false;
}

int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  n = 30;

  int pat[16] = {};

  v.reserve(14348907);
  do {
    Pr p(0, 0);
    for (int i = 0; i < 15; i++) {
      if (pat[i] == 1) {
        p.first += x[i + 15];
        p.second += y[i + 15];
      } else if (pat[i] == 2) {
        p.first -= x[i + 15];
        p.second -= y[i + 15];
      }
    }
    v.push_back(p);
  } while (next_pat(pat));

  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    update(i, v[i].second);
  }

  lint res = 0;

  do {
    Pr p(0, 0);
    for (int i = 0; i < 15; i++) {
      if (pat[i] == 1) {
        p.first += x[i];
        p.second += y[i];
      } else if (pat[i] == 2) {
        p.first -= x[i];
        p.second -= y[i];
      }
    }

    Pr pl(-p.first - d, -INF);
    Pr pr(-p.first + d, INF);
    int l = lower_bound(v.begin(), v.end(), pl) - v.begin();
    int r = upper_bound(v.begin(), v.end(), pr) - v.begin();
    lint t = query(l, r);
    res = max(res, t + p.second);
  } while (next_pat(pat));

  cout << res << endl;

  return 0;
}