Problem p03594 (C++)

Buggy submission (status: Runtime Error):
#include <assert.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

char rgby(int x) {
  if (x == 0)
    return 'R';
  if (x == 1)
    return 'G';
  if (x == 2)
    return 'B';
  if (x == 3)
    return 'Y';
  if (x == -1)
    assert(false);
}

int main() {
  ll H, W, d;
  cin >> H >> W >> d;
  vector<vector<int>> ans(H, vector<int>(W, -1));
  if (d % 2) {
    for (int y = 0; y < H; y++)
      for (int x = 0; x < W; x++)
        ans[y][x] = (y + x) % 2;
  } else {
    for (int bx = 0; bx < W; bx += d) {
      for (int by = 0; by < H; by += d) { // RGRG
        int col = (by / d + bx / d) % 2;
        for (int dy = -(d - 2) / 2; dy <= (d - 2) / 2; dy++)
          for (int dx = abs(dy); dx < d - abs(dy); dx++) {
            if (0 <= by + dy && by + dy < H && 0 <= bx + dx && bx + dx < W)
              ans[by + dy][bx + dx] = col;
          }
      }
    }
    for (int bx = -(d / 2); bx < W + d / 2; bx += d) { // BYBY
      for (int by = (d / 2); by < H + d / 2; by += d) {
        int col = ((by + d / 2) / d + (bx + d / 2) / d) % 2 + 2;
        for (int dy = -(d - 2) / 2; dy <= (d - 2) / 2; dy++)
          for (int dx = abs(dy); dx < d - abs(dy); dx++) {
            if (0 <= by + dy && by + dy < H && 0 <= bx + dx && bx + dx < W)
              ans[by + dy][bx + dx] = col;
          }
      }
    }
  }
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++)
      cout << rgby(ans[y][x]);
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <assert.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

char rgby(int x) {
  if (x == 0)
    return 'R';
  if (x == 1)
    return 'G';
  if (x == 2)
    return 'B';
  if (x == 3)
    return 'Y';
  if (x == -1)
    assert(false);
}

int main() {
  ll H, W, d;
  cin >> H >> W >> d;
  vector<vector<int>> ans(H, vector<int>(W, -1));
  if (d % 2) {
    for (int y = 0; y < H; y++)
      for (int x = 0; x < W; x++)
        ans[y][x] = (y + x) % 2;
  } else {
    for (int bx = 0; bx < W + (d / 2); bx += d) {
      for (int by = 0; by < H + (d / 2); by += d) { // RGRG
        int col = (by / d + bx / d) % 2;
        for (int dy = -(d - 2) / 2; dy <= (d - 2) / 2; dy++)
          for (int dx = abs(dy); dx < d - abs(dy); dx++) {
            if (0 <= by + dy && by + dy < H && 0 <= bx + dx && bx + dx < W)
              ans[by + dy][bx + dx] = col;
          }
      }
    }
    for (int bx = -(d / 2); bx < W + d / 2; bx += d) { // BYBY
      for (int by = (d / 2); by < H + d / 2; by += d) {
        int col = ((by + d / 2) / d + (bx + d / 2) / d) % 2 + 2;
        for (int dy = -(d - 2) / 2; dy <= (d - 2) / 2; dy++)
          for (int dx = abs(dy); dx < d - abs(dy); dx++) {
            if (0 <= by + dy && by + dy < H && 0 <= bx + dx && bx + dx < W)
              ans[by + dy][bx + dx] = col;
          }
      }
    }
  }
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++)
      cout << rgby(ans[y][x]);
    cout << endl;
  }
  return 0;
}