Problem p03429 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
int row, col, __, II;
char s[1111][1111];
void draw_(int x, int y) {
  s[x][y] = '<';
  s[x][y + 1] = '>';
}
void drawI(int x, int y) {
  s[x][y] = '^';
  s[x + 1][y] = 'v';
}
void draw31(int x, int y) {
  for (int i = 0; i < 3; i++) {
    if (__ > 0) {
      __--;
      draw_(x + i, y);
    }
  }
  if (II > 0) {
    II--;
    drawI(x, y + 2);
  }
}
void draw13(int x, int y) {
  for (int i = 0; i < 3; i++) {
    if (II > 0) {
      II--;
      drawI(x, y + i);
    }
  }
  if (__ > 0) {
    __--;
    draw_(x + 2, y);
  }
}
void draw22(int x, int y) {
  for (int i = 0; i < 2; i++) {
    if (__ > 0) {
      __--;
      draw_(x + i * 2, y + i);
    }
  }
  for (int i = 0; i < 2; i++) {
    if (II > 0) {
      II--;
      drawI(x + 1 - i, y + 2 * i);
    }
  }
}

#define vec vector<pair<int, int>>
vec v22, v_, vI, v33;
bool chk(int __, int II) {
  __ = max(__, 0);
  II = max(II, 0);
  return v22.size() >= (__ + 1) / 2 + (II + 1) / 2;
}
void draw() {
  for (int i = 0; i < v22.size(); i++) {
    if (__ > 0) {
      __--;
      draw_(v22[i].first, v22[i].second);
      if (__ > 0) {
        __--;
        draw_(v22[i].first + 1, v22[i].second);
      }
    } else if (II > 0) {
      II--;
      drawI(v22[i].first, v22[i].second);
      if (II > 0) {
        II--;
        drawI(v22[i].first, v22[i].second + 1);
      }
    }
  }
}
void cut(int row, int col) {
  int R = row;
  if (R % 2 == 1)
    R--;
  int C = col;
  if (C % 2 == 1)
    C--;
  for (int i = 0; i < R / 2; i++)
    for (int j = 0; j < C / 2; j++)
      v22.push_back(make_pair(i * 2, j * 2));
  if (row % 2 == 1) {
    for (int j = 0; j < col / 2; j++)
      v_.push_back(make_pair(row - 1, j * 2));
  }
  if (col % 2 == 1) {
    for (int j = 0; j < row / 2; j++)
      vI.push_back(make_pair(j * 2, col - 1));
  }
  if (row % 2 == 1 && col % 2 == 1) {
    v33.push_back(v22.back());
    v22.pop_back();
    v_.pop_back();
    vI.pop_back();
  }
}
bool proc() {
  if ((__ + II) * 2 > row * col)
    return false;
  cut(row, col);

  int tmp = min(__, (int)v_.size());
  __ -= tmp;
  for (int i = 0; i < tmp; i++)
    draw_(v_[i].first, v_[i].second);

  tmp = min(II, (int)vI.size());
  II -= tmp;
  for (int i = 0; i < tmp; i++)
    drawI(vI[i].first, vI[i].second);

  if (!v33.empty()) {
    if (chk(__ - 3, II - 1)) {
      draw31(v33[0].first, v33[0].second);
    } else if (chk(__ - 2, II - 2)) {
      draw22(v33[0].first, v33[0].second);
    } else if (chk(__ - 1, II - 3)) {
      draw13(v33[0].first, v33[0].second);
    } else
      return false;
  }
  if (chk(__, II)) {
    draw();
    return true;
  } else
    return false;
}
int main() {
  scanf("%d%d%d%d", &row, &col, &__, &II);
  for (int i = 0; i < row; i++)
    for (int j = 0; j < col; j++)
      s[i][j] = '.';
  if (!proc()) {
    puts("NO");
  } else {
    puts("YES");
    for (int i = 0; i < row; i++)
      puts(s[i]);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
int row, col, __, II;
char s[1111][1111];
void draw_(int x, int y) {
  s[x][y] = '<';
  s[x][y + 1] = '>';
}
void drawI(int x, int y) {
  s[x][y] = '^';
  s[x + 1][y] = 'v';
}
void draw31(int x, int y) {
  for (int i = 0; i < 3; i++) {
    if (__ > 0) {
      __--;
      draw_(x + i, y);
    }
  }
  if (II > 0) {
    II--;
    drawI(x, y + 2);
  }
}
void draw13(int x, int y) {
  for (int i = 0; i < 3; i++) {
    if (II > 0) {
      II--;
      drawI(x, y + i);
    }
  }
  if (__ > 0) {
    __--;
    draw_(x + 2, y);
  }
}
void draw22(int x, int y) {
  for (int i = 0; i < 2; i++) {
    if (__ > 0) {
      __--;
      draw_(x + i * 2, y + i);
    }
  }
  for (int i = 0; i < 2; i++) {
    if (II > 0) {
      II--;
      drawI(x + 1 - i, y + 2 * i);
    }
  }
}

#define vec vector<pair<int, int>>
vec v22, v_, vI, v33;
bool chk(int __, int II) {
  __ = max(__, 0);
  II = max(II, 0);
  return v22.size() >= (__ + 1) / 2 + (II + 1) / 2;
}
void draw() {
  for (int i = 0; i < v22.size(); i++) {
    if (__ > 0) {
      __--;
      draw_(v22[i].first, v22[i].second);
      if (__ > 0) {
        __--;
        draw_(v22[i].first + 1, v22[i].second);
      }
    } else if (II > 0) {
      II--;
      drawI(v22[i].first, v22[i].second);
      if (II > 0) {
        II--;
        drawI(v22[i].first, v22[i].second + 1);
      }
    }
  }
}
void cut(int row, int col) {
  int R = row;
  if (R % 2 == 1)
    R--;
  int C = col;
  if (C % 2 == 1)
    C--;
  for (int i = 0; i < R / 2; i++)
    for (int j = 0; j < C / 2; j++)
      v22.push_back(make_pair(i * 2, j * 2));
  if (row % 2 == 1) {
    for (int j = 0; j < col / 2; j++)
      v_.push_back(make_pair(row - 1, j * 2));
  }
  if (col % 2 == 1) {
    for (int j = 0; j < row / 2; j++)
      vI.push_back(make_pair(j * 2, col - 1));
  }
  if (row % 2 == 1 && col % 2 == 1 && row > 1 && col > 1) {
    v33.push_back(v22.back());
    v22.pop_back();
    v_.pop_back();
    vI.pop_back();
  }
}
bool proc() {
  if ((__ + II) * 2 > row * col)
    return false;
  cut(row, col);

  int tmp = min(__, (int)v_.size());
  __ -= tmp;
  for (int i = 0; i < tmp; i++)
    draw_(v_[i].first, v_[i].second);

  tmp = min(II, (int)vI.size());
  II -= tmp;
  for (int i = 0; i < tmp; i++)
    drawI(vI[i].first, vI[i].second);

  if (!v33.empty()) {
    if (chk(__ - 3, II - 1)) {
      draw31(v33[0].first, v33[0].second);
    } else if (chk(__ - 2, II - 2)) {
      draw22(v33[0].first, v33[0].second);
    } else if (chk(__ - 1, II - 3)) {
      draw13(v33[0].first, v33[0].second);
    } else
      return false;
  }
  if (chk(__, II)) {
    draw();
    return true;
  } else
    return false;
}
int main() {
  scanf("%d%d%d%d", &row, &col, &__, &II);
  for (int i = 0; i < row; i++)
    for (int j = 0; j < col; j++)
      s[i][j] = '.';
  if (!proc()) {
    puts("NO");
  } else {
    puts("YES");
    for (int i = 0; i < row; i++)
      puts(s[i]);
  }
  return 0;
}