Problem p00762 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
// #define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

// N, E, S, W
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

struct Dice {
  V v;
  Dice() {}
  Dice(V _v) : v(_v) {}
  Dice(int _t, int _f) : v(6) {
    v[0] = _t;
    v[1] = _f;
    v[2] = getRight(_t, _f);
    v[5] = 7 - v[0];
    v[4] = 7 - v[1];
    v[3] = 7 - v[2];
  }
  int getRight(int t, int f) {
    bool swapped = false;
    if (t > f) {
      swap(t, f);
      swapped = true;
    }
    int r = -1;
    if (t == 1 && f == 2)
      r = 3;
    if (t == 1 && f == 3)
      r = 5;
    if (t == 1 && f == 4)
      r = 2;
    if (t == 1 && f == 5)
      r = 4;
    if (t == 2 && f == 3)
      r = 1;
    if (t == 2 && f == 4)
      r = 6;
    if (t == 2 && f == 6)
      r = 3;
    if (t == 3 && f == 5)
      r = 1;
    if (t == 3 && f == 6)
      r = 5;
    if (t == 4 && f == 5)
      r = 6;
    if (t == 4 && f == 6)
      r = 2;
    if (t == 5 && f == 6)
      r = 4;
    // assert(r != -1);
    if (swapped)
      r = 7 - r;
    return r;
  }
  void rot(int dir) {
    int t = v[0];
    switch (dir) {
    case 0: // 'N'
      v[0] = v[1];
      v[1] = v[5];
      v[5] = v[4];
      v[4] = t;
      break;
    case 1: // 'E'
      v[0] = v[3];
      v[3] = v[5];
      v[5] = v[2];
      v[2] = t;
      break;
    case 2: // 'S'
      v[0] = v[4];
      v[4] = v[5];
      v[5] = v[1];
      v[1] = t;
      break;
    case 3: // 'W'
      v[0] = v[2];
      v[2] = v[5];
      v[5] = v[3];
      v[3] = t;
      break;
    }
    // assert(1);
  }
  int at(int dir) {
    switch (dir) {
    case 0:
      return v[4];
      break; // 'N'
    case 1:
      return v[2];
      break; // 'E'
    case 2:
      return v[1];
      break; // 'S'
    case 3:
      return v[3];
      break; // 'W'
    }
    // assert(1);
  }
  void print() {
    rep(i, 6) cerr << v[i] << " ";
    cerr << endl;
  }
};

const int MAX = 200;
int field[MAX][MAX][MAX];

void drop(int x, int y, Dice dice) {
  // fall
  int z = 0;
  while (field[x][y][z])
    z++;
  // cerr << "drop: (" << x << ", " << y << ", " << z << ") ";
  // rep(i, 6) cerr << dice.v[i] << " ";
  // cerr << endl;

  int nz = z - 1;
  bool dropped = false;
  if (nz >= 0) {
    int dir = -1;
    int ma_num = -1;
    rep(k, 4) {
      int nx = x + dx[k], ny = y + dy[k];
      // ??????4??\\??? ?????? ????????????????????? ->
      // ???????????§?????????????????????
      if (dice.at(k) >= 4 && !field[nx][ny][nz] && ma_num < dice.at(k)) {
        dir = k;
        ma_num = dice.at(k);
      }
    }
    if (dir != -1) {
      dice.rot(dir);

      // cerr << "move to: " << dir << ": (" << x + dx[dir] << ", " << y +
      // dy[dir] << ")"; dice.print();

      drop(x + dx[dir], y + dy[dir], dice);

      dropped = true;
    }
  }

  if (!dropped) {
    field[x][y][z] = dice.v[0];
  }
}

