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

#include <iostream>
#include <string>

#define set(v, vt, vx, vy, vz)                                                 \\
  {                                                                            \\
    v.t = vt;                                                                  \\
    v.x = vx;                                                                  \\
    v.y = vy;                                                                  \\
    v.z = vz;                                                                  \\
  }

using namespace std;

struct Vector3 {
  char t;
  short x;
  short y;
  short z;
};

int main(void) {
  int n, h;
  Vector3 pos[200];

  while (cin >> n >> h, n | h) {
    string c;
    short a, b;
    int count = 0;
    for (int i = 0; i < h; ++i) {
      cin >> c >> a >> b;
      if (c == "xy") {
        set(pos[i], 'A', a, b, -1);
      } else if (c == "xz") {
        set(pos[i], 'B', a, -1, b);
      } else if (c == "yz") {
        set(pos[i], 'C', -1, a, b);
      }
      count += n;
    }
    int cnt = 0;
    for (int i = 0; i < h - 1; ++i) {
      int m = 0;
      switch (pos[i].t) {
      case 'A':
        for (int j = i + 1; j < h; ++j) {
          if (pos[i].t == pos[j].t)
            continue;
          if (pos[i].x == pos[j].x) {
            --count;
            for (int k = 0; k < h; ++k) {
              if (i == k || j == k)
                continue;
              if (pos[i].y == pos[k].y && pos[j].z == pos[k].z) {
                cnt++;
                break;
              }
            }
          } else if (pos[i].y == pos[j].y) {
            --count;
            for (int k = 0; k < h; ++k) {
              if (i == k || j == k)
                continue;
              if (pos[i].x == pos[k].x && pos[j].z == pos[k].z) {
                cnt++;
                break;
              }
            }
          }
        }
        break;
      case 'B':
        for (int j = i + 1; j < h; ++j) {
          if (pos[i].t == pos[j].t)
            continue;
          if (pos[i].x == pos[j].x) {
            --count;
            for (int k = 0; k < h; ++k) {
              if (i == k || j == k)
                continue;
              if (pos[i].z == pos[k].z && pos[j].y == pos[k].y) {
                cnt++;
                break;
              }
            }
          } else if (pos[i].z == pos[j].z) {
            --count;
            for (int k = 0; k < h; ++k) {
              if (i == k || j == k)
                continue;
              if (pos[i].x == pos[k].x && pos[j].y == pos[k].y) {
                cnt++;
                break;
              }
            }
          }
        }
        break;
      case 'C':
        for (int j = i + 1; j < h; ++j) {
          if (pos[i].t == pos[j].t)
            continue;
          if (pos[i].y == pos[j].y) {
            --count;
            for (int k = 0; k < h; ++k) {
              if (i == k || j == k)
                continue;
              if (pos[i].z == pos[k].z && pos[j].x == pos[k].x) {
                cnt++;
                break;
              }
            }
          } else if (pos[i].z == pos[j].z) {
            --count;
            for (int k = 0; k < h; ++k) {
              if (i == k || j == k)
                continue;
              if (pos[i].y == pos[k].y && pos[j].x == pos[k].x) {
                cnt++;
                break;
              }
            }
          }
        }
        break;
      }
    }
    cout << n * n * n - (count + (cnt / 3)) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

bool cube[500][500][500];

int main(void) {
  int n, h;
  while (cin >> n >> h, n | h) {
    string c;
    int a, b;
    for (int z = 0; z < n; ++z)
      for (int y = 0; y < n; ++y)
        for (int x = 0; x < n; ++x)
          cube[z][y][x] = true;
    int count = 0;
    for (int i = 0; i < h; ++i) {
      cin >> c >> a >> b;
      --a;
      --b;
      if (c == "xy") {
        for (int j = 0; j < n; ++j)
          if (cube[j][b][a]) {
            cube[j][b][a] = false;
            ++count;
          }
      } else if (c == "xz") {
        for (int j = 0; j < n; ++j)
          if (cube[b][j][a]) {
            cube[b][j][a] = false;
            ++count;
          }
      } else if (c == "yz") {
        for (int j = 0; j < n; ++j)
          if (cube[b][a][j]) {
            cube[b][a][j] = false;
            ++count;
          }
      }
    }
    cout << n * n * n - count << endl;
  }
  return 0;
}