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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

template <class T> struct dice {
  T t, b, n, s, e, w; // top bottom north south east west
  dice() {}
  dice(T t, T b, T n, T s, T e, T w) : t(t), b(b), n(n), s(s), e(e), w(w) {}
  void roll(T &a, T &b, T &c, T &d) {
    swap(a, b);
    swap(b, c);
    swap(c, d);
  }
  void roll_x() { roll(t, n, b, s); }
  void roll_y() { roll(t, w, b, e); }
  void roll_z() { roll(s, e, n, w); }
  void roll_d(int d) {
    if (d == 0)
      roll(t, s, b, n);
    else if (d == 1)
      roll(t, n, b, s);
    else if (d == 2)
      roll(t, w, b, e);
    else
      roll(t, e, b, w);
  }
  vector<dice> all_rolls() {
    vector<dice> ret;
    for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        ret.push_back(*this);
    return ret;
  }
  void toTable(int *res) {
    res[0] = t;
    res[1] = b;
    res[2] = n;
    res[3] = s;
    res[4] = e;
    res[5] = w;
  }
};

int main() {
  int dy[] = {1, -1, 0, 0};
  int dx[] = {0, 0, 1, -1};
  dice<int> DI(1, 6, 2, 5, 4, 3);
  vector<dice<int>> vs = DI.all_rolls();
  int n;
  while (cin >> n, n) {
    map<pii, pii> mp;
    REP(i, n) {
      int t, s;
      cin >> t >> s;
      dice<int> di;
      bool tmp = 0;
      // cout << "a" << endl;
      FOR(it, vs) {
        if (it->t == t && it->s == s) {
          di = *it;
          tmp = 1;
          break;
        }
      }
      // cout << "a" << endl;
      assert(tmp == 1);
      int y = 0, x = 0;
      while (1) {
        pii now = mp[pii(y, x)];
        bool f = 0;
        for (int j = 6; j >= 4; --j) {
          int r[6];
          di.toTable(r);
          for (int k = 2; k < 6; ++k) {
            if (r[k] == j) {
              int yy = y + dy[k - 2];
              int xx = x + dx[k - 2];
              pii p = mp[pii(yy, xx)];
              if (p.first < now.first) {
                y = yy;
                x = xx;
                f = 1;
                di.roll_d(k - 2);
                break;
              }
            }
          }
          if (f)
            break;
        }
        if (!f)
          break;
      }
      mp[pii(y, x)].first++;
      mp[pii(y, x)].second = di.t;
    }
    int ar[6] = {};
    FOR(it, mp) {
      pii p = it->first;
      pii q = it->second;
      // cout << p.first << "," << p.second << endl;
      // cout << q.first << ";" << q.second << endl;
      if (it->second.first > 0) {
        ar[it->second.second - 1]++;
      }
    }
    REP(i, 6) {
      if (i)
        cout << " ";
      cout << ar[i];
    }
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

template <class T> struct dice {
  T t, b, n, s, e, w; // top bottom north south east west
  dice() {}
  dice(T t, T b, T n, T s, T e, T w) : t(t), b(b), n(n), s(s), e(e), w(w) {}
  void roll(T &a, T &b, T &c, T &d) {
    swap(a, b);
    swap(b, c);
    swap(c, d);
  }
  void roll_x() { roll(t, n, b, s); }
  void roll_y() { roll(t, w, b, e); }
  void roll_z() { roll(s, e, n, w); }
  void roll_d(int d) {
    if (d == 0)
      roll(t, s, b, n);
    else if (d == 1)
      roll(t, n, b, s);
    else if (d == 2)
      roll(t, w, b, e);
    else
      roll(t, e, b, w);
  }
  vector<dice> all_rolls() {
    vector<dice> ret;
    for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        ret.push_back(*this);
    return ret;
  }
  void toTable(int *res) {
    res[0] = t;
    res[1] = b;
    res[2] = n;
    res[3] = s;
    res[4] = e;
    res[5] = w;
  }
};

int main() {
  int dy[] = {1, -1, 0, 0};
  int dx[] = {0, 0, 1, -1};
  dice<int> DI(1, 6, 2, 5, 3, 4);
  int n;
  while (cin >> n, n) {
    map<pii, pii> mp;
    REP(i, n) {
      int t, s;
      cin >> t >> s;
      dice<int> di;
      FOR(it, DI.all_rolls()) {
        if (it->t == t && it->s == s) {
          di = *it;
          break;
        }
      }
      int y = 0, x = 0;
      while (1) {
        pii now = mp[pii(y, x)];
        bool f = 0;
        for (int j = 6; j >= 4; --j) {
          int r[6];
          di.toTable(r);
          for (int k = 2; k < 6; ++k) {
            if (r[k] == j) {
              int yy = y + dy[k - 2];
              int xx = x + dx[k - 2];
              pii p = mp[pii(yy, xx)];
              if (p.first < now.first) {
                y = yy;
                x = xx;
                f = 1;
                di.roll_d(k - 2);
                break;
              }
            }
          }
          if (f)
            break;
        }
        if (!f)
          break;
      }
      mp[pii(y, x)].first++;
      mp[pii(y, x)].second = di.t;
    }
    int ar[6] = {};
    FOR(it, mp) {
      pii p = it->first;
      pii q = it->second;
      // cout << p.first << "," << p.second << endl;
      // cout << q.first << ";" << q.second << endl;
      if (it->second.first > 0) {
        ar[it->second.second - 1]++;
      }
    }
    REP(i, 6) {
      if (i)
        cout << " ";
      cout << ar[i];
    }
    cout << endl;
  }
}