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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef vector<int> PP;
typedef vector<PP> P;
int n, r;

struct block {
  int x1, x2, y, f;
};

block b[20002];
int fie[10002][2];

bool comp(const block &b1, const block &b2) { return b1.y < b2.y; }

int main(void) {
  while (1) {
    scanf("%d%d", &n, &r);
    if (n == 0 && r == 0)
      break;
    memset(fie, 0, sizeof(fie));
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; i++) {
      int y1, y2;
      scanf("%d%d%d%d", &b[i * 2].x1, &y1, &b[i * 2].x2, &y2);
      b[i * 2].x2--;
      b[i * 2 + 1].x1 = b[i * 2].x1;
      b[i * 2 + 1].x2 = b[i * 2].x2;
      b[i * 2].y = y1;
      b[i * 2 + 1].y = y2;
      b[i * 2].f = 1;
      b[i * 2 + 1].f = -1;
    }
    sort(b, b + n * 2, comp);
    int ny = 0;
    int now = 0, prev = 1;
    int res = 0, res2 = 0;
    for (int i = 0; i < n * 2; i++) {
      if (ny != b[i].y) {
        swap(now, prev);
        int cnt = 0;
        int cnt2 = 0, bt = 0, cnt3 = 0;
        for (int j = 0; j <= 10000; j++) {
          if (fie[j][now] > 0 && fie[j][prev] == 0)
            cnt3++;
          if (fie[j][now] == 0 && fie[j][prev] > 0)
            cnt3++;
          fie[j][now] = fie[j][prev];
          if (fie[j][now] > 0) {
            cnt++;
            if (bt == 0)
              cnt2++, bt = 1;
          } else
            bt = 0;
        }
        res += cnt * (b[i].y - ny);
        res2 += cnt2 * 2 * (b[i].y - ny) + cnt3;
      }
      ny = b[i].y;
      for (int j = b[i].x1; j <= b[i].x2; j++)
        fie[j][now] += b[i].f;
      // printf("%d\\n",res2);
    }
    printf("%d\\n", res);
    if (r == 2) {
      swap(now, prev);
      int cnt3 = 0;
      for (int j = 0; j <= 10000; j++) {
        if (fie[j][now] > 0 && fie[j][prev] == 0)
          cnt3++;
        if (fie[j][now] == 0 && fie[j][prev] > 0)
          cnt3++;
        fie[j][now] = fie[j][prev];
      }
      res2 += cnt3;
      printf("%d\\n", res2);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef vector<int> PP;
typedef vector<PP> P;
int n, r;
short x1[10001], x2[10001], y1[10001], y2[10001];
int d[2] = {
    1,
    -1,
};
short fie[5000][5000];
vector<short> vx;
vector<short> vy;

void zatu() {
  for (int i = 0; i < n; i++) {
    // for(int j=-1;j<=1;j++){
    vx.push_back(x1[i]);
    vx.push_back(x2[i]);
    vy.push_back(y1[i]);
    vy.push_back(y2[i]);
    //}
  }
  vx.push_back(0);
  vx.push_back(10001);
  vy.push_back(0);
  vy.push_back(10001);
  sort(vx.begin(), vx.end());
  sort(vy.begin(), vy.end());
  vx.erase(unique(vx.begin(), vx.end()), vx.end());
  vy.erase(unique(vy.begin(), vy.end()), vy.end());
  for (int i = 0; i < n; i++) {
    x1[i] = find(vx.begin(), vx.end(), x1[i]) - vx.begin();
    x2[i] = find(vx.begin(), vx.end(), x2[i]) - vx.begin();
    y1[i] = find(vy.begin(), vy.end(), y1[i]) - vy.begin();
    y2[i] = find(vy.begin(), vy.end(), y2[i]) - vy.begin();
  }
}

int main(void) {
  while (1) {
    scanf("%d%d", &n, &r);
    if (n == 0 && r == 0)
      break;
    vx.clear();
    vy.clear();
    memset(fie, 0, sizeof(fie));
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
      x1[i]++;
      y1[i]++;
      x2[i]++;
      y2[i]++;
    }
    zatu();
    for (int i = 0; i < n; i++) {
      fie[x1[i]][y1[i]]++;
      fie[x1[i]][y2[i]]--;
      fie[x2[i]][y1[i]]--;
      fie[x2[i]][y2[i]]++;
    }
    for (int i = 0; i <= vy.size(); i++) {
      for (int j = 1; j <= vx.size(); j++) {
        fie[j][i] += fie[j - 1][i];
      }
    }
    for (int i = 0; i <= vx.size(); i++) {
      for (int j = 1; j <= vy.size(); j++) {
        fie[j][i] += fie[j][i - 1];
      }
    }
    long long res = 0;
    for (int i = 0; i < vx.size(); i++) {
      for (int j = 0; j < vy.size(); j++) {
        if (fie[i][j] > 0)
          res += (long long)(vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]);
      }
    }

    cout << res << endl;
    if (r == 2) {
      for (int i = 1; i < vx.size(); i++) {
        for (int j = 1; j < vy.size(); j++) {
          if (fie[i][j] >= 1) {
            bool f = false;
            for (int k = -1; k <= 1; k++) {
              for (int l = -1; l <= 1; l++) {
                if (fie[i + k][j + l] == 0)
                  f = true;
              }
            }
            if (!f)
              fie[i][j] = -1;
          }
        }
      }
      /*
      for(int i=0;i<vx.size();i++){
              for(int j=0;j<vy.size();j++){
                      printf("%2d ",fie[i][j]);
              }
              printf("\\n");
      }
      */
      int res2 = 0;
      for (int i = 1; i <= vx.size(); i++) {
        for (int j = 1; j <= vy.size(); j++) {
          if (fie[i][j] >= 1) {
            int cnt = 0;

            for (int k = 0; k < 2; k++) {
              int nx = i + d[k];
              int ny = j + d[k];
              if (ny == 0 || ny == vy.size())
                res2 += vx[i + 1] - vx[i], cnt += vx[i + 1] - vx[i];
              else if (fie[i][ny] == 0)
                res2 += vx[i + 1] - vx[i], cnt += vx[i + 1] - vx[i];
              if (nx == 0 || nx == vx.size())
                res2 += vy[j + 1] - vy[j], cnt += vy[j + 1] - vy[j];
              else if (fie[nx][j] == 0)
                res2 += vy[j + 1] - vy[j], cnt += vy[j + 1] - vy[j];
            }
            fie[i][j] = cnt;
            if (fie[i][j] == 0)
              fie[i][j] = -1;
          }
        }
      }
      /*
      for(int i=0;i<vx.size();i++){
              for(int j=0;j<vy.size();j++){
                      printf("%2d ",fie[i][j]);
              }
              printf("\\n");
      }
      */
      printf("%d\\n", res2);
    }
  }
  return 0;
}