Problem p03858 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;

unordered_map<int, set<int>> X, Y;
unordered_map<int, vector<int>> pX, pY;
map<pair<int, int>, int> Gree;
int n, a, b, x[N], y[N], d;
long long ans;

int abs(int x) { return x < 0 ? ~x + 1 : x; }

int find(const vector<int> &T, int pl, int pr) {
  int l = 0, r = T.size() - 1, mid, ret;
  for (int i = l + 1; i <= r; ++i)
    if (T[i - 1] > T[i]) {

      puts("GG");
      exit(0);
    }
  while (l <= r) {
    mid = (l + r) >> 1;
    if (T[mid] > pr)
      r = mid - 1;
    else
      l = mid + 1;
  }
  ret = l;
  l = 0, r = T.size() - 1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (T[mid] >= pl)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return ret - l;
}

void Dfs(int, int);

void find(const set<int> &T, int pl, int pr, int zb, bool o) {
  for (set<int>::iterator p; (p = T.lower_bound(pl)), p != T.end() && *p <= pr;)
    if (o)
      Dfs(*p, zb);
    else
      Dfs(zb, *p);
}

void Erase(int x, int y) { X[x].erase(y), Y[y].erase(x); }

void Dfs(int x, int y) {
  ans += Gree[pair<int, int>(x, y)];
  Erase(x, y);
  find(X[x - d], y - d, y + d, x - d, 0);
  find(X[x + d], y - d, y + d, x + d, 0);
  find(Y[y - d], x - d, x + d, y - d, 1);
  find(Y[y + d], x - d, x + d, y + d, 1);
}

int main() {
  scanf("%d%d%d", &n, &a, &b);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &x[i], &y[i]);
    x[i] += y[i], y[i] = x[i] - (y[i] << 1);
    X[x[i]].insert(y[i]), Y[y[i]].insert(x[i]);
    pX[x[i]].emplace_back(y[i]), pY[y[i]].emplace_back(x[i]);
  }
  d = max(abs(x[a] - x[b]), abs(y[a] - y[b]));
  for (auto &v : pX)
    sort(v.second.begin(), v.second.end());
  for (auto &v : pY)
    sort(v.second.begin(), v.second.end());
  for (int i = 1; i <= n; ++i) {
    Gree[pair<int, int>(x[i], y[i])] =
        find(pX[x[i] - d], y[i] - d, y[i] + d) +
        find(pX[x[i] + d], y[i] - d, y[i] + d) +
        find(pY[y[i] - d], x[i] - d + 1, x[i] + d - 1) +
        find(pY[y[i] + d], x[i] - d + 1, x[i] + d - 1);
  }
  Dfs(x[a], y[a]);
  printf("%lld\n", ans >> 1);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;

unordered_map<int, set<int>> X, Y;
unordered_map<int, vector<int>> pX, pY;
map<pair<int, int>, int> Gree;
int n, a, b, x[N], y[N], d;
long long ans;

int abs(int x) { return x < 0 ? ~x + 1 : x; }

int find(const vector<int> &T, int pl, int pr) {
  int l = 0, r = T.size() - 1, mid, ret;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (T[mid] > pr)
      r = mid - 1;
    else
      l = mid + 1;
  }
  ret = l;
  l = 0, r = T.size() - 1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (T[mid] >= pl)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return ret - l;
}

void Dfs(int, int);

void find(const set<int> &T, int pl, int pr, int zb, bool o) {
  for (set<int>::iterator p; (p = T.lower_bound(pl)), p != T.end() && *p <= pr;)
    if (o)
      Dfs(*p, zb);
    else
      Dfs(zb, *p);
}

void Erase(int x, int y) { X[x].erase(y), Y[y].erase(x); }

void Dfs(int x, int y) {
  ans += Gree[pair<int, int>(x, y)];
  Erase(x, y);
  find(X[x - d], y - d, y + d, x - d, 0);
  find(X[x + d], y - d, y + d, x + d, 0);
  find(Y[y - d], x - d, x + d, y - d, 1);
  find(Y[y + d], x - d, x + d, y + d, 1);
}

int main() {
  scanf("%d%d%d", &n, &a, &b);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &x[i], &y[i]);
    x[i] += y[i], y[i] = x[i] - (y[i] << 1);
    X[x[i]].insert(y[i]), Y[y[i]].insert(x[i]);
    pX[x[i]].emplace_back(y[i]), pY[y[i]].emplace_back(x[i]);
  }
  d = max(abs(x[a] - x[b]), abs(y[a] - y[b]));
  for (auto &v : pX)
    sort(v.second.begin(), v.second.end());
  for (auto &v : pY)
    sort(v.second.begin(), v.second.end());
  for (int i = 1; i <= n; ++i) {
    Gree[pair<int, int>(x[i], y[i])] =
        find(pX[x[i] - d], y[i] - d, y[i] + d) +
        find(pX[x[i] + d], y[i] - d, y[i] + d) +
        find(pY[y[i] - d], x[i] - d + 1, x[i] + d - 1) +
        find(pY[y[i] + d], x[i] - d + 1, x[i] + d - 1);
  }
  Dfs(x[a], y[a]);
  printf("%lld\n", ans >> 1);
  return 0;
}