Problem p03334 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pi pair<int, int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define inf 1000000000
using namespace std;
template <class T> inline void read(T &a) {
  static char c;
  int f = 0;
  while (((c = getchar()) < '0' || c > '9') && (c != '-'))
    ;
  if (c == '-') {
    a = 0;
    f = 1;
  } else {
    a = c - '0';
  }
  while ((c = getchar()) <= '9' && c >= '0')
    a = (a << 3) + (a << 1) + c - '0';
  if (f)
    a = -a;
}
bool vis[605][605];
int cnt = 0, num = 0, o = 0, d1, d2, n;
int dx[4] = {1, -1, 1, -1}, mod;
int dy[4] = {1, 1, -1, -1};
pi p[360005], g[360005], ans[100005];
inline bool cmp(pi a, pi b) {
  return (a.fi + a.se) % mod < (b.fi + b.se) % mod;
}
int main() {
  int i, j, k;
  scanf("%d%d%d", &n, &d1, &d2);
  for (i = 0; i < 2 * n; i++) {
    for (j = 0; j < 2 * n; j++) {
      p[++num] = mp(i, j);
      if ((i * i + j * j == d1) || (i * i + j * j == d2))
        g[++o] = mp(i, j);
    }
  }
  for (mod = 2;; mod++) {
    sort(p + 1, p + num + 1, cmp);
    memset(vis, 0, sizeof(vis));
    cnt = 0;
    for (i = 1; i <= num; i++) {
      if (vis[p[i].fi][p[i].se])
        continue;
      ans[++cnt] = p[i];
      if (cnt >= n * n)
        break;
      for (j = 1; j <= o; j++) {
        for (k = 0; k < 4; k++) {
          int x = p[i].fi + dx[k] * g[j].fi, y = p[i].se + dy[k] * g[j].se;
          if (x < 0 || x >= 2 * n || y < 0 || y >= 2 * n)
            continue;
          vis[x][y] = 1;
        }
      }
    }
    if (cnt >= n * n) {
      for (i = 1; i <= cnt; i++)
        printf("%d %d\n", ans[i].fi, ans[i].se);
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pi pair<int, int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define inf 1000000000
using namespace std;
template <class T> inline void read(T &a) {
  static char c;
  int f = 0;
  while (((c = getchar()) < '0' || c > '9') && (c != '-'))
    ;
  if (c == '-') {
    a = 0;
    f = 1;
  } else {
    a = c - '0';
  }
  while ((c = getchar()) <= '9' && c >= '0')
    a = (a << 3) + (a << 1) + c - '0';
  if (f)
    a = -a;
}
bool vis[605][605];
int cnt = 0, num = 0, o = 0, d1, d2, n;
int dx[4] = {1, -1, 1, -1}, mod;
int dy[4] = {1, 1, -1, -1};
pi p[360005], g[360005], ans[100005];
inline bool cmp(pi a, pi b) {
  return (a.fi + a.se) % mod < (b.fi + b.se) % mod;
}
int main() {
  int i, j, k;
  scanf("%d%d%d", &n, &d1, &d2);
  for (i = 0; i < 2 * n; i++) {
    for (j = 0; j < 2 * n; j++) {
      p[++num] = mp(i, j);
      if ((i * i + j * j == d1) || (i * i + j * j == d2))
        g[++o] = mp(i, j);
    }
  }
  for (mod = 2;; mod++) {
    stable_sort(p + 1, p + num + 1, cmp);
    memset(vis, 0, sizeof(vis));
    cnt = 0;
    for (i = 1; i <= num; i++) {
      if (vis[p[i].fi][p[i].se])
        continue;
      ans[++cnt] = p[i];
      if (cnt >= n * n)
        break;
      for (j = 1; j <= o; j++) {
        for (k = 0; k < 4; k++) {
          int x = p[i].fi + dx[k] * g[j].fi, y = p[i].se + dy[k] * g[j].se;
          if (x < 0 || x >= 2 * n || y < 0 || y >= 2 * n)
            continue;
          vis[x][y] = 1;
        }
      }
    }
    if (cnt >= n * n) {
      for (i = 1; i <= cnt; i++)
        printf("%d %d\n", ans[i].fi, ans[i].se);
      return 0;
    }
  }
  return 0;
}