Problem p00432 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
int n, r;
vector<P> x[10002];
vector<P> xx[10002];
int main(void) {
  while (cin >> n >> r && n) {
    for (int i = 0; i < 10002; i++) {
      x[i].clear();
      xx[i].clear();
    }

    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      for (int j = x1; j < x2; j++) {
        bool f = true;
        for (int k = 0; k < (int)x[j].size(); k++) {
          if (x[j][k].F <= y2 && x[j][k].S >= y1) {
            x[j][k].F = min(x[j][k].F, y1);
            x[j][k].S = max(x[i][k].S, y2);
            f = false;
            break;
          }
        }
        if (f)
          x[j].push_back(P(y1, y2));
      }
    }

    for (int i = 0; i < 10000; i++) {
      if (x[i].empty())
        continue;
      sort(x[i].begin(), x[i].end());
      xx[i].push_back(x[i][0]);
      for (int j = 1, k = 0; j < (int)x[i].size(); j++) {
        if (xx[i][k].S >= x[i][j].F) {
          xx[i][k].S = max(xx[i][k].S, x[i][j].S);
        } else {
          xx[i].push_back(x[i][j]);
          k++;
        }
      }
      // x[i].clear();
      /*
      cout << "x = " << i << endl;
      for(int j = 0; j < (int)xx[i].size(); j++){
        cout << "( " << xx[i][j].F << " , " << xx[i][j].S << " )" << endl;
      }
      cout << endl;
      //*/
    }

    int res = 0;
    for (int i = 0; i < 10000; i++) {
      for (int j = 0; j < (int)xx[i].size(); j++) {
        res += xx[i][j].S - xx[i][j].F;
      }
    }
    cout << res << endl;

    if (r - 1) {
      res = 0;
      for (int i = 0; i < 10000; i++) {
        res += 2 * (int)xx[i].size();
        for (int j = 0; j < (int)xx[i].size(); j++) {
          res += 2 * (xx[i][j].S - xx[i][j].F);
          if (i) {
            for (int k = 0; k < (int)xx[i - 1].size(); k++) {
              if (xx[i][j].F < xx[i - 1][k].S && xx[i][j].S > xx[i - 1][k].F) {
                res -= min(xx[i][j].S, xx[i - 1][k].S) -
                       max(xx[i][j].F, xx[i - 1][k].F);
              }
            }
          }
          if (i + 1 < 10000) {
            for (int k = 0; k < (int)xx[i + 1].size(); k++) {
              if (xx[i][j].F < xx[i + 1][k].S && xx[i][j].S > xx[i + 1][k].F) {
                res -= min(xx[i][j].S, xx[i + 1][k].S) -
                       max(xx[i][j].F, xx[i + 1][k].F);
              }
            }
          }
        }
      }
      cout << res << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
int n, r;
vector<P> x[10002];
vector<P> xx[10002];
int main(void) {
  while (cin >> n >> r && n) {
    for (int i = 0; i < 10002; i++) {
      x[i].clear();
      xx[i].clear();
    }

    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      for (int j = x1; j < x2; j++) {
        bool f = true;
        for (int k = 0; k < (int)x[j].size(); k++) {
          if (x[j][k].F <= y2 && x[j][k].S >= y1) {
            x[j][k].F = min(x[j][k].F, y1);
            x[j][k].S = max(x[j][k].S, y2);
            f = false;
            break;
          }
        }
        if (f)
          x[j].push_back(P(y1, y2));
      }
    }

    for (int i = 0; i < 10000; i++) {
      if (x[i].empty())
        continue;
      sort(x[i].begin(), x[i].end());
      xx[i].push_back(x[i][0]);
      for (int j = 1, k = 0; j < (int)x[i].size(); j++) {
        if (xx[i][k].S >= x[i][j].F) {
          xx[i][k].S = max(xx[i][k].S, x[i][j].S);
        } else {
          xx[i].push_back(x[i][j]);
          k++;
        }
      }
      // x[i].clear();
      /*
      cout << "x = " << i << endl;
      for(int j = 0; j < (int)xx[i].size(); j++){
        cout << "( " << xx[i][j].F << " , " << xx[i][j].S << " )" << endl;
      }
      cout << endl;
      //*/
    }

    int res = 0;
    for (int i = 0; i < 10000; i++) {
      for (int j = 0; j < (int)xx[i].size(); j++) {
        res += xx[i][j].S - xx[i][j].F;
      }
    }
    cout << res << endl;

    if (r - 1) {
      res = 0;
      for (int i = 0; i < 10000; i++) {
        res += 2 * (int)xx[i].size();
        for (int j = 0; j < (int)xx[i].size(); j++) {
          res += 2 * (xx[i][j].S - xx[i][j].F);
          if (i) {
            for (int k = 0; k < (int)xx[i - 1].size(); k++) {
              if (xx[i][j].F < xx[i - 1][k].S && xx[i][j].S > xx[i - 1][k].F) {
                res -= min(xx[i][j].S, xx[i - 1][k].S) -
                       max(xx[i][j].F, xx[i - 1][k].F);
              }
            }
          }
          if (i + 1 < 10000) {
            for (int k = 0; k < (int)xx[i + 1].size(); k++) {
              if (xx[i][j].F < xx[i + 1][k].S && xx[i][j].S > xx[i + 1][k].F) {
                res -= min(xx[i][j].S, xx[i + 1][k].S) -
                       max(xx[i][j].F, xx[i + 1][k].F);
              }
            }
          }
        }
      }
      cout << res << endl;
    }
  }
}