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

#include <stdio.h>
#include <string.h>

// cubeÌzñðmÛ(³Í1/4)
unsigned char cube[501][501][130];

int main() {
  int i, j, n, a, b, h, m, k, x, y, z;
  char c[3];
  c[2] = '\\n';
  while (0 <= scanf("%d%d", &n, &h)) {
    if (n == 0 && h == 0)
      break;
    // zñÌú»
    memset(cube, 0, sizeof(cube));

    // «ÌJ¢ÄécubeÌ
    m = 0;
    i = h + 1;
    while (--i) {
      j = n + 1;
      scanf(" %c %c %d%d ", &c[0], &c[1], &a, &b);
      if (c[0] == 'x' && c[1] == 'y') {
        while (--j) {
          // cubeÌ³ª³k³êÄ¢éÌÅA2ÌÝæðgÁÄAÌLè³µð¯Ê
          if ((cube[a][b][(j >> 2) + 1] >> (j % 4)) % 2 == 1)
            continue;
          cube[a][b][(j >> 2) + 1] += (1 << (j % 4));
          ++m;
        }
        /*				for(x=1;x<=n;++x){
                                                for(y=1;y<=n;++y){
                                                        for(z=1;z<=n;++z){
                                                                printf("%d
           ",(cube[x][y][(z>>2)+1]>>(z%4))%2);
                                                        }
                                                        printf("\\n");
                                                }
                                                printf("\\n");
                                        }*/
        continue;
      }
      if (c[0] == 'x' && c[1] == 'z') {
        //			printf("%c%c %d %d\\n",c[0],c[1],a,b);
        while (--j) {
          if ((cube[a][j][(b >> 2) + 1] >> (b % 4)) % 2 == 1)
            continue;
          cube[a][j][(b >> 2) + 1] += (1 << (b % 4));
          ++m;
        }
        /*				for(x=1;x<=n;++x){
                                                for(y=1;y<=n;++y){
                                                        for(z=1;z<=n;++z){
                                                                printf("%d
           ",(cube[x][y][(z>>2)+1]>>(z%4))%2);
                                                        }
                                                        printf("\\n");
                                                }
                                                printf("\\n");
                                        }
        */
        continue;
      }
      if (c[0] == 'y' && c[1] == 'z') {
        //			printf("%c%c %d %d\\n",c[0],c[1],a,b);
        while (--j) {
          //					printf("cube[%d][%d][(%d>>2)+1]=%d\\n",j,a,b,cube[j][a][(b>>2)+1]);
          if ((cube[j][a][(b >> 2) + 1] >> (b % 4)) % 2 == 1)
            continue;
          cube[j][a][(b >> 2) + 1] += (1 << (b % 4));
          ++m;
        }
        /*				for(x=1;x<=n;++x){
                                                for(y=1;y<=n;++y){
                                                        for(z=1;z<=n;++z){
                                                                printf("%d
           ",(cube[x][y][(z>>2)+1]>>(z%4))%2);
                                                        }
                                                        printf("\\n");
                                                }
                                                printf("\\n");
                                        }*/
        continue;
      }
    }
    // ÌJ¢Ä¢È¢cubeÌÂðoÍ
    printf("%d\\n", n * n * n - m);
    /*		for(i=1;i<=n;i++){
                            for(j=1;j<=n;j++){
                                    for(k=1;k<=n;k++){
                                            printf("%d
       ",(cube[i][j][(k>>2)+1]>>(k%4))%2);
                                    }
                                    printf("\\n");
                            }
                            printf("\\n");
                    }
                    */
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <stdio.h>

char cube[501][501][501];

int main() {
  int i, j, k, l, n, a, b, t, h, m;
  char c[2];
  t = 0;
  while (0 <= scanf("%d%d", &n, &h)) {
    if (n == 0 && h == 0)
      break;
    ++t;
    m = 0;
    i = h + 1;
    while (--i) {
      j = n + 1;
      scanf(" %c%c %d%d", &c[0], &c[1], &a, &b);
      if (c[0] == 'x' && c[1] == 'y') {
        while (--j) {
          if (cube[a][b][j] == t)
            continue;
          cube[a][b][j] = t;
          ++m;
        }
        continue;
      }
      if (c[0] == 'x' && c[1] == 'z') {
        while (--j) {
          if (cube[a][j][b] == t)
            continue;
          cube[a][j][b] = t;
          ++m;
        }
        continue;
      }
      if (c[0] == 'y' && c[1] == 'z') {
        while (--j) {
          if (cube[j][a][b] == t)
            continue;
          cube[j][a][b] = t;
          ++m;
        }
        continue;
      }
    }
    printf("%d\\n", n * n * n - m);
  }
  return 0;
}