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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
double EPS = 1e-10;
double EQ(double a, double b) { return abs(a - b) < EPS; }
void fast_stream() {
  cin.tie(0);
  std::ios_base::sync_with_stdio(0);
}
template <class T> string IntToString(T num) {
  string res;
  stringstream ss;
  ss << num;
  return ss.str();
}
ll StringToInt(string &str) {
  ll res = 0;
  for (int i = 0; i < (int)str.size(); i++)
    res = (res * 10 + str[i] - '0');
  return res;
}

typedef pair<pii, int> ppii;

int N, R;
// bitset<10001*100001> field;
int field[2][10011];
vector<ppii> rangeSet[10010];
int line[10010];
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
const int INF = 1000000000;
int main() {
  fast_stream();
  while (cin >> N >> R && (N | R)) {
    int miny, minx, maxy, maxx;
    miny = minx = INF;
    maxx = maxy = -INF;
    for (int i = 0; i < 10001; i++)
      rangeSet[i].clear();
    memset(line, 0, sizeof(line));
    memset(field, 0, sizeof(field));
    for (int i = 0; i < N; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      rangeSet[y1].push_back(make_pair(pii(x1, x2), 1));
      rangeSet[y2].push_back(make_pair(pii(x1, x2), -1));
      maxy = max(y2 + 1, maxy);
      maxx = max(x2, maxx);
      miny = min(y1, miny);
      minx = min(x1, minx);
    }
    int S = 0;
    int E = 0;
    for (int y = miny; y < maxy; y++) {
      int cur = y % 2;
      int prv = cur ^ 1;
      memset(field[cur], 0, sizeof(field[cur]));
      for (int i = 0; i < (int)rangeSet[y].size(); i++) {
        int sx = rangeSet[y][i].first.first;
        int ex = rangeSet[y][i].first.second;
        int a = rangeSet[y][i].second;
        for (int j = sx; j < ex; j++)
          line[j] += a;
      }
      for (int x = minx; x < maxx; x++) {
        if (line[x] > 0) {
          S++;
          field[cur][x] = 1;
        }
      }
      for (int x = minx; x < maxx; x++) {
        if (field[prv][x] ^ field[cur][x])
          E++;
        if (field[cur][x] == 1) {
          if (x - 1 < 0 || field[cur][x - 1] == 0)
            E++;
          if (field[cur][x + 1] == 0)
            E++;
        }
      }
      // if(y==maxy-2)cout<<E<<endl;
    }
    // int S=field.count();
    cout << S << endl;
    if (R == 2)
      cout << E << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
double EPS = 1e-10;
double EQ(double a, double b) { return abs(a - b) < EPS; }
void fast_stream() {
  cin.tie(0);
  std::ios_base::sync_with_stdio(0);
}
template <class T> string IntToString(T num) {
  string res;
  stringstream ss;
  ss << num;
  return ss.str();
}
ll StringToInt(string &str) {
  ll res = 0;
  for (int i = 0; i < (int)str.size(); i++)
    res = (res * 10 + str[i] - '0');
  return res;
}

typedef pair<pii, int> ppii;

int N, R;
bitset<10001 * 100001> field;
vector<ppii> rangeSet[10010];
int line[10010];
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
const int INF = 1000000000;
int main() {
  fast_stream();
  while (cin >> N >> R && (N | R)) {
    int miny, minx, maxy, maxx;
    miny = minx = INF;
    maxx = maxy = -INF;
    for (int i = 0; i < 10001; i++)
      rangeSet[i].clear();
    field.reset();
    memset(line, 0, sizeof(line));
    for (int i = 0; i < N; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      rangeSet[y1].push_back(make_pair(pii(x1, x2), 1));
      rangeSet[y2].push_back(make_pair(pii(x1, x2), -1));
      maxy = max(y2 + 1, maxy);
      maxx = max(x2, maxx);
      miny = min(y1, miny);
      minx = min(x1, minx);
    }
    int S = 0;
    for (int y = miny; y < maxy; y++) {
      for (int i = 0; i < (int)rangeSet[y].size(); i++) {
        int sx = rangeSet[y][i].first.first;
        int ex = rangeSet[y][i].first.second;
        int a = rangeSet[y][i].second;
        for (int j = sx; j < ex; j++)
          line[j] += a;
      }
      for (int x = minx; x < maxx; x++)
        if (line[x] > 0) {
          S++;
          field[y * 10001 + x] = 1;
        }
    }
    // int S=field.count();
    int E = 0;
    for (int i = miny; i < maxy; i++) {
      for (int j = minx; j < maxx; j++) {
        if (field[i * 10001 + j] == 0)
          continue;
        for (int k = 0; k < 4; k++) {
          int ny = i + dy[k];
          int nx = j + dx[k];
          if (ny >= 0 && nx >= 0 && ny <= 10000 && nx <= 10000) {
            if (field[ny * 10001 + nx] == 0) {
              E++;
            }
          } else {
            E++;
          }
        }
      }
    }
    cout << S << endl;
    if (R == 2)
      cout << E << endl;
  }
  return 0;
}