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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>

using namespace std;

int hilbert(int x, int y, int d) {
  if (d < 0) {
    return 0;
  }
  if (x < (1 << d)) {
    if (y < (1 << d)) {
      return hilbert(y, x, d - 1);
    } else {
      return hilbert(x, y - (1 << d), d - 1) + (1 << d) * (1 << d);
    }
  } else {
    if (y < (1 << d)) {
      return hilbert((1 << d) - 1 - y, (1 << d) * 2 - 1 - x, d - 1) +
             (1 << d) * (1 << d) * 3;
    } else {
      return hilbert(x - (1 << d), y - (1 << d), d - 1) +
             (1 << d) * (1 << d) * 2;
    }
  }
}

#define M 5

#define INF (1 << 20)

class rec {
public:
  int xt, xb, yt, yb;

  bool operator<(const rec &a) const { return true; }
};

rec kou(rec m, rec n) {
  rec r;
  r.xt = min(m.xt, n.xt);
  r.xb = max(m.xb, n.xb);
  r.yt = min(m.yt, n.yt);
  r.yb = max(m.yb, n.yb);
  return r;
}

bool kt(rec b, rec n, int s) {
  s = sqrt((double)s) + 1;
  return (b.xt - n.xt > -s && b.xt - n.xb < s) &&
         (b.yt - n.yt > -s && b.yt - n.yb < s);
}

bool eq(rec a, rec b) {
  return a.xt == b.xt && a.xb == b.xb && a.yt == b.yt && a.yb == b.yb;
}

int ky(rec a, rec b) {
  return (a.xt - b.xt) * (a.xt - b.xt) + (a.yt - b.yt) * (a.yt - b.yt);
}

class rtree {
public:
  class point {
  public:
    int n;
    int k[M];
    rec r[M];
  };

  point *mm;
  int ulas;

  rtree(int s) {
    mm = (point *)malloc(sizeof(point) * (s / M * 3 + 10));
    ulas = 0;
  }

  int get() {
    ulas++;
    return ulas;
  }

  int make(vector<pair<int, rec>> d) {
    int k = (d.size() - 1) / M + 1;
    vector<pair<int, rec>> r(k);
    for (int i = 0; i < k; i++) {
      int s = i * d.size() / k;
      int e = (i + 1) * d.size() / k;
      int n = get();
      mm[n].n = e - s;
      rec b;
      b.xt = INF;
      b.xb = -INF;
      b.yt = INF;
      b.yb = -INF;
      for (int j = 0; j < e - s; j++) {
        mm[n].k[j] = d[s + j].first;
        mm[n].r[j] = d[s + j].second;
        b = kou(b, d[s + j].second);
      }
      r[i] = make_pair(n, b);
    }
    if (k == 1) {
      return r[0].first;
    }
    return make(r);
  }

  int find(rec r, int b, int s) {
    if (mm[b].k[0] < 0) {
      for (int i = 0; i < mm[b].n; i++) {
        if (!eq(r, mm[b].r[i])) {
          s = min(s, ky(r, mm[b].r[i]));
        }
      }
    } else {
      for (int i = 0; i < mm[b].n; i++) {
        if (kt(r, mm[b].r[i], s)) {
          s = find(r, mm[b].k[i], s);
        }
      }
    }
    return s;
  }
};

