Problem p03334 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;

ll get_root(ll n) {
  assert(n >= 0);
  ll x = sqrt(n);
  while (x * x < n)
    x++;
  while (x * x > n)
    x--;
  if (x * x != n)
    return -1;
  return x;
}

vector<pii> get_pairs(ll n) {
  vector<pii> ans;
  for (ll a = 0; a * a <= n; a++) {
    ll b = get_root(n - a * a);
    if (b != -1) {
      assert(a * a + b * b == n);
      ans.emplace_back(a, b);
    }
  }
  return ans;
}

const int N = 666;

bool was[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);

  int n, a, b;
  cin >> n >> a >> b;

  /*
  int ans = 0;
  for (int i = 4; i <= 200 * 1000; i += 4) {
      int c = get_pairs(i).size();
      if ( c > ans ) {
          cerr << i << " " << c << endl;
          ans = c;
      }
  }
  */

  auto ps = get_pairs(a);
  for (auto p : get_pairs(b))
    ps.push_back(p);

  for (int rep = 2;; rep++) {
    vector<pii> cands;
    for (int i = 0; i < 2 * n; i++) {
      for (int j = 0; j < 2 * n; j++) {
        cands.emplace_back(i, j);
        was[i][j] = false;
      }
    }
    if (rep <= 7)
      stable_sort(cands.begin(), cands.end(), [rep](auto a, auto b) {
        return (a.first + a.second) % rep < (b.first + b.second) % rep;
      });
    else
      random_shuffle(cands.begin(), cands.end());
    vector<pii> ans;
    for (auto p : cands) {
      int i = p.first;
      int j = p.second;
      if (was[i][j] || ans.size() == n * n)
        continue;
      ans.emplace_back(i, j);
      for (int dx = -1; dx <= 1; dx += 2) {
        for (int dy = -1; dy <= 1; dy += 2) {
          for (auto d : ps) {
            int x = i + d.first * dx;
            int y = j + d.second * dy;
            if (x >= 0 && x < 2 * n && y >= 0 && y < 2 * n)
              was[x][y] = true;
          }
        }
      }
    }
    if (ans.size() < n * n) {
      continue;
    }
    for (auto p : ans)
      cout << p.first << " " << p.second << "\n";
    return 0;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;

ll get_root(ll n) {
  assert(n >= 0);
  ll x = sqrt(n);
  while (x * x < n)
    x++;
  while (x * x > n)
    x--;
  if (x * x != n)
    return -1;
  return x;
}

vector<pii> get_pairs(ll n) {
  vector<pii> ans;
  for (ll a = 0; a * a <= n; a++) {
    ll b = get_root(n - a * a);
    if (b != -1) {
      assert(a * a + b * b == n);
      ans.emplace_back(a, b);
    }
  }
  return ans;
}

const int N = 666;

bool was[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);

  int n, a, b;
  cin >> n >> a >> b;

  /*
  int ans = 0;
  for (int i = 4; i <= 200 * 1000; i += 4) {
      int c = get_pairs(i).size();
      if ( c > ans ) {
          cerr << i << " " << c << endl;
          ans = c;
      }
  }
  */

  auto ps = get_pairs(a);
  for (auto p : get_pairs(b))
    ps.push_back(p);

  for (int rep = 2;; rep++) {
    vector<pii> cands;
    for (int i = 0; i < 2 * n; i++) {
      for (int j = 0; j < 2 * n; j++) {
        cands.emplace_back(i, j);
        was[i][j] = false;
      }
    }
    if (rep <= 7)
      stable_sort(cands.begin(), cands.end(), [rep](auto a, auto b) {
        return (a.first + a.second) % rep < (b.first + b.second) % rep;
      });
    else
      stable_sort(cands.begin(), cands.end(), [rep](auto a, auto b) {
        return abs(a.first - a.second) % (rep - 6) <
               abs(b.first - b.second) % (rep - 6);
      });
    vector<pii> ans;
    for (auto p : cands) {
      int i = p.first;
      int j = p.second;
      if (was[i][j] || ans.size() == n * n)
        continue;
      ans.emplace_back(i, j);
      for (int dx = -1; dx <= 1; dx += 2) {
        for (int dy = -1; dy <= 1; dy += 2) {
          for (auto d : ps) {
            int x = i + d.first * dx;
            int y = j + d.second * dy;
            if (x >= 0 && x < 2 * n && y >= 0 && y < 2 * n)
              was[x][y] = true;
          }
        }
      }
    }
    if (ans.size() < n * n) {
      continue;
    }
    for (auto p : ans)
      cout << p.first << " " << p.second << "\n";
    return 0;
  }
}