Problem p03429 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int A, B, N, M;
string res[1001];

void putA(ll i, ll j) {
  res[i][j] = '<';
  res[i][j + 1] = '>';
  A--;
}
void putB(ll i, ll j) {
  res[i][j] = '^';
  res[i + 1][j] = 'v';
  B--;
}

int main() {
  cin >> N >> M >> A >> B;
  int area = N * M, tot = 2 * (A + B);
  if (area < tot) {
    cout << "NO" << endl;
    return 0;
  }
  rep(i, N) rep(j, M) res[i] += ".";
  if (N % 2 == 0 && M % 2 == 0) {
    for (ll i = 0; i < N; i += 2)
      for (ll j = 0; j < M; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }
  } else if (M % 2 == 0) {
    for (ll j = 0; j < M; j += 2) {
      if (A > 0) {
        putA(N - 1, j);
      }
    }
    for (ll i = 0; i < N - 1; i += 2)
      for (ll j = 0; j < M; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }
  } else if (N % 2 == 0) {
    for (ll i = 0; i < N; i += 2) {
      if (B > 0) {
        putB(i, M - 1);
      }
    }
    for (ll i = 0; i < N; i += 2)
      for (ll j = 0; j < M - 1; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }
  } else {
    ll preA = A, preB = B;
    for (ll j = 0; j < M - 1; j += 2) {
      if (A > 0) {
        putA(N - 1, j);
      }
    }
    for (ll i = 0; i < N - 1; i += 2) {
      if (B > 0) {
        putB(i, M - 1);
      }
    }
    for (ll i = 0; i < N - 1; i += 2)
      for (ll j = 0; j < M - 1; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }

    if (A > 0 || B > 0) {
      rep(i, N) res[i] = "";
      rep(i, N) rep(j, M) res[i] += ".";

      A = preA;
      B = preB;
      assert(A >= 2 && B >= 2);
      putA(N - 3, M - 3);
      putB(N - 3, M - 1);
      putA(N - 1, M - 2);
      putB(N - 2, M - 3);
      for (ll j = 0; j < M - 3; j += 2) {
        if (A > 0) {
          putA(N - 1, j);
        }
      }
      for (ll i = 0; i < N - 3; i += 2) {
        if (B > 0) {
          putB(i, M - 1);
        }
      }
      for (ll i = 0; i < N - 1; i += 2)
        for (ll j = 0; j < M - 1; j += 2) {
          if (i == N - 3 && j == M - 3)
            continue;

          if (A > 0) {
            putA(i, j);
            if (A > 0)
              putA(i + 1, j);
          } else if (B > 0) {
            putB(i, j);
            if (B > 0)
              putB(i, j + 1);
          }
        }
    }
  }
  if (A > 0 || B > 0) {
    cout << "NO" << endl;
    return 0;
  }
  cout << "YES" << endl;
  rep(i, N) { cout << res[i] << endl; }
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int A, B, N, M;
string res[1001];

void putA(ll i, ll j) {
  res[i][j] = '<';
  res[i][j + 1] = '>';
  A--;
}
void putB(ll i, ll j) {
  res[i][j] = '^';
  res[i + 1][j] = 'v';
  B--;
}

int main() {
  cin >> N >> M >> A >> B;
  int area = N * M, tot = 2 * (A + B);
  if (area < tot) {
    cout << "NO" << endl;
    return 0;
  }
  rep(i, N) rep(j, M) res[i] += ".";
  if (N % 2 == 0 && M % 2 == 0) {
    for (ll i = 0; i < N; i += 2)
      for (ll j = 0; j < M; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }
  } else if (M % 2 == 0) {
    for (ll j = 0; j < M; j += 2) {
      if (A > 0) {
        putA(N - 1, j);
      }
    }
    for (ll i = 0; i < N - 1; i += 2)
      for (ll j = 0; j < M; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }
  } else if (N % 2 == 0) {
    for (ll i = 0; i < N; i += 2) {
      if (B > 0) {
        putB(i, M - 1);
      }
    }
    for (ll i = 0; i < N; i += 2)
      for (ll j = 0; j < M - 1; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }
  } else {
    ll preA = A, preB = B;
    for (ll j = 0; j < M - 1; j += 2) {
      if (A > 0) {
        putA(N - 1, j);
      }
    }
    for (ll i = 0; i < N - 1; i += 2) {
      if (B > 0) {
        putB(i, M - 1);
      }
    }
    for (ll i = 0; i < N - 1; i += 2)
      for (ll j = 0; j < M - 1; j += 2) {
        if (A > 0) {
          putA(i, j);
          if (A > 0)
            putA(i + 1, j);
        } else if (B > 0) {
          putB(i, j);
          if (B > 0)
            putB(i, j + 1);
        }
      }

    if (A > 0 || B > 0) {
      rep(i, N) res[i] = "";
      rep(i, N) rep(j, M) res[i] += ".";

      A = preA;
      B = preB;
      // assert(A>=2&&B>=2);
      if (A > 0)
        putA(N - 3, M - 3);
      if (B > 0)
        putB(N - 3, M - 1);
      if (A > 0)
        putA(N - 1, M - 2);
      if (B > 0)
        putB(N - 2, M - 3);
      for (ll j = 0; j < M - 3; j += 2) {
        if (A > 0) {
          putA(N - 1, j);
        }
      }
      for (ll i = 0; i < N - 3; i += 2) {
        if (B > 0) {
          putB(i, M - 1);
        }
      }
      for (ll i = 0; i < N - 1; i += 2)
        for (ll j = 0; j < M - 1; j += 2) {
          if (i == N - 3 && j == M - 3)
            continue;

          if (A > 0) {
            putA(i, j);
            if (A > 0)
              putA(i + 1, j);
          } else if (B > 0) {
            putB(i, j);
            if (B > 0)
              putB(i, j + 1);
          }
        }
    }
  }
  if (A > 0 || B > 0) {
    cout << "NO" << endl;
    return 0;
  }
  cout << "YES" << endl;
  rep(i, N) { cout << res[i] << endl; }
  return 0;
}