Problem p03429 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  Int h, w, a, b;
  cin >> h >> w >> a >> b;
  auto NO = []() {
    cout << "NO" << endl;
    exit(0);
  };

  if (h * w < 2 * (a + b))
    NO();
  vector<string> s(h, string(w, '.'));

  if (h == 1 && b > 0)
    NO();
  if (w == 1 && a > 0)
    NO();

  if (h & 1) {
    for (Int j = 0; j + 1 < w; j += 2) {
      if (a == 0)
        break;
      s[0][j] = '<';
      s[0][j + 1] = '>';
      a--;
    }
  }

  if (w & 1) {
    for (Int i = 0; i + 1 < h; i += 2) {
      if (b == 0)
        break;
      s[i][w - 1] = '^';
      s[i + 1][w - 1] = 'v';
      b--;
    }
  }

  if (!a && !b) {
    cout << "YES" << endl;
    for (Int i = 0; i < h; i++)
      cout << s[i] << endl;
    return 0;
  }

  if ((h & 1) && (w & 1)) {
    assert(h >= 3 && w >= 3);
    assert(a >= 0 && b >= 0);
    if (a & 1) {
      s[h - 1][w - 2] = '<';
      s[h - 1][w - 1] = '>';
      a--;
    }
    if (b & 1) {
      s[h - 2][w - 3] = '^';
      s[h - 1][w - 3] = 'v';
      b--;
    }
  }

  for (Int i = (h & 1); i + 1 < h; i += 2) {
    for (Int j = 0; j + 1 < w; j += 2) {
      if (a > 0 || b > 0) {
        assert(s[i][j] == '.');
        assert(s[i][j + 1] == '.');
        assert(s[i + 1][j] == '.');
        assert(s[i + 1][j + 1] == '.');
      }
      if (a > 0) {
        if (a > 0) {
          s[i][j] = '<';
          s[i][j + 1] = '>';
          a--;
        }
        if (a > 0) {
          s[i + 1][j] = '<';
          s[i + 1][j + 1] = '>';
          a--;
        }
      } else if (b > 0) {
        if (b > 0) {
          s[i][j] = '^';
          s[i + 1][j] = 'v';
          b--;
        }
        if (b > 0) {
          s[i][j + 1] = '^';
          s[i + 1][j + 1] = 'v';
          b--;
        }
      }
    }
  }
  if (a || b)
    NO();
  cout << "YES" << endl;
  for (Int i = 0; i < h; i++)
    cout << s[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  Int h, w, a, b;
  cin >> h >> w >> a >> b;
  auto NO = []() {
    cout << "NO" << endl;
    exit(0);
  };

  if (h * w < 2 * (a + b))
    NO();
  vector<string> s(h, string(w, '.'));

  if (h == 1 && b > 0)
    NO();
  if (w == 1 && a > 0)
    NO();

  if (h & 1) {
    for (Int j = 0; j + 1 < w; j += 2) {
      if (a == 0)
        break;
      s[0][j] = '<';
      s[0][j + 1] = '>';
      a--;
    }
  }

  if (w & 1) {
    for (Int i = 0; i + 1 < h; i += 2) {
      if (b == 0)
        break;
      s[i][w - 1] = '^';
      s[i + 1][w - 1] = 'v';
      b--;
    }
  }

  if (!a && !b) {
    cout << "YES" << endl;
    for (Int i = 0; i < h; i++)
      cout << s[i] << endl;
    return 0;
  }

  if ((h & 1) && (w & 1)) {
    assert(h >= 3 && w >= 3);
    assert(a >= 0 && b >= 0);
    if (a & 1) {
      s[h - 1][w - 2] = '<';
      s[h - 1][w - 1] = '>';
      a--;
    }
    if (b & 1) {
      s[h - 2][w - 3] = '^';
      s[h - 1][w - 3] = 'v';
      b--;
    }
  }

  for (Int i = (h & 1); i + 1 < h; i += 2) {
    for (Int j = 0; j + 1 < w; j += 2) {
      if (a > 0 || b > 0) {
        if (s[i][j] != '.')
          NO();
        if (s[i][j + 1] != '.')
          NO();
        if (s[i + 1][j] != '.')
          NO();
        if (s[i + 1][j + 1] != '.')
          NO();
      }
      if (a > 0) {
        if (a > 0) {
          s[i][j] = '<';
          s[i][j + 1] = '>';
          a--;
        }
        if (a > 0) {
          s[i + 1][j] = '<';
          s[i + 1][j + 1] = '>';
          a--;
        }
      } else if (b > 0) {
        if (b > 0) {
          s[i][j] = '^';
          s[i + 1][j] = 'v';
          b--;
        }
        if (b > 0) {
          s[i][j + 1] = '^';
          s[i + 1][j + 1] = 'v';
          b--;
        }
      }
    }
  }
  if (a || b)
    NO();
  cout << "YES" << endl;
  for (Int i = 0; i < h; i++)
    cout << s[i] << endl;
  return 0;
}