Problem p03429 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \
  do {                                                                         \
    cerr << __LINE__ << ": ";                                                  \
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

void quit(const vector<string> &v) {
  cout << (v.size() ? "YES" : "NO") << endl;
  for (auto i : v)
    cout << i << endl;
  exit(0);
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int h, w, yoko, tate;
  cin >> h >> w >> yoko >> tate;
  assert(h % 2 + w % 2);

  vector<string> ans(h, string(w, '.'));
  auto put = [&](int y, int x, int is_tate, int two) {
    rep(i, two + 1) {
      if (is_tate) {
        ans[y][x + i] = '^';
        ans[y + 1][x + i] = 'v';
        tate--;
      } else {
        ans[y + i][x] = '<';
        ans[y + i][x + 1] = '>';
        yoko--;
      }
    }
  };

  if (h % 2) {
    int k = min(yoko, w / 2);
    rep(i, k) put(h - 1, 2 * i, 0, 0);
  }
  if (w % 2) {
    int k = min(tate, h / 2);
    rep(i, k) put(h - 2 - 2 * i, w - 1, 1, 0);
  }

  // for(auto i : ans) dbg(i); dbg(tate, yoko);

  vector<pi> normal, extended;
  for (int i = 0; i + 1 < h; i += 2)
    for (int j = 0; j + 1 < w; j += 2) {
      if (i + 3 == h && (ans[i + 2][j] == '.' || ans[i + 2][j + 1] == '.') ||
          j + 3 == w && (ans[i][j + 2] == '.' || ans[i + 1][j + 2] == '.'))
        extended.emplace_back(i, j);
      else
        normal.emplace_back(i, j);
    }

  // dbg(normal, extended);

  for (pi p : normal) {
    if (tate > 1)
      put(p.first, p.second, 1, tate > 1);
    else if (yoko > 1)
      put(p.first, p.second, 0, yoko > 1);
    else if (tate)
      put(p.first, p.second, 1, tate > 1);
    else if (yoko)
      put(p.first, p.second, 0, yoko > 1);
  }
  for (pi p : extended) {
    if (tate >= yoko && tate > 1 || tate + yoko == 1 && tate)
      put(p.first, p.second, 1, tate > 1);
    else if (yoko >= tate && yoko > 1 || tate + yoko == 1 && yoko)
      put(p.first, p.second, 0, yoko > 1);
    else {
      if (tate && p.first + 3 == h &&
          (ans[p.first + 2][p.second] == '.' ||
           ans[p.first + 2][p.second + 1] == '.')) {
        rep(i, 2) if (ans[p.first + 2][p.second + i] == '.') {
          put(p.first + 1, p.second + i, 1, 0);
          break;
        }
        if (yoko)
          put(p.first, p.second, 0, 0);
      } else if (yoko && p.second + 3 == w &&
                 (ans[p.first][p.second + 2] == '.' ||
                  ans[p.first + 1][p.second + 2] == '.')) {
        rep(i, 2) if (ans[p.first + i][p.second + 2] == '.') {
          put(p.first + i, p.second + 1, 0, 0);
          break;
        }
        if (tate)
          put(p.first, p.second, 1, 0);
      }
    }
  }
  if (tate + yoko)
    quit(vector<string>());
  else
    quit(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...)                                                               \
  do {                                                                         \
    cerr << __LINE__ << ": ";                                                  \
    dbgprint(#__VA_ARGS__, __VA_ARGS__);                                       \
  } while (0);

using namespace std;

namespace std {
template <class S, class T> struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T> struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v)
      h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v)
    os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v)
    os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

void quit(const vector<string> &v) {
  cout << (v.size() ? "YES" : "NO") << endl;
  for (auto i : v)
    cout << i << endl;
  exit(0);
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int h, w, yoko, tate;
  cin >> h >> w >> yoko >> tate;

  vector<string> ans(h, string(w, '.'));
  auto put = [&](int y, int x, int is_tate, int two) {
    rep(i, two + 1) {
      if (is_tate) {
        ans[y][x + i] = '^';
        ans[y + 1][x + i] = 'v';
        tate--;
      } else {
        ans[y + i][x] = '<';
        ans[y + i][x + 1] = '>';
        yoko--;
      }
    }
  };

  if (h % 2) {
    int k = min(yoko, w / 2);
    rep(i, k) put(h - 1, 2 * i, 0, 0);
  }
  if (w % 2) {
    int k = min(tate, h / 2);
    rep(i, k) put(h - 2 - 2 * i, w - 1, 1, 0);
  }

  // for(auto i : ans) dbg(i); dbg(tate, yoko);

  vector<pi> normal, extended;
  for (int i = 0; i + 1 < h; i += 2)
    for (int j = 0; j + 1 < w; j += 2) {
      if (i + 3 == h && (ans[i + 2][j] == '.' || ans[i + 2][j + 1] == '.') ||
          j + 3 == w && (ans[i][j + 2] == '.' || ans[i + 1][j + 2] == '.'))
        extended.emplace_back(i, j);
      else
        normal.emplace_back(i, j);
    }

  // dbg(normal, extended);

  for (pi p : normal) {
    if (tate > 1)
      put(p.first, p.second, 1, tate > 1);
    else if (yoko > 1)
      put(p.first, p.second, 0, yoko > 1);
    else if (tate)
      put(p.first, p.second, 1, tate > 1);
    else if (yoko)
      put(p.first, p.second, 0, yoko > 1);
  }
  for (pi p : extended) {
    if (tate >= yoko && tate > 1 || tate + yoko == 1 && tate)
      put(p.first, p.second, 1, tate > 1);
    else if (yoko >= tate && yoko > 1 || tate + yoko == 1 && yoko)
      put(p.first, p.second, 0, yoko > 1);
    else {
      if (tate && p.first + 3 == h &&
          (ans[p.first + 2][p.second] == '.' ||
           ans[p.first + 2][p.second + 1] == '.')) {
        rep(i, 2) if (ans[p.first + 2][p.second + i] == '.') {
          put(p.first + 1, p.second + i, 1, 0);
          break;
        }
        if (yoko)
          put(p.first, p.second, 0, 0);
      } else if (yoko && p.second + 3 == w &&
                 (ans[p.first][p.second + 2] == '.' ||
                  ans[p.first + 1][p.second + 2] == '.')) {
        rep(i, 2) if (ans[p.first + i][p.second + 2] == '.') {
          put(p.first + i, p.second + 1, 0, 0);
          break;
        }
        if (tate)
          put(p.first, p.second, 1, 0);
      }
    }
  }
  if (tate + yoko)
    quit(vector<string>());
  else
    quit(ans);
  return 0;
}