Problem p00178 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

const int SIZE = 1001;
const int HEIGHT = 5001;
int n;
int D[SIZE + 10], P[SIZE + 10], Q[SIZE + 10];
bool B[HEIGHT + 10][10];

int solve() {
  for (int i = 0; i < HEIGHT; ++i) {
    for (int j = 0; j < 5; ++j) {
      B[i][j] = false;
    }
  }

  for (int i = 0; i < n; ++i) {
    int d = D[i], p = P[i], q = Q[i];
    if (d == 1) {
      bool flag2 = true;
      for (int j = HEIGHT - 1; j >= 0; --j) {
        bool flag = false;
        for (int k = 0; k < p; ++k) {
          if (B[j][q + k])
            flag = true;
        }
        if (flag) {
          for (int k = 0; k < p; ++k) {
            B[j + 1][q + k] = true;
          }
          flag2 = false;
          break;
        }
      }
      if (flag2) {
        for (int k = 0; k < p; ++k) {
          B[0][q + k] = true;
        }
      }
    } else if (d == 2) {
      bool flag2 = true;
      for (int j = HEIGHT - 1; j >= 0; --j) {
        bool flag = false;
        for (int k = 0; k < p; ++k) {
          if (B[j + k][q])
            flag = true;
        }
        if (flag) {
          for (int k = 0; k < p; ++k) {
            B[j + k + 1][q] = true;
          }
          flag2 = false;
          break;
        }
      }
      if (flag2) {
        for (int k = 0; k < p; ++k) {
          B[k][q] = true;
        }
      }
    }

    for (int j = HEIGHT - 1; j >= 0; --j) {
      bool flag = true;
      for (int k = 0; k < 5; ++k) {
        if (!B[j][k])
          flag = false;
      }
      if (flag) {
        for (int k = j + 1; k < HEIGHT; ++k) {
          for (int l = 0; l < 5; ++l) {
            B[k - 1][l] = B[k][l];
          }
          for (int l = 0; l < 5; ++l) {
            B[k][l] = false;
          }
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < HEIGHT; ++i) {
    for (int j = 0; j < 5; ++j) {
      if (B[i][j])
        res++;
    }
  }
  return res;
}

int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; ++i) {
      cin >> D[i] >> P[i] >> Q[i];
      Q[i]--;
    }
    cout << solve() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

const int SIZE = 1001;
const int HEIGHT = 5001;
int n;
int D[SIZE], P[SIZE], Q[SIZE];
bool B[HEIGHT][5];

int solve() {
  fill((bool *)B, (bool *)B + HEIGHT * 5, false);

  for (int i = 0; i < n; ++i) {
    int d = D[i], p = P[i], q = Q[i];
    if (d == 1) {
      bool flag2 = true;
      for (int j = HEIGHT - 1; j >= 0; --j) {
        bool flag = false;
        for (int k = 0; k < p; ++k) {
          if (B[j][q + k])
            flag = true;
        }
        if (flag) {
          for (int k = 0; k < p; ++k) {
            B[j + 1][q + k] = true;
          }
          flag2 = false;
          break;
        }
      }
      if (flag2) {
        for (int k = 0; k < p; ++k) {
          B[0][q + k] = true;
        }
      }
    } else if (d == 2) {
      bool flag2 = true;
      for (int j = HEIGHT - 1; j >= 0; --j) {
        bool flag = false;
        for (int k = 0; k < p; ++k) {
          if (B[j + k][q])
            flag = true;
        }
        if (flag) {
          for (int k = 0; k < p; ++k) {
            B[j + k + 1][q] = true;
          }
          flag2 = false;
          break;
        }
      }
      if (flag2) {
        for (int k = 0; k < p; ++k) {
          B[k][q] = true;
        }
      }
    }

    for (int j = HEIGHT - 1; j >= 0; --j) {
      bool flag = true;
      for (int k = 0; k < 5; ++k) {
        if (!B[j][k])
          flag = false;
      }
      if (flag) {
        for (int k = j + 1; k < HEIGHT; ++k) {
          for (int l = 0; l < 5; ++l) {
            B[k - 1][l] = B[k][l];
          }
          for (int l = 0; l < 5; ++l) {
            B[k][l] = false;
          }
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i < HEIGHT; ++i) {
    for (int j = 0; j < 5; ++j) {
      if (B[i][j])
        res++;
    }
  }
  return res;
}

int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; ++i) {
      cin >> D[i] >> P[i] >> Q[i];
      Q[i]--;
    }
    cout << solve() << endl;
  }
  return 0;
}