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

#include <bits/stdc++.h>
using namespace std;

class imos2 {
public:
  void addInc(long long int x, long long int y) {
    inc.emplace_back(make_pair(x, y));
    incsortflag = false;
  }

  void addDec(long long int x, long long int y) {
    dec.emplace_back(make_pair(x, y));
    decsortflag = false;
  }

  pair<long long int, long long int> ans(long long int R) {
    pair<long long int, long long int> ret = make_pair(0, 0);
    if (!incsortflag) {
      for (auto now : inc) {
        incymax = max(incymax, now.second);
        incymin = min(incymin, now.second);
      }
      sort(inc.begin(), inc.end());
      incsortflag = true;
    }
    if (!decsortflag) {
      for (auto now : dec) {
        decymax = max(decymax, now.second);
        decymin = min(decymin, now.second);
      }
      sort(dec.begin(), dec.end());
      decsortflag = true;
    }
    long long int ymin = min(incymin, decymin), ymax = max(incymax, decymax);
    vector<long long int> tmp1(ymax - ymin + 1);
    vector<long long int> tmp2(ymax - ymin + 1);
    long long int incidx = 0, decidx = 0;
    for (long long int i = inc[0].first; i <= dec[dec.size() - 1].first; i++) {
      while (incidx < inc.size() && inc[incidx].first == i &&
             inc[incidx].second <= ymax) {
        tmp2[inc[incidx].second - ymin]++;
        incidx++;
      }
      while (decidx < dec.size() && dec[decidx].first == i &&
             dec[decidx].second <= ymax) {
        tmp2[dec[decidx].second - ymin]--;
        decidx++;
      }
      for (long long int j = 0; j < ymax - ymin; j++) {
        tmp2[j + 1] += tmp2[j];
      }
      for (long long int j = 0; j < ymax - ymin + 1; j++) {
        tmp2[j] += tmp1[j];
        // cout << setw( 2 ) << tmp2[j];
        if (tmp2[j]) {
          ret.first++;
        }
        if (R == 2) {
          if ((!tmp2[j]) != (!tmp1[j])) {
            ret.second++;
          }
          if (j > 0 && (!tmp2[j]) != (!tmp2[j - 1])) {
            ret.second++;
          }
        }
      }
      if (tmp2[0]) {
        ret.second++;
      }
      tmp1 = tmp2;
      tmp2 = vector<long long int>(ymax - ymin + 1);
    }
    return ret;
  }

  vector<pair<long long int, long long int>> inc;
  vector<pair<long long int, long long int>> dec;

  bool incsortflag;
  bool decsortflag;

  long long int incymax = LLONG_MIN, decymax = LLONG_MIN;
  long long int incymin = LLONG_MAX, decymin = LLONG_MAX;
};

int main() {
  long long int N, R;
  while (cin >> N >> R, N || R) {
    imos2 imos;
    for (size_t i = 0; i < N; i++) {
      long long int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      imos.addInc(x1, y1);
      imos.addInc(x2, y2);
      imos.addDec(x1, y2);
      imos.addDec(x2, y1);
    }
    auto memo = imos.ans(R);
    if (R == 1) {
      cout << memo.first << endl;
    } else {
      cout << memo.first << endl << memo.second << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int datax[10001][10001];
int main() {
  int N, R;
  while (cin >> N >> R, N || R) {
    memset(datax, 0, sizeof(datax));
    int mx = 10001, Mx = 1, my = 10001, My = 1;
    for (size_t i = 0; i < N; i++) {
      int x1, x2, y1, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      datax[x1][y1]++;
      datax[x2][y2]++;
      datax[x1][y2]--;
      datax[x2][y1]--;
      mx = min(mx, x1);
      Mx = max(Mx, x2);
      my = min(my, y1);
      My = max(My, y2);
    }

    for (size_t i = mx; i <= Mx; i++) {
      for (size_t j = my; j <= My; j++) {
        datax[i + 1][j] += datax[i][j];
      }
    }
    for (size_t j = my; j <= My; j++) {
      for (size_t i = mx; i <= Mx; i++) {
        datax[i][j + 1] += datax[i][j];
      }
    }
    long long int ans = 0;

    for (size_t i = mx; i <= Mx; i++) {
      for (size_t j = my; j <= My; j++) {
        if (datax[i][j]) {
          ans++;
          // cout << i << " " << j << " " << datax[i][j] << endl;
        }
      }
    }
    cout << ans << endl;
    ans = 0;
    if (R == 2) {
      for (size_t i = mx; i <= Mx; i++) {
        if (datax[i][0] != 0) {
          ans++;
        }
      }
      for (size_t i = my; i <= My; i++) {
        if (datax[0][i] != 0) {
          ans++;
        }
      }
      for (size_t i = mx; i <= Mx; i++) {
        for (size_t j = my; j <= My; j++) {
          if (datax[i][j] * datax[i][j + 1] == 0 &&
              datax[i][j] != datax[i][j + 1]) {
            ans++;
          }
          if (datax[i][j] * datax[i + 1][j] == 0 &&
              datax[i][j] != datax[i + 1][j]) {
            ans++;
          }
        }
      }
      cout << ans << endl;
    }
  }
}