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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned long long ll;
int n, K;
int xf[100], xt[100];
int yf[100], yt[100];
int zf[100], zt[100];
vector<int> vx;
vector<int> vy;
vector<int> vz;
int cnt[201][201][201];

void zatu() {
  for (int i = 0; i < n; i++) {
    vx.push_back(xf[i]);
    vx.push_back(xt[i]);
    vy.push_back(yf[i]);
    vy.push_back(yt[i]);
    vz.push_back(zf[i]);
    vz.push_back(zt[i]);
  }
  sort(vx.begin(), vx.end());
  vx.erase(unique(vx.begin(), vx.end()), vx.end());
  sort(vy.begin(), vy.end());
  vy.erase(unique(vy.begin(), vy.end()), vy.end());
  sort(vz.begin(), vz.end());
  vz.erase(unique(vz.begin(), vz.end()), vz.end());
  for (int i = 0; i < n; i++) {
    // printf("%d %d %d %d %d %d\\n",xf[i],xt[i],yf[i],yt[i],zf[i],zt[i]);
    xf[i] = find(vx.begin(), vx.end(), xf[i]) - vx.begin();
    xt[i] = find(vx.begin(), vx.end(), xt[i]) - vx.begin();
    yf[i] = find(vy.begin(), vy.end(), yf[i]) - vy.begin();
    yt[i] = find(vy.begin(), vy.end(), yt[i]) - vy.begin();
    zf[i] = find(vz.begin(), vz.end(), zf[i]) - vz.begin();
    zt[i] = find(vz.begin(), vz.end(), zt[i]) - vz.begin();
    // printf("%d %d %d %d %d %d\\n",xf[i],xt[i],yf[i],yt[i],zf[i],zt[i]);
  }
}

int main(void) {
  scanf("%d%d", &n, &K);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d%d%d", &xf[i], &yf[i], &zf[i], &xt[i], &yt[i], &zt[i]);
  }
  zatu();
  for (int i = 0; i < n; i++) {
    for (int j = xf[i]; j < xt[i]; j++) {
      for (int k = yf[i]; k < yt[i]; k++) {
        for (int l = zf[i]; l < zt[i]; l++) {
          cnt[j][k][l]++;
        }
      }
    }
  }
  ll res = 0;
  for (int i = 0; i < vx.size() - 1; i++) {
    for (int j = 0; j < vy.size() - 1; j++) {
      for (int k = 0; k < vz.size() - 1; k++) {
        if (cnt[i][j][k] >= K) {
          res += (ll)(vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]) *
                 (vz[k + 1] - vz[k]);
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned long long ll;
int n, K;
int xf[100], xt[100];
int yf[100], yt[100];
int zf[100], zt[100];
vector<int> vx;
vector<int> vy;
vector<int> vz;
int cnt[501][501][501];

void zatu() {
  for (int i = 0; i < n; i++) {
    vx.push_back(xf[i]);
    vx.push_back(xt[i]);
    vy.push_back(yf[i]);
    vy.push_back(yt[i]);
    vz.push_back(zf[i]);
    vz.push_back(zt[i]);
  }
  sort(vx.begin(), vx.end());
  vx.erase(unique(vx.begin(), vx.end()), vx.end());
  sort(vy.begin(), vy.end());
  vy.erase(unique(vy.begin(), vy.end()), vy.end());
  sort(vz.begin(), vz.end());
  vz.erase(unique(vz.begin(), vz.end()), vz.end());
  for (int i = 0; i < n; i++) {
    // printf("%d %d %d %d %d %d\\n",xf[i],xt[i],yf[i],yt[i],zf[i],zt[i]);
    xf[i] = find(vx.begin(), vx.end(), xf[i]) - vx.begin();
    xt[i] = find(vx.begin(), vx.end(), xt[i]) - vx.begin();
    yf[i] = find(vy.begin(), vy.end(), yf[i]) - vy.begin();
    yt[i] = find(vy.begin(), vy.end(), yt[i]) - vy.begin();
    zf[i] = find(vz.begin(), vz.end(), zf[i]) - vz.begin();
    zt[i] = find(vz.begin(), vz.end(), zt[i]) - vz.begin();
    // printf("%d %d %d %d %d %d\\n",xf[i],xt[i],yf[i],yt[i],zf[i],zt[i]);
  }
}

int main(void) {
  scanf("%d%d", &n, &K);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d%d%d", &xf[i], &yf[i], &zf[i], &xt[i], &yt[i], &zt[i]);
  }
  zatu();
  for (int i = 0; i < n; i++) {
    for (int j = xf[i]; j < xt[i]; j++) {
      for (int k = yf[i]; k < yt[i]; k++) {
        for (int l = zf[i]; l < zt[i]; l++) {
          cnt[j][k][l]++;
        }
      }
    }
  }
  ll res = 0;
  for (int i = 0; i < vx.size() - 1; i++) {
    for (int j = 0; j < vy.size() - 1; j++) {
      for (int k = 0; k < vz.size() - 1; k++) {
        if (cnt[i][j][k] >= K) {
          res += (ll)(vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]) *
                 (vz[k + 1] - vz[k]);
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}