Problem p03334 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("trapv")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,tune=native")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
// using arr = array;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using ld = long double;
const int maxn = 305, mod = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, A, B, col[maxn][maxn], f[maxn][maxn];
vpi sols;
void dfs(int x, int y) {
  for (auto &i : sols) {
    int tx = x + i.first, ty = y + i.second;
    if (tx < 0 || tx >= 2 * n || ty < 0 || ty >= 2 * n || col[tx][ty] ||
        f[tx][ty])
      continue;
    col[tx][ty] = 3 ^ col[x][y];
    dfs(tx, ty);
  }
}
void solve(vpi &a, int d) {
  memset(col, 0, sizeof col);
  memset(f, 0x69, sizeof f);
  sols.clear();
  for (int i = 0; i < 1000; i++)
    for (int j = 0; j < 1000; j++) {
      if (i * i + j * j == d) {
        sols.pb({i, j});
        sols.pb({-i, j});
        sols.pb({i, -j});
        sols.pb({-i, -j});
      }
    }
  for (auto i : a)
    f[i.first][i.second] = 0;
  vpi l, r;

  for (auto t : a) {
    int i = t.first, j = t.second;
    if (!col[i][j]) {
      col[i][j] = 1;
      dfs(i, j);
    }
    (col[i][j] == 1 ? l : r).pb({i, j});
  }

  if (l.size() >= r.size())
    a = l;
  else
    a = r;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> A >> B;
  vpi ans;
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      ans.pb({i, j});
  solve(ans, A);
  solve(ans, B);
  for (int i = 0; i < n * n; i++)
    cout << ans[i].first << " " << ans[i].second << '\n';
}


Fixed submission:
// #pragma GCC optimize("trapv")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,tune=native")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
// using arr = array;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using ld = long double;
const int maxn = 606, mod = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, A, B, col[maxn][maxn], f[maxn][maxn];
vpi sols;
void dfs(int x, int y) {
  for (auto &i : sols) {
    int tx = x + i.first, ty = y + i.second;
    if (tx < 0 || tx >= 2 * n || ty < 0 || ty >= 2 * n || col[tx][ty] ||
        f[tx][ty])
      continue;
    col[tx][ty] = 3 ^ col[x][y];
    dfs(tx, ty);
  }
}
void solve(vpi &a, int d) {
  memset(col, 0, sizeof col);
  memset(f, 0x69, sizeof f);
  sols.clear();
  for (int i = 0; i < 1000; i++)
    for (int j = 0; j < 1000; j++) {
      if (i * i + j * j == d) {
        sols.pb({i, j});
        sols.pb({-i, j});
        sols.pb({i, -j});
        sols.pb({-i, -j});
      }
    }
  for (auto i : a)
    f[i.first][i.second] = 0;
  vpi l, r;

  for (auto t : a) {
    int i = t.first, j = t.second;
    if (!col[i][j]) {
      col[i][j] = 1;
      dfs(i, j);
    }
    (col[i][j] == 1 ? l : r).pb({i, j});
  }

  if (l.size() >= r.size())
    a = l;
  else
    a = r;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> A >> B;
  vpi ans;
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      ans.pb({i, j});
  solve(ans, A);
  solve(ans, B);
  for (int i = 0; i < n * n; i++)
    cout << ans[i].first << " " << ans[i].second << '\n';
}