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

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, M;
// x y z

vint xs[500], ys[500];
bool f[500][500];

int sx[500], sy[500];

signed main() {
  while (cin >> N >> M, N || M) {
    memset(f, 0, sizeof(f));
    rep(i, N) {
      xs[i].clear();
      ys[i].clear();
      sx[i] = sy[i] = 0;
    }
    vpint hoge;
    rep(i, M) {
      string s;
      int a, b;
      cin >> s >> a >> b;
      a--;
      b--;
      if (s == "xy") {
        f[a][b] = 1;
        sx[a]++;
        sy[b]++;
        hoge.pb(pint(a, b));
      } else if (s == "xz") {
        xs[b].pb(a);
      } else {
        ys[b].pb(a);
      }
    }
    sort(all(hoge));
    hoge.erase(unique(all(hoge)), hoge.end());
    int sum = 0;
    rep(z, N) {
      sort(all(xs[z]));
      sort(all(ys[z]));
      xs[z].erase(unique(all(xs[z])), xs[z].end());
      ys[z].erase(unique(all(ys[z])), ys[z].end());

      for (auto &x : xs[z]) {
        sum += N - sx[x];
      }
      for (auto &y : ys[z]) {
        sum += N - sy[y];
      }
      sum -= (int)xs[z].size() * ys[z].size();
      for (auto &x : xs[z]) {
        for (auto &y : ys[z]) {
          if (f[x][y])
            sum++;
        }
      }
    }
    sum += hoge.size() * N;
    cout << N * N * N - sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, M;
// x y z
bitset<500> f[500][500];
signed main() {
  while (cin >> N >> M, N || M) {
    memset(f, 0, sizeof(f));
    rep(i, M) {
      string s;
      int a, b;
      cin >> s >> a >> b;
      a--;
      b--;
      if (s == "xy") {
        rep(j, N) f[a][b][j] = 1;
      } else if (s == "yz") {
        rep(j, N) f[j][a][b] = 1;
      } else {
        rep(j, N) f[a][j][b] = 1;
      }
    }
    int ans = 0;
    rep(i, N) rep(j, N) rep(k, N) ans += f[i][j][k];
    cout << N * N * N - ans << endl;
  }
  return 0;
}