Problem p03429 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)

template <typename T> void debug_out(T t) { cerr << t; }

template <typename A, typename B> void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}

template <typename T> void debug_out(vector<T> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1)
      cerr << ", ";
  }
}

template <typename T> void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1)
      cerr << endl;
  }
}

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  debug_out(H);
  cerr << " ";
  debug_out(T...);
}

template <typename T> void debug_out(set<T> a) {
  vector<T> _a;
  for (T t : a)
    _a.push_back(t);
  debug_out(_a);
}

#define debug(...)                                                             \
  cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__),         \
      cerr << endl;

int n, m, a, b;

int main() {
  cin >> n >> m >> a >> b;
  if ((a + b) * 2 > m * n) {
    cout << "NO";
    return 0;
  }
  int r = 0, c = 0;
  vector<vector<char>> res(n, vector<char>(m, '.'));
  range(i, 0, n) {
    for (int j = 0; j < m - 1; j += 2) {
      res[i][j] = '<';
      res[i][j + 1] = '>';
      r++;
    }
  }
  for (int i = 0; i < n - 1; i += 2) {
    if (res[i][m - 1] == '.') {
      res[i][m - 1] = '^';
      res[i + 1][m - 1] = 'v';
      c++;
    }
  }
  if (res[n - 1][m - 1] == '.') {
    int x = n - 1, y = m - 1;
    while (r < a || c < b) {
      if (x == n - 1) {
        if (y >= 2) {
          swap(res[x][y], res[x][y - 1]);
          swap(res[x][y - 1], res[x][y - 2]);
          y -= 2;
          while (x) {
            int sx = x - 1, sy;
            if (res[x - 1][y] == '<')
              sy = y + 1;
            else
              sy = y - 1;
            res[x - 1][y] = '^';
            res[x][y] = 'v';
            res[sx][sy] = '.';
            x = sx;
            y = sy;
            r--;
            c++;
            if (r >= a && c >= b)
              break;
          }
        } else {
          cout << "NO";
          return 0;
        }
      } else {
        if (y >= 2) {
          swap(res[x][y], res[x][y - 1]);
          swap(res[x][y - 1], res[x][y - 2]);
          y -= 2;
          while (x < n - 1) {
            int sx = x + 1, sy;
            if (res[x + 1][y] == '<')
              sy = y - 1;
            else
              sy = y - 1;
            res[x + 1][y] = 'v';
            res[x][y] = '^';
            res[sx][sy] = '.';
            x = sx;
            y = sy;
            r--;
            c++;
            if (r >= a && c >= b)
              break;
          }
        } else {
          cout << "NO";
          return 0;
        }
      }
    }
  }
  while (r < a || c < b) {
    bool change = false;
    for (int i = 0; i < n - 1; i += 2) {
      for (int j = 0; j < m - 1; j += 2) {
        if (res[i][j] == '<' && res[i][j + 1] == '>' && res[i + 1][j] == '<' &&
            res[i + 1][j + 1] == '>') {
          res[i][j] = '^';
          res[i][j + 1] = '^';
          res[i + 1][j] = 'v';
          res[i + 1][j + 1] = 'v';
          r -= 2;
          c += 2;
          change = true;
          if (r >= a && c >= b)
            break;
        }
      }
      if (r >= a && c >= b)
        break;
    }
    if (!change) {
      cout << "NO";
      return 0;
    }
  }
  while (r > a) {
    range(i, 0, n) {
      range(j, 0, m - 1) {
        if (res[i][j] == '<' && res[i][j + 1] == '>') {
          res[i][j] = '.';
          res[i][j + 1] = '.';
          r--;
          if (r == a)
            break;
        }
      }
      if (r == a)
        break;
    }
  }
  while (c > b) {
    range(i, 0, n - 1) {
      range(j, 0, m) {
        if (res[i][j] == '^' && res[i + 1][j] == 'v') {
          res[i][j] = '.';
          res[i + 1][j] = '.';
          c--;
          if (c == b)
            break;
        }
      }
      if (c == b)
        break;
    }
  }
  cout << "YES\n";
  range(i, 0, n) {
    range(j, 0, m) cout << res[i][j];
    cout << endl;
  }
  return 0;
}


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