int main() {
  int N;
  scanf("%d", &N);
  vector<rec> w(N);
  for (int i = 0; i < N; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x += 10000;
    y += 10000;
    w[i].xt = x;
    w[i].xb = x;
    w[i].yt = y;
    w[i].yb = y;
  }
  vector<pair<int, rec>> d(N);
  for (int i = 0; i < d.size(); i++) {
    d[i] = make_pair(hilbert(w[i].xt, w[i].yt, 20), w[i]);
  }
  sort(d.begin(), d.end());
  for (int i = 0; i < N; i++) {
    d[i].first = -1;
  }
  int m = INF;
  for (int i = 0; i < N - 1; i++) {
    m = min(m, ky(d[i].second, d[i + 1].second));
  }
  rtree t(N);
  int o = t.make(d);
  for (int i = 0; i < N; i++) {
    m = t.find(w[i], o, m);
  }
  printf("%d\\n", m);
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>

using namespace std;

int hilbert(int x, int y, int d) {
  if (d < 0) {
    return 0;
  }
  if (x < (1 << d)) {
    if (y < (1 << d)) {
      return hilbert(y, x, d - 1);
    } else {
      return hilbert(x, y - (1 << d), d - 1) + (1 << d) * (1 << d);
    }
  } else {
    if (y < (1 << d)) {
      return hilbert((1 << d) - 1 - y, (1 << d) * 2 - 1 - x, d - 1) +
             (1 << d) * (1 << d) * 3;
    } else {
      return hilbert(x - (1 << d), y - (1 << d), d - 1) +
             (1 << d) * (1 << d) * 2;
    }
  }
}

#define M 5

#define INF (1 << 20)

class rec {
public:
  int xt, xb, yt, yb;

  bool operator<(const rec &a) const { return true; }
};

rec kou(rec m, rec n) {
  rec r;
  r.xt = min(m.xt, n.xt);
  r.xb = max(m.xb, n.xb);
  r.yt = min(m.yt, n.yt);
  r.yb = max(m.yb, n.yb);
  return r;
}

bool kt(rec b, rec n, int s) {
  s *= s;
  return (b.xt - n.xt > -s && b.xt - n.xb < s) &&
         (b.yt - n.yt > -s && b.yt - n.yb < s);
}

bool eq(rec a, rec b) {
  return a.xt == b.xt && a.xb == b.xb && a.yt == b.yt && a.yb == b.yb;
}

int ky(rec a, rec b) {
  return (a.xt - b.xt) * (a.xt - b.xt) + (a.yt - b.yt) * (a.yt - b.yt);
}

class rtree {
public:
  class point {
  public:
    int n;
    int k[M];
    rec r[M];
  };

  point *mm;
  int ulas;

  rtree(int s) {
    mm = (point *)malloc(sizeof(point) * (s / M * 3 + 10));
    ulas = 0;
  }

  int get() {
    ulas++;
    return ulas;
  }

  int make(vector<pair<int, rec>> d) {
    int k = (d.size() - 1) / M + 1;
    vector<pair<int, rec>> r(k);
    for (int i = 0; i < k; i++) {
      int s = i * d.size() / k;
      int e = (i + 1) * d.size() / k;
      int n = get();
      mm[n].n = e - s;
      rec b;
      b.xt = INF;
      b.xb = -INF;
      b.yt = INF;
      b.yb = -INF;
      for (int j = 0; j < e - s; j++) {
        mm[n].k[j] = d[s + j].first;
        mm[n].r[j] = d[s + j].second;
        b = kou(b, d[s + j].second);
      }
      r[i] = make_pair(n, b);
    }
    if (k == 1) {
      return r[0].first;
    }
    return make(r);
  }

  int find(rec r, int b, int s) {
    if (mm[b].k[0] < 0) {
      for (int i = 0; i < mm[b].n; i++) {
        if (!eq(r, mm[b].r[i])) {
          s = min(s, ky(r, mm[b].r[i]));
        }
      }
    } else {
      for (int i = 0; i < mm[b].n; i++) {
        if (kt(r, mm[b].r[i], s)) {
          s = min(s, find(r, mm[b].k[i], s));
        }
      }
    }
    return s;
  }
};

int main() {
  int N;
  scanf("%d", &N);
  vector<rec> w(N);
  for (int i = 0; i < N; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x += 10000;
    y += 10000;
    w[i].xt = x;
    w[i].xb = x;
    w[i].yt = y;
    w[i].yb = y;
  }
  vector<pair<int, rec>> d(N);
  for (int i = 0; i < d.size(); i++) {
    d[i] = make_pair(hilbert(w[i].xt, w[i].yt, 20), w[i]);
  }
  sort(d.begin(), d.end());
  for (int i = 0; i < N; i++) {
    d[i].first = -1;
  }
  int m = INF;
  for (int i = 0; i < N - 1; i++) {
    m = min(m, ky(d[i].second, d[i + 1].second));
  }
  rtree t(N);
  int o = t.make(d);
  for (int i = 0; i < N; i++) {
    m = t.find(w[i], o, m);
  }
  printf("%d\\n", m);
}