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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
const int INF = 1 << 30;
using namespace std;

int n, k;

short int pt[300][300][300];
int X1[50], X2[50], Y1[50], Y2[50], D1[50], D2[50];
unsigned long long int uX[300], uY[300], uD[300];

int compress(int *x1, int *x2, unsigned long long int *unzip) {
  vector<int> vec;
  for (int i = 0; i < n; i++) {
    for (int j = -1; j <= 1; j++) {
      int a = x1[i] + j, b = x2[i] + j;
      if (0 <= a)
        vec.push_back(a);
      if (0 <= b)
        vec.push_back(b);
    }
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  for (int i = 0; i < n; i++) {
    x1[i] = find(vec.begin(), vec.end(), x1[i]) - vec.begin();
    x2[i] = find(vec.begin(), vec.end(), x2[i]) - vec.begin();
  }
  for (int i = 0; i < vec.size(); i++)
    unzip[i] = vec[i];
  return vec.size();
}

int main() {
  int W, H, D;
  unsigned long long int ans = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> X1[i] >> Y1[i] >> D1[i];
    cin >> X2[i] >> Y2[i] >> D2[i];
  }
  W = compress(X1, X2, uX);
  H = compress(Y1, Y2, uY);
  D = compress(D1, D2, uD);
  for (int i = 0; i < n; i++) {
    for (int j = X1[i]; j < X2[i]; j++) {
      for (int m = Y1[i]; m < Y2[i]; m++) {
        for (int l = D1[i]; l < D2[i]; l++) {
          pt[j][m][l]++;
        }
      }
    }
  }
  for (int m = 0; m < D; m++) {
    for (int j = 0; j < H; j++) {
      for (int i = 0; i < W; i++) {
        if (pt[i][j][m] >= k && uX[i + 1] && uY[j + 1] && uD[m + 1]) {
          ans +=
              (uX[i + 1] - uX[i]) * (uY[j + 1] - uY[j]) * (uD[m + 1] - uD[m]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
const int INF = 1 << 30;
using namespace std;

int n, k;

int pt[300][300][300];
int X1[50], X2[50], Y1[50], Y2[50], D1[50], D2[50];
unsigned long long int uX[150], uY[150], uD[150];

int compress(int *x1, int *x2, unsigned long long int *unzip) {
  vector<int> vec;
  for (int i = 0; i < n; i++) {
    for (int j = -1; j <= 1; j++) {
      int a = x1[i] + j, b = x2[i] + j;
      if (0 <= a)
        vec.push_back(a);
      if (0 <= b)
        vec.push_back(b);
    }
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  for (int i = 0; i < n; i++) {
    x1[i] = find(vec.begin(), vec.end(), x1[i]) - vec.begin();
    x2[i] = find(vec.begin(), vec.end(), x2[i]) - vec.begin();
  }
  for (int i = 0; i < vec.size(); i++)
    unzip[i] = vec[i];
  return vec.size();
}

int main() {
  int W, H, D;
  unsigned long long int ans = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> X1[i] >> Y1[i] >> D1[i];
    cin >> X2[i] >> Y2[i] >> D2[i];
  }
  W = compress(X1, X2, uX);
  H = compress(Y1, Y2, uY);
  D = compress(D1, D2, uD);
  for (int i = 0; i < n; i++) {
    for (int j = X1[i]; j < X2[i]; j++) {
      for (int m = Y1[i]; m < Y2[i]; m++) {
        for (int l = D1[i]; l < D2[i]; l++) {
          pt[j][m][l]++;
        }
      }
    }
  }
  for (int m = 0; m < D; m++) {
    for (int j = 0; j < H; j++) {
      for (int i = 0; i < W; i++) {
        if (pt[i][j][m] >= k && uX[i + 1] && uY[j + 1] && uD[m + 1]) {
          ans +=
              (uX[i + 1] - uX[i]) * (uY[j + 1] - uY[j]) * (uD[m + 1] - uD[m]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}