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

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

void compress(vector<int> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

inline int index(const vector<int> &v, int x) {
  return lower_bound(v.begin(), v.end(), x) - v.begin();
}

int main() {
  int n, r;
  while (scanf("%d%d", &n, &r), n) {
    vector<int> x1(n), y1(n), x2(n), y2(n);
    vector<int> xs(2), ys(2);
    xs.reserve(2 * n + 2);
    ys.reserve(2 * n + 2);
    xs[0] = ys[0] = -100000;
    xs[1] = ys[1] = 100000;

    for (int i = 0; i < n; ++i) {
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
      xs.push_back(x1[i]);
      xs.push_back(x2[i]);
      ys.push_back(y1[i]);
      ys.push_back(y2[i]);
    }

    compress(xs);
    compress(ys);

    vector<vector<int>> incs(xs.size()), decs = incs;
    for (int i = 0; i < n; ++i) {
      int xi1 = index(xs, x1[i]);
      int xi2 = index(xs, x2[i]);
      int yi1 = index(ys, y1[i]);
      int yi2 = index(ys, y2[i]);

      incs[xi1].push_back(yi1);
      decs[xi2].push_back(yi1);
      decs[xi1].push_back(yi2);
      incs[xi2].push_back(yi2);
    }

    int area = 0, len = 0;

    vector<int> prev(ys.size()), now = prev, next = prev;
    for (int i = 0; i + 1 < xs.size(); ++i) {
      fill(next.begin(), next.end(), 0);
      for (int j = 0; j < incs[i + 1].size(); ++j) {
        ++next[incs[i + 1][j]];
      }
      for (int j = 0; j < decs[i + 1].size(); ++j) {
        --next[decs[i + 1][j]];
      }
      for (int j = 1; j < next.size(); ++j) {
        next[j] += next[j - 1];
      }
      for (int j = 0; j < next.size(); ++j) {
        next[j] += now[j];
      }

      for (int j = 1; j + 1 < ys.size(); ++j) {
        if (now[j] == 0)
          continue;

        int dx = xs[i + 1] - xs[i];
        int dy = ys[j + 1] - ys[j];
        area += dx * dy;

        if (prev[j] == 0)
          len += dy;
        if (next[j] == 0)
          len += dy;
        if (now[j - 1] == 0)
          len += dx;
        if (now[j + 1] == 0)
          len += dx;
      }

      prev.swap(now);
      now.swap(next);
    }

    printf("%d\\n", area);
    if (r == 2)
      printf("%d\\n", len);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

void compress(vector<int> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

int index(const vector<int> &v, int x) {
  return lower_bound(v.begin(), v.end(), x) - v.begin();
}

int main() {
  int n, r;
  while (scanf("%d%d", &n, &r), n) {
    vector<int> x1(n), y1(n), x2(n), y2(n);
    vector<int> xs(2), ys(2);
    xs.reserve(2 * n + 2);
    ys.reserve(2 * n + 2);
    xs[0] = ys[0] = -100000;
    xs[1] = ys[1] = 100000;

    for (int i = 0; i < n; ++i) {
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
      xs.push_back(x1[i]);
      xs.push_back(x2[i]);
      ys.push_back(y1[i]);
      ys.push_back(y2[i]);
    }

    compress(xs);
    compress(ys);

    vector<vector<int>> imos(xs.size(), vector<int>(ys.size()));
    for (int i = 0; i < n; ++i) {
      int xi1 = index(xs, x1[i]);
      int xi2 = index(xs, x2[i]);
      int yi1 = index(ys, y1[i]);
      int yi2 = index(ys, y2[i]);

      ++imos[xi1][yi1];
      --imos[xi2][yi1];
      --imos[xi1][yi2];
      ++imos[xi2][yi2];
    }

    for (int i = 1; i < xs.size(); ++i)
      for (int j = 1; j < ys.size(); ++j) {
        imos[i][j] += imos[i][j - 1];
      }

    for (int i = 1; i < xs.size(); ++i)
      for (int j = 1; j < ys.size(); ++j) {
        imos[i][j] += imos[i - 1][j];
      }

    int area = 0, len = 0;
    for (int i = 1; i + 1 < xs.size(); ++i)
      for (int j = 1; j + 1 < ys.size(); ++j) {
        if (imos[i][j] == 0)
          continue;

        int dx = xs[i + 1] - xs[i];
        int dy = ys[j + 1] - ys[j];

        area += dx * dy;

        if (imos[i - 1][j] == 0)
          len += dy;
        if (imos[i + 1][j] == 0)
          len += dy;
        if (imos[i][j - 1] == 0)
          len += dx;
        if (imos[i][j + 1] == 0)
          len += dx;
      }

    printf("%d\\n", area);
    if (r == 2)
      printf("%d\\n", len);
  }
}