using namespace std;

#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)

template <typename T> void debug_out(T t) { cerr << t; }

template <typename A, typename B> void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}

template <typename T> void debug_out(vector<T> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1)
      cerr << ", ";
  }
}

template <typename T> void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1)
      cerr << endl;
  }
}

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  debug_out(H);
  cerr << " ";
  debug_out(T...);
}

template <typename T> void debug_out(set<T> a) {
  vector<T> _a;
  for (T t : a)
    _a.push_back(t);
  debug_out(_a);
}

#define debug(...)                                                             \
  cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__),         \
      cerr << endl;

int n, m, a, b;

int main() {
  cin >> n >> m >> a >> b;
  if ((a + b) * 2 > m * n) {
    cout << "NO";
    return 0;
  }
  int r = 0, c = 0;
  vector<vector<char>> res(n, vector<char>(m, '.'));
  range(i, 0, n) {
    for (int j = 0; j < m - 1; j += 2) {
      res[i][j] = '<';
      res[i][j + 1] = '>';
      r++;
    }
  }
  for (int i = 0; i < n - 1; i += 2) {
    if (res[i][m - 1] == '.') {
      res[i][m - 1] = '^';
      res[i + 1][m - 1] = 'v';
      c++;
    }
  }
  if (res[n - 1][m - 1] == '.') {
    int x = n - 1, y = m - 1;
    while (r < a || c < b) {
      if (x == n - 1) {
        if (y >= 2) {
          swap(res[x][y], res[x][y - 1]);
          swap(res[x][y - 1], res[x][y - 2]);
          y -= 2;
          while (x) {
            int sx = x - 1, sy;
            if (res[x - 1][y] == '<')
              sy = y + 1;
            else
              sy = y - 1;
            res[x - 1][y] = '^';
            res[x][y] = 'v';
            res[sx][sy] = '.';
            x = sx;
            y = sy;
            r--;
            c++;
            if (r >= a && c >= b)
              break;
          }
        } else {
          cout << "NO";
          return 0;
        }
      } else {
        if (y >= 2) {
          swap(res[x][y], res[x][y - 1]);
          swap(res[x][y - 1], res[x][y - 2]);
          y -= 2;
          while (x < n - 1) {
            int sx = x + 1, sy;
            if (res[x + 1][y] == '<')
              sy = y + 1;
            else
              sy = y - 1;
            res[x + 1][y] = 'v';
            res[x][y] = '^';
            res[sx][sy] = '.';
            x = sx;
            y = sy;
            r--;
            c++;
            if (r >= a && c >= b)
              break;
          }
        } else {
          cout << "NO";
          return 0;
        }
      }
    }
  }
  while (r < a || c < b) {
    bool change = false;
    for (int i = 0; i < n - 1; i += 2) {
      for (int j = 0; j < m - 1; j += 2) {
        if (res[i][j] == '<' && res[i][j + 1] == '>' && res[i + 1][j] == '<' &&
            res[i + 1][j + 1] == '>') {
          res[i][j] = '^';
          res[i][j + 1] = '^';
          res[i + 1][j] = 'v';
          res[i + 1][j + 1] = 'v';
          r -= 2;
          c += 2;
          change = true;
          if (r >= a && c >= b)
            break;
        }
      }
      if (r >= a && c >= b)
        break;
    }
    if (!change) {
      cout << "NO";
      return 0;
    }
  }
  while (r > a) {
    range(i, 0, n) {
      range(j, 0, m - 1) {
        if (res[i][j] == '<' && res[i][j + 1] == '>') {
          res[i][j] = '.';
          res[i][j + 1] = '.';
          r--;
          if (r == a)
            break;
        }
      }
      if (r == a)
        break;
    }
  }
  while (c > b) {
    range(i, 0, n - 1) {
      range(j, 0, m) {
        if (res[i][j] == '^' && res[i + 1][j] == 'v') {
          res[i][j] = '.';
          res[i + 1][j] = '.';
          c--;
          if (c == b)
            break;
        }
      }
      if (c == b)
        break;
    }
  }
  cout << "YES\n";
  range(i, 0, n) {
    range(j, 0, m) cout << res[i][j];
    cout << endl;
  }
  return 0;
}