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

// 39
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int n, r; cin >> n >> r, n | r;) {
    vector<vector<pair<int, int>>> v;
    for (int i = 0; i < n; i++) {
      int x, y, xx, yy;
      cin >> x >> y >> xx >> yy;
      if (v.size() < yy) {
        v.resize(yy);
      }
      for (int j = y; j < yy; j++) {
        pair<int, int> p = make_pair(x, xx);
        int ix = lower_bound(v[j].begin(), v[j].end(), p) - v[j].begin();
        v[j].insert(v[j].begin() + ix, p);
        int m = xx;
        int k;
        for (k = ix + 1; k < v[j].size() && v[j][k].first <= m; k++) {
          m = max(m, v[j][k].second);
        }
        v[j][ix].second = m;
        v[j].erase(v[j].begin() + ix + 1, v[j].begin() + k);
        if (ix && v[j][ix - 1].second >= v[j][ix].first) {
          v[j][ix - 1].second = max(v[j][ix - 1].second, v[j][ix].second);
          v[j].erase(v[j].begin() + ix);
        }
      }
    }
    int a = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v[i].size(); j++) {
        a += v[i][j].second - v[i][j].first;
      }
    }
    cout << a << endl;
    if (r == 2) {
      int l = 0;
      bool b[2][10000] = {};
      for (int i = 0; i < v.size(); i++) {
        l += v[i].size() * 2;
        fill(b[i & 1], b[(i & 1) + 1], false);
        for (int j = 0; j < v[i].size(); j++) {
          for (int k = v[i][j].first; k < v[i][j].second; k++) {
            b[i & 1][k] = true;
          }
        }
        for (int j = 0; j < 10000; j++) {
          l += b[i & 1][j] ^ b[!(i & 1)][j];
        }
      }
      l += count(b[v.size() - 1 & 1], b[(v.size() - 1 & 1) + 1], true);
      cout << l << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 39
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int main() {
  for (int n, r; cin >> n >> r, n | r;) {
    vector<vector<pair<int, int>>> v;
    for (int i = 0; i < n; i++) {
      int x, y, xx, yy;
      cin >> x >> y >> xx >> yy;
      if (v.size() < yy) {
        v.resize(yy);
      }
      for (int j = y; j < yy; j++) {
        v[j].push_back(make_pair(x, xx));
      }
    }
    for (int i = 0; i < v.size(); i++) {
      sort(v[i].begin(), v[i].end());
      for (int j = 0; j < v[i].size(); j++) {
        int m = v[i][j].second;
        int k;
        for (k = j + 1; k < v[i].size() && v[i][k].first <= m; k++) {
          m = max(m, v[i][k].second);
        }
        v[i][j].second = m;
        v[i].erase(v[i].begin() + j + 1, v[i].begin() + k);
      }
    }
    int a = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v[i].size(); j++) {
        a += v[i][j].second - v[i][j].first;
      }
    }
    cout << a << endl;
    if (r == 2) {
      int l = 0;
      bool b[2][10000] = {};
      for (int i = 0; i < v.size(); i++) {
        l += v[i].size() * 2;
        fill(b[i & 1], b[(i & 1) + 1], false);
        for (int j = 0; j < v[i].size(); j++) {
          for (int k = v[i][j].first; k < v[i][j].second; k++) {
            b[i & 1][k] = true;
          }
        }
        for (int j = 0; j < 10000; j++) {
          l += b[i & 1][j] ^ b[!(i & 1)][j];
        }
      }
      l += count(b[v.size() - 1 & 1], b[(v.size() - 1 & 1) + 1], true);
      cout << l << endl;
    }
  }
  return 0;
}