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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

class Compress {
public:
  vector<long long> v;
  map<long long, long long> rev;

  // ??§?¨???§?????????
  // vec????????¨????????±??????????????????????????????????????????????´???????????????????
  Compress(vector<long long> vec) {
    this->v = vec;
    sort(this->v.begin(), this->v.end());
    this->v.erase(unique(this->v.begin(), this->v.end()), this->v.end());

    for (long long x : v) {
      rev[this->zip(x)] = x;
    }
  }

  long long zip(long long x) {
    return lower_bound(ALL(this->v), x) - this->v.begin();
  }

  long long unzip(long long x) { return this->rev[x]; }
};

int main(int argc, char *argv[]) {
  LL N, X1, Y1, X2, Y2;

  cin >> N;
  vector<LL> allX(N), allY(N);
  vector<LL> x1(N), y1(N), x2(N), y2(N);
  FOR(i, 0, N) {
    cin >> X1 >> Y1 >> X2 >> Y2;
    x1[i] = X1;
    y1[i] = Y1;
    x2[i] = X2;
    y2[i] = Y2;

    allX.push_back(X1);
    allX.push_back(X2);
    allY.push_back(Y1);
    allY.push_back(Y2);
  }
  allX.push_back(1000000001);
  allY.push_back(1000000001);

  // ??§?¨???§???
  Compress cx(allX);
  Compress cy(allY);

  vector<vector<int>> field(5100, vector<int>(5100, 0));
  FOR(i, 0, N) {
    field[cy.zip(y1[i])][cx.zip(x1[i])]++;
    field[cy.zip(y2[i])][cx.zip(x2[i])]++;
    field[cy.zip(y1[i])][cx.zip(x2[i])]--;
    field[cy.zip(y2[i])][cx.zip(x1[i])]--;
  }

  // ?´???????
  FOR(y, 0, field.size() - 1) {
    FOR(x, 1, field[0].size() - 1) { field[y][x] += field[y][x - 1]; }
  }
  FOR(x, 0, field[0].size()) {
    FOR(y, 1, field.size()) { field[y][x] += field[y - 1][x]; }
  }

  LL area = 0;
  FOR(y, 0, field.size() - 1) {
    FOR(x, 0, field[0].size() - 1) {
      if (field[y][x]) {
        area +=
            (cx.unzip(x + 1) - cx.unzip(x)) * (cy.unzip(y + 1) - cy.unzip(y));
      }
    }
  }

  cout << area << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

class Compress {
public:
  vector<long long> v;
  map<long long, long long> rev;

  // ??§?¨???§?????????
  // vec????????¨????????±??????????????????????????????????????????????´???????????????????
  Compress(vector<long long> vec) {
    this->v = vec;
    sort(this->v.begin(), this->v.end());
    this->v.erase(unique(this->v.begin(), this->v.end()), this->v.end());

    for (long long x : v) {
      rev[this->zip(x)] = x;
    }
  }

  long long zip(long long x) {
    return lower_bound(ALL(this->v), x) - this->v.begin();
  }

  long long unzip(long long x) { return this->rev[x]; }
};

int main(int argc, char *argv[]) {
  LL N, X1, Y1, X2, Y2;

  cin >> N;
  vector<LL> allX(N), allY(N);
  vector<LL> x1(N), y1(N), x2(N), y2(N);
  FOR(i, 0, N) {
    cin >> X1 >> Y1 >> X2 >> Y2;
    x1[i] = X1;
    y1[i] = Y1;
    x2[i] = X2;
    y2[i] = Y2;
    for (int d = 0; d <= 1; ++d) {
      allX.push_back(X1 + d);
      allX.push_back(X2 + d);
      allY.push_back(Y1 + d);
      allY.push_back(Y2 + d);
    }
  }
  allX.push_back(1000000001);
  allY.push_back(1000000001);

  // ??§?¨???§???
  Compress cx(allX);
  Compress cy(allY);

  vector<vector<int>> field(12001, vector<int>(12001, 0));
  FOR(i, 0, N) {
    std::cout << cy.zip(y1[i]) << " " << cx.zip(x1[i]) << " " << cy.zip(y2[i])
              << " " << cx.zip(x2[i]) << std::endl;
    field[cy.zip(y1[i])][cx.zip(x1[i])]++;
    field[cy.zip(y2[i])][cx.zip(x2[i])]++;
    field[cy.zip(y1[i])][cx.zip(x2[i])]--;
    field[cy.zip(y2[i])][cx.zip(x1[i])]--;
  }

  // ?´???????
  FOR(y, 0, field.size() - 1) {
    FOR(x, 1, field[0].size() - 1) { field[y][x] += field[y][x - 1]; }
  }
  FOR(x, 0, field[0].size()) {
    FOR(y, 1, field.size()) { field[y][x] += field[y - 1][x]; }
  }

  LL area = 0;
  FOR(y, 0, field.size() - 1) {
    FOR(x, 0, field[0].size() - 1) {
      if (field[y][x]) {
        area +=
            (cx.unzip(x + 1) - cx.unzip(x)) * (cy.unzip(y + 1) - cy.unzip(y));
      }
    }
  }

  cout << area << endl;
}