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

#include <algorithm>
#include <assert.h>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int xcomp(vector<int> &a, vector<int> &b) { return a[0] < b[0]; }

int ycomp(vector<int> &a, vector<int> &b) { return a[1] < b[1]; }

#define XSORT(c) sort((c).begin(), (c).end(), xcomp)
#define YSORT(c) sort((c).begin(), (c).end(), ycomp)

#define ALL(x) (x).begin(), (x).end()

int henkan(int a) {
  if (a == 10000) {
    return 199;
  } else {
    return (a + 10000) / 100;
  }
}

int henkan2(int a) {
  if (a == 10000) {
    return 1999;
  } else {
    return (a + 10000) / 10;
  }
}
LL mod = 1000000007;
int main() {

  int n;
  cin >> n;

  if (n <= 40000) {
    vector<vector<int>> v(n, vector<int>(2));
    REP(i, n) { cin >> v[i][0] >> v[i][1]; }
    int min = 1000000000;
    REP(i, n) {
      FOR(j, i + 1, n) {
        int r = (v[i][0] - v[j][0]) * (v[i][0] - v[j][0]) +
                (v[i][1] - v[j][1]) * (v[i][1] - v[j][1]);
        if (r < min) {
          min = r;
        }
      }
    }
    cout << min << endl;
  } else {

    vector<vector<vector<vector<int>>>> v(
        200, vector<vector<vector<int>>>(200, vector<vector<int>>(0)));
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v[henkan(a)][henkan(b)].push_back({a, b});
    }
    int min = 100000;

    REP(i, 199) {

      REP(j, 199) {
        vector<vector<int>> vv(0);

        vv.insert(vv.end(), v[i][j].begin(), v[i][j].end());
        vv.insert(vv.end(), v[i][j + 1].begin(), v[i][j + 1].end());
        vv.insert(vv.end(), v[i + 1][j].begin(), v[i + 1][j].end());
        vv.insert(vv.end(), v[i + 1][j + 1].begin(), v[i + 1][j + 1].end());

        REP(ii, vv.size()) {
          FOR(jj, ii + 1, vv.size()) {
            int r = (vv[ii][0] - vv[jj][0]) * (vv[ii][0] - vv[jj][0]) +
                    (vv[ii][1] - vv[jj][1]) * (vv[ii][1] - vv[jj][1]);
            if (r < min) {
              min = r;
            }
          }
        }
      }
    }
    cout << min << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int xcomp(vector<int> &a, vector<int> &b) { return a[0] < b[0]; }

int ycomp(vector<int> &a, vector<int> &b) { return a[1] < b[1]; }

#define XSORT(c) sort((c).begin(), (c).end(), xcomp)
#define YSORT(c) sort((c).begin(), (c).end(), ycomp)

#define ALL(x) (x).begin(), (x).end()

int henkan(int a) {
  if (a == 10000) {
    return 199;
  } else {
    return (a + 10000) / 100;
  }
}

int henkan2(int a) {
  if (a == 10000) {
    return 1999;
  } else {
    return (a + 10000) / 10;
  }
}
LL mod = 1000000007;
int main() {

  int n;
  cin >> n;

  if (n <= 40000) {
    vector<vector<int>> v(n, vector<int>(2));
    REP(i, n) { cin >> v[i][0] >> v[i][1]; }
    int min = 1000000000;
    REP(i, n) {
      FOR(j, i + 1, n) {
        int r = (v[i][0] - v[j][0]) * (v[i][0] - v[j][0]) +
                (v[i][1] - v[j][1]) * (v[i][1] - v[j][1]);
        if (r < min) {
          min = r;
        }
      }
    }
    cout << min << endl;
  } else {

    vector<vector<vector<vector<int>>>> v(
        200, vector<vector<vector<int>>>(200, vector<vector<int>>(0)));
    vector<vector<vector<vector<int>>>> v2(
        2000, vector<vector<vector<int>>>(2000, vector<vector<int>>(0)));
    REP(i, n) {
      int a, b;
      cin >> a >> b;
      v[henkan(a)][henkan(b)].push_back({a, b});
      v2[henkan2(a)][henkan2(b)].push_back({a, b});
    }
    int min = 100000;

    REP(i, 199) {

      REP(j, 199) {
        vector<vector<int>> vv(0);

        vv.insert(vv.end(), v[i][j].begin(), v[i][j].end());
        vv.insert(vv.end(), v[i][j + 1].begin(), v[i][j + 1].end());
        vv.insert(vv.end(), v[i + 1][j].begin(), v[i + 1][j].end());
        vv.insert(vv.end(), v[i + 1][j + 1].begin(), v[i + 1][j + 1].end());

        if (vv.size() >= 101) {
          goto hoge;
        }
        REP(ii, vv.size()) {
          FOR(jj, ii + 1, vv.size()) {
            int r = (vv[ii][0] - vv[jj][0]) * (vv[ii][0] - vv[jj][0]) +
                    (vv[ii][1] - vv[jj][1]) * (vv[ii][1] - vv[jj][1]);
            if (r < min) {
              min = r;
            }
          }
        }
      }
    }
    if (false) {
    hoge:

      REP(i, 1999) {

        REP(j, 1999) {
          vector<vector<int>> vv(0);

          vv.insert(vv.end(), v2[i][j].begin(), v2[i][j].end());
          vv.insert(vv.end(), v2[i][j + 1].begin(), v2[i][j + 1].end());
          vv.insert(vv.end(), v2[i + 1][j].begin(), v2[i + 1][j].end());
          vv.insert(vv.end(), v2[i + 1][j + 1].begin(), v2[i + 1][j + 1].end());

          REP(ii, vv.size()) {
            FOR(jj, ii + 1, vv.size()) {
              int r = (vv[ii][0] - vv[jj][0]) * (vv[ii][0] - vv[jj][0]) +
                      (vv[ii][1] - vv[jj][1]) * (vv[ii][1] - vv[jj][1]);
              if (r < min) {
                min = r;
              }
            }
          }
        }
      }
    }
    cout << min << endl;
  }
}