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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
<url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0509>
?????????============================================================
?????¢???????????¢??????????????????????????????????????????.???????????????????????§????????????????????¨????????¢????????¨???????±???????????????°?????????????????????.
?????????, ?????¢?????§?¨??????¢??¨??????????????¨???,
??????????????????????¬?????????¶ (1), (2) ????????????????????¨??????. (1)
???????????????????????¢??? 4 ????????? x, y ??§?¨??????¨??? 0 ??\\??? 10000
??\\????????´??°??§??????,
    ????????¢???????????? x ???, ????????? y ??????????????§??????.
(2) ?????????????????°????????? 10000 ?????\\?????§??????.
??\\???????????? ??? 1 ?????????????????¢????????° n
??¨??????????¨?????????¨?????´??° r ???????????§????????£?????????????????????.
2 ????????\\??????????????????, ?????????????????????????????§?¨? (x1, y1)
??¨??????????????§?¨? (x2, y2) ?????§?¨??????? x1, y1, x2, y2
????????§????????§????????£??????????????????.

???????????? r = 1 ?????¨??? 1 ???????????¢??????, r = 2 ?????¨??? 1
???????????¢???, 2 ???????????¨???????????????????????????.
??????????????´??????????????????????????\\???????????¨.

=================================================================

?§£??¬=============================================================

================================================================
*/

int m[2][10010];
vector<pll> ps[10010];
int maxh, maxw;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, r;
  while (cin >> N >> r, N | r) {
    maxh = -1, maxw = -1;
    memset(m, 0, sizeof(m));
    for (int i = 0; i < 10010; i++)
      ps[i].clear();
    for (int i = 0; i < N; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      x1++;
      y1++;
      x2++;
      y2++;
      ps[y1].push_back({x1, 1});
      ps[y1].push_back({x2, -1});
      ps[y2].push_back({x1, -1});
      ps[y2].push_back({x2, 1});
      maxh = max(maxh, y2);
      maxw = max(maxw, x2);
    }

    int Area = 0, Length = 0;
    int cur = 0, next = 1;
    for (int i = 0; i <= maxh + 1; i++) {
      memset(m[next], 0, sizeof(m[next]));
      for (auto p : ps[i]) {
        m[next][p.first] += p.second; // ?´??????????????????¶???
      }
      for (int j = 0; j <= maxw; j++) {
        m[next][j + 1] += m[next][j]; // ?´??????? ?¨?????????????
        m[next][j] += m[cur][j];      // ?´??????? ???????????????
      }
      for (int j = 0; j <= maxw; j++) {
        if (m[next][j] > 0)
          Area++;
        if ((m[cur][j] > 0) != (m[next][j] > 0))
          Length++;
        if ((m[next][j] > 0) != (m[next][j + 1] > 0))
          Length++;
      }
      cur = !cur;
      next = !next;
    }
    cout << Area << endl;
    if (r == 2)
      cout << Length << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
<url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0509>
?????????============================================================
?????¢???????????¢??????????????????????????????????????????.???????????????????????§????????????????????¨????????¢????????¨???????±???????????????°?????????????????????.
?????????, ?????¢?????§?¨??????¢??¨??????????????¨???,
??????????????????????¬?????????¶ (1), (2) ????????????????????¨??????. (1)
???????????????????????¢??? 4 ????????? x, y ??§?¨??????¨??? 0 ??\\??? 10000
??\\????????´??°??§??????,
    ????????¢???????????? x ???, ????????? y ??????????????§??????.
(2) ?????????????????°????????? 10000 ?????\\?????§??????.
??\\???????????? ??? 1 ?????????????????¢????????° n
??¨??????????¨?????????¨?????´??° r ???????????§????????£?????????????????????.
2 ????????\\??????????????????, ?????????????????????????????§?¨? (x1, y1)
??¨??????????????§?¨? (x2, y2) ?????§?¨??????? x1, y1, x2, y2
????????§????????§????????£??????????????????.

???????????? r = 1 ?????¨??? 1 ???????????¢??????, r = 2 ?????¨??? 1
???????????¢???, 2 ???????????¨???????????????????????????.
??????????????´??????????????????????????\\???????????¨.

=================================================================
??\\???=============================================================
??\\???????????°????????????????????????????????????n, r ?????¨?????? 0
?????¨?????\\????????????????????????????????????????????°??? 10
????¶?????????????
================================================================
??????=============================================================
???????????????????????¨???, r = 1 ?????¨??? 1 ???????????¢??????, r = 2
?????¨??? 1 ???????????¢???, 2 ???????????¨???????????????????????????.
================================================================
?§£??¬=============================================================

================================================================
*/

class CC {
public:
  ll N;
  vector<ll> xs; // ????????¨:xs[x1 or x2????????????] := ????????§?¨?

  CC(ll N) : N(N) {}
  // x1, x2?????§?¨???§??????, ??§?¨???§???????????????????????????
  int compress(vector<ll> &x1, vector<ll> &x2, ll MIN_W = -1e10,
               ll MAX_W = 1e10) {
    for (int i = 0; i < N; i++) {
      /* ??´???????????£??????d????¶???????????????? */
      for (int d = -1; d <= 1; d++) {
        ll tx1 = x1[i], tx2 = x2[i];
        if (MIN_W <= tx1 && tx1 <= MAX_W)
          xs.push_back(tx1);
        if (MIN_W <= tx2 && tx2 <= MAX_W)
          xs.push_back(tx2);
      }
    }

    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());

    for (int i = 0; i < N; i++) {
      x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
      x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
    }

    return (int)xs.size(); // xs.size????????§??§???6*N
  }
  void clear() { xs.clear(); }
  void set(int v) { N = v; }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, r;
  vector<ll> x1, y1, x2, y2;
  CC CCx(0), CCy(0);
  while (cin >> N >> r, N | r) {
    x1.clear();
    y1.clear();
    x2.clear();
    y2.clear();
    x1.resize(N);
    y1.resize(N);
    x2.resize(N);
    y2.resize(N);
    for (int i = 0; i < N; i++)
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    CCx.clear();
    CCx.set(N);
    CCy.clear();
    CCy.set(N);
    ll W = CCx.compress(x1, x2);
    ll H = CCy.compress(y1, y2);

    /* imos????????¨???????????§?¨??????¢????¨???????*/
    vector<vector<int>> imos(H + 2, vector<int>(W + 2, 0)); // ??¢????¨????
    vector<vector<int>> Sum(H + 2, vector<int>(W + 2, 0));
    for (int i = 0; i < N; i++) {
      imos[y1[i]][x1[i]]++;
      imos[y1[i]][x2[i]]--;
      imos[y2[i]][x1[i]]--;
      imos[y2[i]][x2[i]]++;
    }
    ll ans = 0;
    ll circuit = 0;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        Sum[y + 1][x + 1] =
            Sum[y + 1][x] + Sum[y][x + 1] - Sum[y][x] + imos[y][x];
        if (Sum[y + 1][x + 1] > 0) {
          ans += (CCy.xs[y + 1] - CCy.xs[y]) * (CCx.xs[x + 1] - CCx.xs[x]);
          circuit += (CCy.xs[y + 1] - CCy.xs[y]) + (CCx.xs[x + 1] - CCx.xs[x]);
        }
      }
    }
    cout << ans << endl;
    if (r == 1)
      continue;
  }
  return 0;
}