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

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  int x[2][50], y[2][50], z[2][50];
  vector<int> dx, dy, dz;
  for (int i = 0; i < N; i++) {
    scanf("%d%d%d%d%d%d", &x[0][i], &y[0][i], &z[0][i], &x[1][i], &y[1][i],
          &z[1][i]);
    dx.push_back(x[0][i]);
    dx.push_back(x[1][i]);
    dy.push_back(y[0][i]);
    dy.push_back(y[1][i]);
    dz.push_back(z[0][i]);
    dz.push_back(z[1][i]);
  }
  sort(dx.begin(), dx.end());
  sort(dy.begin(), dy.end());
  sort(dz.begin(), dz.end());
  dx.erase(unique(dx.begin(), dx.end()), dx.end());
  dy.erase(unique(dy.begin(), dy.end()), dy.end());
  dz.erase(unique(dz.begin(), dz.end()), dz.end());
  int d[dx.size() - 1][dy.size() - 1][dz.size() - 1];
  for (int i = 0; i < dx.size() - 1; i++)
    for (int j = 0; j < dy.size() - 1; j++)
      for (int k = 0; k < dz.size() - 1; k++)
        d[i][j][k] = 0;
  for (int s = 0; s < N; s++)
    for (int i = 0; i < dx.size() - 1; i++)
      for (int j = 0; j < dy.size() - 1; j++)
        for (int k = 0; k < dz.size() - 1; k++)
          if (x[0][s] <= dx[i] && dx[i + 1] <= x[1][s])
            if (y[0][s] <= dy[j] && dy[j + 1] <= y[1][s])
              if (z[0][s] <= dz[k] && dz[k + 1] <= z[1][s])
                d[i][j][k]++;
  ll res = 0;
  for (int i = 0; i < dx.size() - 1; i++)
    for (int j = 0; j < dy.size() - 1; j++)
      for (int k = 0; k < dz.size() - 1; k++)
        if (d[i][j][k] >= K)
          res += (ll)(dx[i + 1] - dx[i]) * (dy[j + 1] - dy[j]) *
                 (dz[k + 1] - dz[k]);
  printf("%lld\\n", res);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  int x[2][50], y[2][50], z[2][50];
  vector<int> dx, dy, dz;
  for (int i = 0; i < N; i++) {
    scanf("%d%d%d%d%d%d", &x[0][i], &y[0][i], &z[0][i], &x[1][i], &y[1][i],
          &z[1][i]);
    dx.push_back(x[0][i]);
    dx.push_back(x[1][i]);
    dy.push_back(y[0][i]);
    dy.push_back(y[1][i]);
    dz.push_back(z[0][i]);
    dz.push_back(z[1][i]);
  }
  sort(dx.begin(), dx.end());
  sort(dy.begin(), dy.end());
  sort(dz.begin(), dz.end());
  dx.erase(unique(dx.begin(), dx.end()), dx.end());
  dy.erase(unique(dy.begin(), dy.end()), dy.end());
  dz.erase(unique(dz.begin(), dz.end()), dz.end());
  int d[dx.size() - 1][dy.size() - 1][dz.size() - 1];
  for (int i = 0; i < dx.size(); i++)
    for (int j = 0; j < dy.size(); j++)
      for (int k = 0; k < dz.size(); k++)
        d[i][j][k] = 0;
  for (int s = 0; s < N; s++)
    for (int i = 0; i < dx.size() - 1; i++)
      for (int j = 0; j < dy.size() - 1; j++)
        for (int k = 0; k < dz.size() - 1; k++)
          if (x[0][s] <= dx[i] && dx[i + 1] <= x[1][s])
            if (y[0][s] <= dy[j] && dy[j + 1] <= y[1][s])
              if (z[0][s] <= dz[k] && dz[k + 1] <= z[1][s])
                d[i][j][k]++;
  ll res = 0;
  for (int i = 0; i < dx.size() - 1; i++)
    for (int j = 0; j < dy.size() - 1; j++)
      for (int k = 0; k < dz.size() - 1; k++)
        if (d[i][j][k] >= K)
          res += (ll)(dx[i + 1] - dx[i]) * (dy[j + 1] - dy[j]) *
                 (dz[k + 1] - dz[k]);
  printf("%lld\\n", res);
}