Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  vector<int> a(N);
  rep(i, N) cin >> a[i];

  vector<vector<int>> ans = vector<vector<int>>(N, vector<int>(N));
  int index = 0;
  for (int i = 0; i < H; i++) {
    if (i % 2 == 0) {
      // cout << "right" << endl;
      for (int j = 0; j < W; j++) {
        ans[i][j] = index + 1;
        a[index]--;
        if (a[index] == 0)
          index++;
      }
    } else {
      // cout << "left" << endl;
      for (int j = W - 1; j >= 0; j--) {
        ans[i][j] = index + 1;
        a[index]--;
        if (a[index] == 0)
          index++;
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (j == 0)
        cout << ans[i][j];
      else
        cout << " " << ans[i][j];
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  vector<int> a(N);
  rep(i, N) cin >> a[i];

  vector<vector<int>> ans = vector<vector<int>>(H, vector<int>(W));
  int index = 0;
  for (int i = 0; i < H; i++) {
    if (i % 2 == 0) {
      // cout << "right" << endl;
      for (int j = 0; j < W; j++) {
        ans[i][j] = index + 1;
        a[index]--;
        if (a[index] == 0)
          index++;
      }
    } else {
      // cout << "left" << endl;
      for (int j = W - 1; j >= 0; j--) {
        ans[i][j] = index + 1;
        a[index]--;
        if (a[index] == 0)
          index++;
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (j == 0)
        cout << ans[i][j];
      else
        cout << " " << ans[i][j];
    }
    cout << endl;
  }
  return 0;
}