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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int height[1001][1001];
bool visited[1001][1001];

int main() {
  ll n;
  while (cin >> n, n) {
    memset(visited, 0, sizeof(visited));
    int sqn = sqrt(n);
    for (int y = 0; y <= sqn; ++y) {
      for (int x = 0; x * x + y * y < n; ++x) {
        height[y][x] = ceil(sqrt(n - x * x - y * y));
      }
    }
    int ans = 0;
    for (int y = 0; y <= sqn; ++y) {
      for (int x = 0; x * x + y * y < n; ++x) {
        if (visited[y][x])
          continue;
        if (height[y][x] == 0)
          continue;
        queue<pii> Q;
        Q.push(pii(y, x));
        int flag = 0;
        while (!Q.empty()) {
          pii p = Q.front();
          Q.pop();
          int y = p.first;
          int x = p.second;
          if (visited[y][x])
            continue;
          visited[y][x] = 1;
          if (y == 0)
            flag |= 1;
          if (x == 0)
            flag |= 2;
          REP(i, 4) {
            int yy = y + dy[i];
            int xx = x + dx[i];
            if (yy < 0 || yy > sqn || xx < 0 || xx > sqn)
              continue;
            if (height[yy][xx] != height[y][x])
              continue;
            if (visited[yy][xx])
              continue;
            Q.push(pii(yy, xx));
          }
        }
        if (flag == 0)
          ans += 24;
        else if (flag == 1 || flag == 2)
          ans += 12;
        else
          ans += 6;
      }
    }

    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int main() {
  ll n;
  while (cin >> n, n) {
    map<pii, int> height;
    for (ll y = 0; y <= sqrt(n); ++y) {
      for (ll x = 0; x * x + y * y < n; ++x) {
        height[pii(y, x)] = ceil(sqrt(n - x * x - y * y));
      }
    }
    map<pii, bool> visited;
    int ans = 0;
    FOR(it, height) {
      // cout << it->first.first << "," << it->first.second << " "  <<
      // it->second << endl;
      if (visited[it->first])
        continue;
      queue<pii> Q;
      Q.push(it->first);
      int flag = 0;
      while (!Q.empty()) {
        pii p = Q.front();
        Q.pop();
        if (visited[p])
          continue;
        visited[p] = 1;
        if (p.first == 0)
          flag |= 1;
        if (p.second == 0)
          flag |= 2;
        REP(i, 4) {
          int yy = p.first + dy[i];
          int xx = p.second + dx[i];
          if (height.count(pii(yy, xx)) == 0)
            continue;
          if (height[pii(yy, xx)] != height[p])
            continue;
          if (visited[pii(yy, xx)])
            continue;
          Q.push(pii(yy, xx));
        }
      }
      if (flag == 0)
        ans += 24;
      else if (flag == 1 || flag == 2)
        ans += 12;
      else
        ans += 6;
    }
    cout << ans << endl;
  }
}