void solve(int n) {
  rep(x, MAX) rep(y, MAX) rep(z, MAX) field[x][y][z] = 0;
  rep(i, n) {
    int t, f;
    cin >> t >> f;
    Dice dice(t, f);
    drop(MAX / 2, MAX / 2, dice);
  }

  V cnt(6);
  rep(x, MAX) {
    rep(y, MAX) {
      rrep(z, MAX) {
        if (field[x][y][z]) {
          // cerr << x - 100 << " " << y - 100 << " " << z << " " <<
          // field[x][y][z] << endl;
          ++cnt[field[x][y][z] - 1];
          break;
        }
      }
    }
  }

  // cerr << "Ans:";
  rep(i, 6) { cout << (i != 0 ? " " : "") << cnt[i]; }
  cout << endl;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  while (cin >> n, n) {
    solve(n);
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

// N, E, S, W
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

struct Dice {
  V v;
  Dice() {}
  Dice(V _v) : v(_v) {}
  Dice(int _t, int _f) : v(6) {
    v[0] = _t;
    v[1] = _f;
    v[2] = getRight(_t, _f);
    v[5] = 7 - v[0];
    v[4] = 7 - v[1];
    v[3] = 7 - v[2];
  }
  int getRight(int t, int f) {
    bool swapped = false;
    if (t > f) {
      swap(t, f);
      swapped = true;
    }
    int r = -1;
    if (t == 1 && f == 2)
      r = 3;
    if (t == 1 && f == 3)
      r = 5;
    if (t == 1 && f == 4)
      r = 2;
    if (t == 1 && f == 5)
      r = 4;
    if (t == 2 && f == 3)
      r = 1;
    if (t == 2 && f == 4)
      r = 6;
    if (t == 2 && f == 6)
      r = 3;
    if (t == 3 && f == 5)
      r = 1;
    if (t == 3 && f == 6)
      r = 5;
    if (t == 4 && f == 5)
      r = 6;
    if (t == 4 && f == 6)
      r = 2;
    if (t == 5 && f == 6)
      r = 4;
    // assert(r != -1);
    if (swapped)
      r = 7 - r;
    return r;
  }
  void rot(int dir) {
    int t = v[0];
    switch (dir) {
    case 0: // 'N'
      v[0] = v[1];
      v[1] = v[5];
      v[5] = v[4];
      v[4] = t;
      break;
    case 1: // 'E'
      v[0] = v[3];
      v[3] = v[5];
      v[5] = v[2];
      v[2] = t;
      break;
    case 2: // 'S'
      v[0] = v[4];
      v[4] = v[5];
      v[5] = v[1];
      v[1] = t;
      break;
    case 3: // 'W'
      v[0] = v[2];
      v[2] = v[5];
      v[5] = v[3];
      v[3] = t;
      break;
    }
    // assert(1);
  }
  int at(int dir) {
    switch (dir) {
    case 0:
      return v[4];
      break; // 'N'
    case 1:
      return v[2];
      break; // 'E'
    case 2:
      return v[1];
      break; // 'S'
    case 3:
      return v[3];
      break; // 'W'
    }
    // assert(1);
  }
  void print() {
    rep(i, 6) cerr << v[i] << " ";
    cerr << endl;
  }
};

const int MAX = 200;
int field[MAX][MAX][MAX];

void drop(int x, int y, Dice dice) {
  // fall
  int z = 0;
  while (field[x][y][z])
    z++;
  // cerr << "drop: (" << x << ", " << y << ", " << z << ") ";
  // rep(i, 6) cerr << dice.v[i] << " ";
  // cerr << endl;

  int nz = z - 1;
  bool dropped = false;
  if (nz >= 0) {
    int dir = -1;
    int ma_num = -1;
    rep(k, 4) {
      int nx = x + dx[k], ny = y + dy[k];
      // ??????4??\\??? ?????? ????????????????????? ->
      // ???????????§?????????????????????
      if (dice.at(k) >= 4 && !field[nx][ny][nz] && ma_num < dice.at(k)) {
        dir = k;
        ma_num = dice.at(k);
      }
    }
    if (dir != -1) {
      dice.rot(dir);

      // cerr << "move to: " << dir << ": (" << x + dx[dir] << ", " << y +
      // dy[dir] << ")"; dice.print();

      drop(x + dx[dir], y + dy[dir], dice);

      dropped = true;
    }
  }

  if (!dropped) {
    field[x][y][z] = dice.v[0];
  }
}

void solve(int n) {
  rep(x, MAX) rep(y, MAX) rep(z, MAX) field[x][y][z] = 0;
  rep(i, n) {
    int t, f;
    cin >> t >> f;
    Dice dice(t, f);
    drop(MAX / 2, MAX / 2, dice);
  }

  V cnt(6);
  rep(x, MAX) {
    rep(y, MAX) {
      rrep(z, MAX) {
        if (field[x][y][z]) {
          // cerr << x - 100 << " " << y - 100 << " " << z << " " <<
          // field[x][y][z] << endl;
          ++cnt[field[x][y][z] - 1];
          break;
        }
      }
    }
  }

  // cerr << "Ans:";
  rep(i, 6) { cout << (i != 0 ? " " : "") << cnt[i]; }
  cout << endl;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  while (cin >> n, n) {
    solve(n);
  }
}