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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define X first
#define Y second.first
#define Z second.second
#define DEBUG 1

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> POS;

char cord[3];

int main() {
  //	cut here before submit
  //	freopen ("testcase.cwh10", "r", stdin );
  int n, h;
  while (scanf("%d %d", &n, &h) && n && h) {
    set<POS> pos;
    pos.clear();
    rep(i, h) {
      int u, v;
      scanf("%s %d %d", cord, &u, &v);
      u--, v--;
      if (cord[0] == 'x' && cord[1] == 'z')
        swap(cord[0], cord[1]);
      switch (cord[0]) {
      case 'x':
        pos.insert(POS(u, P(v, -1)));
        break;
      case 'y':
        pos.insert(POS(-1, P(u, v)));
        break;
      case 'z':
        pos.insert(POS(u, P(-1, v)));
        break;
      } // end switch
    }   // end rep
    int res = n * n * n;
    set<POS>::iterator it, jt;
    for (it = pos.begin(); it != pos.end(); it++) {
      if ((*it).X == -1) {
        res -= n;
      } // end if
    }   // end rep

    map<POS, int> intersect;
    intersect.clear();
    for (it = pos.begin(); it != pos.end(); it++) {
      if ((*it).Y == -1) {
        int cx = (*it).X;
        int cz = (*it).Z;
        res -= n;
        for (jt = pos.begin(); jt != pos.end(); jt++) {
          if ((*jt).X == -1 && (*jt).Z == cz) {
            POS is;
            is.X = cx, is.Y = (*jt).Y, is.Z = cz;
            intersect[is]++;
          } // end if
        }   // end rep
      }     // end if
    }       // end rep

    for (it = pos.begin(); it != pos.end(); it++) {
      if ((*it).Z == -1) {
        int cx = (*it).X;
        int cy = (*it).Y;
        res -= n;
        for (jt = pos.begin(); jt != pos.end(); jt++) {
          if ((*jt).X == -1 && (*jt).Y == cy) {
            POS t;
            t.X = cx, t.Y = cy, t.Z = (*jt).Z;
            intersect[t]++;
          } // end if
        }   // end rep
        for (jt = pos.begin(); jt != pos.end(); jt++) {
          if ((*jt).Y == -1 && (*jt).X == cx) {
            POS t;
            t.X = cx, t.Y = cy, t.Z = (*jt).Z;
            intersect[t]++;
          } // end if
        }   // end rep
      }     // end if
    }       // end rep

    map<POS, int>::iterator ii = intersect.begin();
    for (; ii != intersect.end(); ii++) {
      if ((*ii).second == 3)
        res += 2;
      else
        res += (*ii).second;
    } // end for
    printf("%d\\n", res);
  } // end while

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> iP;

bool cube[505][505][505]; // x, y, z order
char cord[3];
int main() {
  //	cut here before submit
  //	freopen ("testcase.CWH", "r", stdin );
  int n, h;
  while (scanf("%d %d", &n, &h) && n && h) {
    rep(i, n) rep(j, n) rep(k, n) cube[i][j][k] = true;

    rep(i, h) {
      int u, v;
      scanf("%s %d %d", cord, &u, &v);
      u--, v--;
      if (cord[0] == 'x' && cord[1] == 'z')
        swap(cord[0], cord[1]);
      switch (cord[0]) {
      case 'x':
        rep(i, n) cube[u][v][i] = false;
        break;
      case 'y':
        rep(i, n) cube[i][u][v] = false;
        break;
      case 'z':
        rep(i, n) cube[v][i][u] = false;
        break;
      } // end switch
    }   // end rep
    int res = 0;
    rep(i, n) rep(j, n) rep(k, n) if (cube[i][j][k]) res++;
    //		cout << res << endl;
    printf("%d\\n", res);
  } // end switch

  return 0;
}