Problem p03429 (C++)

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

long long N, M, n, m, A, B, a[1001][1001], l, r, i, j;
string s = ".<>^v";

int main() {
  scanf("%lld%lld%lld%lld", &N, &M, &A, &B);
  n = N;
  m = M;
  n & 1 && --n;
  m & 1 && --m;
  if (N * M == 2 * (A + B) + 1) {
    for (i = 0; i < m - 2 && A > 0; i += 2) {
      --A;
      a[n][i] = 1;
      a[n][i + 1] = 2;
    }
    for (i = 0; i < n - 2 && B > 0; i += 2) {
      --B;
      a[i][m] = 3;
      a[i + 1][m] = 4;
    }
    [&] {
      for (i = 0; i < n; i += 2) {
        for (j = 0; j < m; j += 2) {
          if (i >= n - 1 && j >= m - 1)
            return 0;
          if (A > 2) {
            a[i][j] = 1;
            a[i][j + 1] = 2;
            --A;
            if (A <= 2)
              continue;
            a[i + 1][j] = 1;
            a[i + 1][j + 1] = 2;
            --A;
            if (A <= 2)
              continue;
          } else if (B > 2) {
            a[i][j] = 3;
            a[i + 1][j] = 4;
            --B;
            if (B <= 2)
              return 0;
            a[i][j + 1] = 3;
            a[i + 1][j + 1] = 4;
            --B;
            if (B <= 2)
              return 0;
          } else
            return 0;
        }
      }
      return 1;
    }();
    A -= 2;
    B -= 2;
    a[n - 2][m - 2] = a[n][m - 1] = 1;
    a[n - 2][m - 1] = a[n][m] = 2;
    a[n - 2][m] = a[n - 1][m - 2] = 3;
    a[n - 1][m] = a[n][m - 2] = 4;
  } else {
    if (N & 1) {
      for (i = 0; i < m && A > 0; i += 2) {
        --A;
        a[n][i] = 1;
        a[n][i + 1] = 2;
      }
    }
    if (M & 1) {
      for (i = 0; i < n && B > 0; i += 2) {
        --B;
        a[i][m] = 3;
        a[i + 1][m] = 4;
      }
    }
    [&] {
      for (i = 0; i < n; i += 2) {
        for (j = 0; j < m; j += 2) {
          if (A > 0) {
            a[i][j] = 1;
            a[i][j + 1] = 2;
            --A;
            if (A <= 0)
              continue;
            a[i + 1][j] = 1;
            a[i + 1][j + 1] = 2;
            --A;
            if (A <= 0)
              continue;
          } else if (B > 0) {
            a[i][j] = 3;
            a[i + 1][j] = 4;
            --B;
            if (B <= 0)
              return 0;
            a[i][j + 1] = 3;
            a[i + 1][j + 1] = 4;
            --B;
            if (B <= 0)
              return 0;
          } else
            return 0;
        }
      }
      return 1;
    }();
  }
  if (A || B)
    return 0 & puts("NO");
  puts("YES");
  for (int i = 0; i < N; ++i, puts(""))
    for (int j = 0; j < M; ++j)
      printf("%c", s[a[i][j]]);
  return 0;
}

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

long long N, M, n, m, A, B, a[1001][1001], l, r, i, j;
string s = ".<>^v";

int main() {
  scanf("%lld%lld%lld%lld", &N, &M, &A, &B);
  n = N;
  m = M;
  n & 1 && --n;
  m & 1 && --m;
  if (N * M == 2 * (A + B) + 1 && ((A - m / 2) & (B - n / 2)) & 1) {
    for (i = 0; i < m - 2 && A > 0; i += 2) {
      --A;
      a[n][i] = 1;
      a[n][i + 1] = 2;
    }
    for (i = 0; i < n - 2 && B > 0; i += 2) {
      --B;
      a[i][m] = 3;
      a[i + 1][m] = 4;
    }
    [&] {
      for (i = 0; i < n; i += 2) {
        for (j = 0; j < m; j += 2) {
          if (i >= n - 1 && j >= m - 1)
            return 0;
          if (A > 2) {
            a[i][j] = 1;
            a[i][j + 1] = 2;
            --A;
            if (A <= 2)
              continue;
            a[i + 1][j] = 1;
            a[i + 1][j + 1] = 2;
            --A;
            if (A <= 2)
              continue;
          } else if (B > 2) {
            a[i][j] = 3;
            a[i + 1][j] = 4;
            --B;
            if (B <= 2)
              return 0;
            a[i][j + 1] = 3;
            a[i + 1][j + 1] = 4;
            --B;
            if (B <= 2)
              return 0;
          } else
            return 0;
        }
      }
      return 1;
    }();
    A -= 2;
    B -= 2;
    a[n - 2][m - 2] = a[n][m - 1] = 1;
    a[n - 2][m - 1] = a[n][m] = 2;
    a[n - 2][m] = a[n - 1][m - 2] = 3;
    a[n - 1][m] = a[n][m - 2] = 4;
  } else {
    if (N & 1) {
      for (i = 0; i < m && A > 0; i += 2) {
        --A;
        a[n][i] = 1;
        a[n][i + 1] = 2;
      }
    }
    if (M & 1) {
      for (i = 0; i < n && B > 0; i += 2) {
        --B;
        a[i][m] = 3;
        a[i + 1][m] = 4;
      }
    }
    [&] {
      for (i = 0; i < n; i += 2) {
        for (j = 0; j < m; j += 2) {
          if (A > 0) {
            a[i][j] = 1;
            a[i][j + 1] = 2;
            --A;
            if (A <= 0)
              continue;
            a[i + 1][j] = 1;
            a[i + 1][j + 1] = 2;
            --A;
            if (A <= 0)
              continue;
          } else if (B > 0) {
            a[i][j] = 3;
            a[i + 1][j] = 4;
            --B;
            if (B <= 0)
              return 0;
            a[i][j + 1] = 3;
            a[i + 1][j + 1] = 4;
            --B;
            if (B <= 0)
              return 0;
          } else
            return 0;
        }
      }
      return 1;
    }();
  }
  if (A || B)
    return 0 & puts("NO");
  puts("YES");
  for (int i = 0; i < N; ++i, puts(""))
    for (int j = 0; j < M; ++j)
      printf("%c", s[a[i][j]]);
  return 0;
}