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

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

enum STATE {
  TOP = 0,
  FRONT,
  BOTTOM,
  BACK,
  LEFT,
  RIGHT,
};

class Dice {
public:
  short v[6];
  Dice() { v[TOP] = v[FRONT] = v[BOTTOM] = v[BACK] = v[LEFT] = v[RIGHT] = 0; }
  Dice(short t, short f) {
    // top, front, bottom, back, left, right
    v[TOP] = 1;
    v[FRONT] = 3;
    v[BOTTOM] = 6;
    v[BACK] = 4;
    v[LEFT] = 2;
    v[RIGHT] = 5;
    REP(i, 0, 4) {
      if (v[TOP] == t)
        break;
      rotateR();
    }
    REP(i, 0, 4) {
      if (v[TOP] == t)
        break;
      rotateU();
    }
    REP(i, 0, 4) {
      if (v[FRONT] == f)
        break;
      short fnt = v[FRONT];
      v[FRONT] = v[RIGHT];
      v[RIGHT] = v[BACK];
      v[BACK] = v[LEFT];
      v[LEFT] = fnt;
    }
  }
  void rotateR() {
    short tp = v[LEFT];
    v[LEFT] = v[BOTTOM];
    v[BOTTOM] = v[RIGHT];
    v[RIGHT] = v[TOP];
    v[TOP] = tp;
  }
  void rotateL() {
    REP(i, 0, 3) { rotateR(); }
  }
  void rotateU() {
    short fnt = v[FRONT];
    v[FRONT] = v[BOTTOM];
    v[BOTTOM] = v[BACK];
    v[BACK] = v[TOP];
    v[TOP] = fnt;
  }
  void rotateD() {
    REP(i, 0, 3) { rotateU(); }
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<vector<vector<Dice>>> mp(
        2 * n + 3,
        vector<vector<Dice>>(2 * n + 3, vector<Dice>(n + 2, Dice())));
    REP(i, 0, 2 * n + 3) {
      REP(j, 0, 2 * n + 3) { mp[i][j][0] = Dice(1, 3); }
    }
    int ans[6] = {0};
    REP(i, 0, n) {
      short t, f;
      cin >> t >> f;
      Dice d(t, f);
      int x = n + 1, y = n + 1, z = n + 1;
      Dice nd;
      while (true) {
        // drop
        while (mp[y][x][z - 1].v[TOP] == 0) {
          z--;
        }
        int nx = x, ny = y, nz = z, mx = -1;
        if (mp[y + 1][x][z - 1].v[TOP] == 0 && d.v[FRONT] >= 4) {
          mx = d.v[FRONT];
          tie(nx, ny, nz) = make_tuple(x, y + 1, z - 1);
          nd = d;
          nd.rotateD();
        }
        if (mp[y - 1][x][z - 1].v[TOP] == 0 && d.v[BACK] >= 4) {
          if (mx < d.v[BACK]) {
            mx = d.v[BACK];
            tie(nx, ny, nz) = make_tuple(x, y - 1, z - 1);
            nd = d;
            nd.rotateU();
          }
        }
        if (mp[y][x + 1][z - 1].v[TOP] == 0 && d.v[RIGHT] >= 4) {
          if (mx < d.v[RIGHT]) {
            mx = d.v[RIGHT];
            tie(nx, ny, nz) = make_tuple(x + 1, y, z - 1);
            nd = d;
            nd.rotateR();
          }
        }
        if (mp[y][x - 1][z - 1].v[TOP] == 0 && d.v[LEFT] >= 4) {
          if (mx < d.v[LEFT]) {
            mx = d.v[LEFT];
            tie(nx, ny, nz) = make_tuple(x - 1, y, z - 1);
            nd = d;
            nd.rotateL();
          }
        }
        if (mx != -1) {
          tie(x, y, z) = make_tuple(nx, ny, nz);
          d = nd;
        } else {
          mp[y][x][z] = d;
          if (z != 1) {
            ans[mp[y][x][z - 1].v[TOP] - 1]--;
          }
          ans[mp[y][x][z].v[TOP] - 1]++;
          break;
        }
      }
    }
    REP(i, 0, 6) { cout << ans[i] << " \\n"[i == 5]; }
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

enum STATE {
  TOP = 0,
  FRONT,
  BOTTOM,
  BACK,
  LEFT,
  RIGHT,
};

class Dice {
public:
  vector<int> v;
  Dice() { v.resize(6, 0); }
  Dice(int t, int f) {
    // top, front, bottom, back, left, right
    v = {1, 3, 6, 4, 2, 5};
    REP(i, 0, 4) {
      if (v[TOP] == t)
        break;
      rotateR();
    }
    REP(i, 0, 4) {
      if (v[TOP] == t)
        break;
      rotateU();
    }
    REP(i, 0, 4) {
      if (v[FRONT] == f)
        break;
      int fnt = v[FRONT];
      v[FRONT] = v[RIGHT];
      v[RIGHT] = v[BACK];
      v[BACK] = v[LEFT];
      v[LEFT] = fnt;
    }
  }
  void rotateR() {
    int tp = v[LEFT];
    v[LEFT] = v[BOTTOM];
    v[BOTTOM] = v[RIGHT];
    v[RIGHT] = v[TOP];
    v[TOP] = tp;
  }
  void rotateL() {
    REP(i, 0, 3) { rotateR(); }
  }
  void rotateU() {
    int fnt = v[FRONT];
    v[FRONT] = v[BOTTOM];
    v[BOTTOM] = v[BACK];
    v[BACK] = v[TOP];
    v[TOP] = fnt;
  }
  void rotateD() {
    REP(i, 0, 3) { rotateU(); }
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<vector<vector<Dice>>> mp(
        2 * n + 3,
        vector<vector<Dice>>(2 * n + 3, vector<Dice>(n + 2, Dice())));
    REP(i, 0, 2 * n + 3) {
      REP(j, 0, 2 * n + 3) { mp[i][j][0] = Dice(1, 3); }
    }
    REP(i, 0, n) {
      int t, f;
      cin >> t >> f;
      Dice d(t, f);
      int x = n + 1, y = n + 1, z = n + 1;
      Dice nd;
      while (true) {
        // drop
        while (mp[y][x][z - 1].v[TOP] == 0) {
          z--;
        }
        int nx = x, ny = y, nz = z, mx = -1;
        if (mp[y + 1][x][z - 1].v[TOP] == 0 && d.v[FRONT] >= 4) {
          mx = d.v[FRONT];
          tie(nx, ny, nz) = make_tuple(x, y + 1, z - 1);
          nd = d;
          nd.rotateD();
        }
        if (mp[y - 1][x][z - 1].v[TOP] == 0 && d.v[BACK] >= 4) {
          if (mx < d.v[BACK]) {
            mx = d.v[BACK];
            tie(nx, ny, nz) = make_tuple(x, y - 1, z - 1);
            nd = d;
            nd.rotateU();
          }
        }
        if (mp[y][x + 1][z - 1].v[TOP] == 0 && d.v[RIGHT] >= 4) {
          if (mx < d.v[RIGHT]) {
            mx = d.v[RIGHT];
            tie(nx, ny, nz) = make_tuple(x + 1, y, z - 1);
            nd = d;
            nd.rotateR();
          }
        }
        if (mp[y][x - 1][z - 1].v[TOP] == 0 && d.v[LEFT] >= 4) {
          if (mx < d.v[LEFT]) {
            mx = d.v[LEFT];
            tie(nx, ny, nz) = make_tuple(x - 1, y, z - 1);
            nd = d;
            nd.rotateL();
          }
        }
        if (mx != -1) {
          tie(x, y, z) = make_tuple(nx, ny, nz);
          d = nd;
        } else {
          mp[y][x][z] = d;
          break;
        }
      }
    }
    int ans[6] = {0};
    REP(i, 0, 2 * n + 3) {
      REP(j, 0, 2 * n + 3) {
        int z = n + 1;
        while (mp[i][j][z].v[FRONT] == 0) {
          z--;
        }
        if (z != 0) {
          ans[mp[i][j][z].v[TOP] - 1]++;
        }
      }
    }
    REP(i, 0, 6) { cout << ans[i] << " \\n"[i == 5]; }
  }

  return 0;
}