Problem p03429 (C++)

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

typedef long long signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for (int i = (l); i < (r); i++)
#define incII(i, l, r) for (int i = (l); i <= (r); i++)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define decII(i, l, r) for (int i = (r); i >= (l); i--)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define FOR(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define RFOR(it, v) for (auto it = v.rbegin(); it != v.rend(); ++it)

template <typename T> bool setmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmineq(T &a, T b) {
  if (b <= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmaxeq(T &a, T b) {
  if (b >= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, m, a, b, r;
string s[1000];

int main() {
  cin >> n >> m >> a >> b;
  inc(i, n) { s[i] = string(m, '.'); }

  int aa = a, bb = b;

  int v = n * m - 2 * (a + b);
  int r = 0;

  if (n % 2 == 1) {
    r += max(m / 2 - a, 0) * 2;
  }
  if (m % 2 == 1) {
    r += max(n / 2 - b, 0) * 2;
  }
  r += n * m % 2;

  if (v < r) {
    cout << "NO" << endl;
  } else {
    if (n % 2 == 1) {
      inc(j, m / 2) {
        if (a > 0) {
          s[n - 1][j * 2 + 0] = '<';
          s[n - 1][j * 2 + 1] = '>';
          a--;
        }
      }
    }

    if (m % 2 == 1) {
      inc(i, n / 2) {
        if (b > 0) {
          int x = (n % 2 == 1 && i == n / 2 - 1 ? 1 : 0);
          s[i * 2 + 0 + x][m - 1] = '^';
          s[i * 2 + 1 + x][m - 1] = 'v';
          b--;
        }
      }
    }

    inc(i, n / 2) {
      inc(j, m / 2) {
        if (n * m % 2 == 1 && i == n / 2 - 1 && j == m / 2 - 1 && a > 0 &&
            b > 0) {
          if (a > 0) {
            s[i * 2 + 0][j * 2 + 0 + 1] = '<';
            s[i * 2 + 0][j * 2 + 1 + 1] = '>';
            a--;
          }
          if (b > 0) {
            s[i * 2 + 0][j * 2 + 0] = '^';
            s[i * 2 + 1][j * 2 + 0] = 'v';
            b--;
          }
        } else {
          if (a >= 2 || (a == 1 && n * m % 2 == 0)) {
            s[i * 2 + 0][j * 2 + 0] = '<';
            s[i * 2 + 0][j * 2 + 1] = '>';
            a--;
            if (a > 0) {
              s[i * 2 + 1][j * 2 + 0] = '<';
              s[i * 2 + 1][j * 2 + 1] = '>';
              a--;
            }
          } else if (b >= 2 || (b == 1 && n * m % 2 == 0)) {
            s[i * 2 + 0][j * 2 + 0] = '^';
            s[i * 2 + 1][j * 2 + 0] = 'v';
            b--;
            if (b > 0) {
              s[i * 2 + 0][j * 2 + 1] = '^';
              s[i * 2 + 1][j * 2 + 1] = 'v';
              b--;
            }
          }
        }
      }
    }

    int ca = 0, cb = 0;
    if (a == 0 && b == 0) {
      inc(i, n) {
        inc(j, m) {
          if (s[i][j] == '<') {
            assert(s[i][j + 1] == '>');
            ca++;
          }
          if (s[i][j] == '>') {
            assert(s[i][j - 1] == '<');
          }
          if (s[i][j] == '^') {
            assert(s[i + 1][j] == 'v');
            cb++;
          }
          if (s[i][j] == 'v') {
            assert(s[i - 1][j] == '^');
          }
        }
      }
      assert(ca == aa && cb == bb);

      cout << "YES" << endl;
      inc(i, n) { cout << s[i] << "\n"; }
    } else {
      assert(n * m % 2 == 0);
      cout << "NO" << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for (int i = (l); i < (r); i++)
#define incII(i, l, r) for (int i = (l); i <= (r); i++)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define decII(i, l, r) for (int i = (r); i >= (l); i--)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define FOR(it, v) for (auto it = v.begin(); it != v.end(); ++it)
#define RFOR(it, v) for (auto it = v.rbegin(); it != v.rend(); ++it)

template <typename T> bool setmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmineq(T &a, T b) {
  if (b <= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> bool setmaxeq(T &a, T b) {
  if (b >= a) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, m, a, b, r;
string s[1000];

int main() {
  cin >> n >> m >> a >> b;
  inc(i, n) { s[i] = string(m, '.'); }

  int aa = a, bb = b;

  int v = n * m - 2 * (a + b);
  int r = 0;

  if (n % 2 == 1) {
    r += max(m / 2 - a, 0) * 2;
  }
  if (m % 2 == 1) {
    r += max(n / 2 - b, 0) * 2;
  }
  r += n * m % 2;

  if (v < r) {
    cout << "NO" << endl;
  } else {
    if (n % 2 == 1) {
      inc(j, m / 2) {
        if (a > 0) {
          s[n - 1][j * 2 + 0] = '<';
          s[n - 1][j * 2 + 1] = '>';
          a--;
        }
      }
    }

    if (m % 2 == 1) {
      inc(i, n / 2) {
        if (b > 0) {
          int x = (n % 2 == 1 && i == n / 2 - 1 ? 1 : 0);
          s[i * 2 + 0 + x][m - 1] = '^';
          s[i * 2 + 1 + x][m - 1] = 'v';
          b--;
        }
      }
    }

    inc(i, n / 2) {
      inc(j, m / 2) {
        if (n * m % 2 == 1 && i == n / 2 - 1 && j == m / 2 - 1 &&
            !(a == 2 || b == 2)) {
          if (a > 0) {
            s[i * 2 + 0][j * 2 + 0 + 1] = '<';
            s[i * 2 + 0][j * 2 + 1 + 1] = '>';
            a--;
          }
          if (b > 0) {
            s[i * 2 + 0][j * 2 + 0] = '^';
            s[i * 2 + 1][j * 2 + 0] = 'v';
            b--;
          }
        } else {
          if (a >= 2 || (a == 1 && n * m % 2 == 0)) {
            s[i * 2 + 0][j * 2 + 0] = '<';
            s[i * 2 + 0][j * 2 + 1] = '>';
            a--;
            if (a > 0) {
              s[i * 2 + 1][j * 2 + 0] = '<';
              s[i * 2 + 1][j * 2 + 1] = '>';
              a--;
            }
          } else if (b >= 2 || (b == 1 && n * m % 2 == 0)) {
            s[i * 2 + 0][j * 2 + 0] = '^';
            s[i * 2 + 1][j * 2 + 0] = 'v';
            b--;
            if (b > 0) {
              s[i * 2 + 0][j * 2 + 1] = '^';
              s[i * 2 + 1][j * 2 + 1] = 'v';
              b--;
            }
          }
        }
      }
    }

    int ca = 0, cb = 0;
    if (a == 0 && b == 0) {
      inc(i, n) {
        inc(j, m) {
          if (s[i][j] == '<') {
            assert(s[i][j + 1] == '>');
            ca++;
          }
          if (s[i][j] == '>') {
            assert(s[i][j - 1] == '<');
          }
          if (s[i][j] == '^') {
            assert(s[i + 1][j] == 'v');
            cb++;
          }
          if (s[i][j] == 'v') {
            assert(s[i - 1][j] == '^');
          }
        }
      }
      assert(ca == aa && cb == bb);

      cout << "YES" << endl;
      inc(i, n) { cout << s[i] << "\n"; }
    } else {
      assert(n * m % 2 == 0);
      cout << "NO" << endl;
    }
  }

  return 0;
}