Problem p00432 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int dp[4001], p[4001];

typedef pair<int, pair<int, int>> Event;

int main() {
  for (;;) {
    int n, r;
    cin >> n >> r;
    if (!(n || r))
      return 0;
    map<int, vector<Event>> events;
    int mx = 0, c = 0, area = 0, circumference = 0;
    for (int i = 0; i < n; i++) {
      int sx, sy, ex, ey;
      cin >> sx >> sy >> ex >> ey;
      events[sy].push_back(make_pair(1, make_pair(sx, ex)));
      events[ey].push_back(make_pair(-1, make_pair(sx, ex)));
      mx = max(mx, ex);
    }
    fill(dp, dp + mx + 1, 0);
    fill(p, p + mx + 1, 0);
    for (map<int, vector<Event>>::iterator v = events.begin();
         v != events.end(); ++v) {
      int count = 0, a = 0, b = 0;
      for (int i = 0; i < mx; i++) {
        if (dp[i]) {
          count++;
          if (dp[i + 1])
            a++;
          if (p[i])
            b++;
        }
        p[i] = dp[i];
      }
      circumference += ((v->first - c) * (count - a) + count - b) * 2;
      area += (v->first - c) * count;
      c = v->first;
      for (vector<Event>::iterator e = v->second.begin(); e != v->second.end();
           ++e)
        for (int i = e->second.first; i < e->second.second; i++)
          dp[i] += e->first;
    }
    /*
    fill(dp, dp+mx, 0);
    for (map<int, vector<Event> >::iterator v = events.begin();
         v != events.end(); ++v) {
      int count = 0;
      for (int i = 0; i < mx; i++) if (dp[i]) count++;
      area += (v->first - c) * count;
      c = v->first;
      for (vector<Event>::iterator e = v->second.begin();
           e != v->second.end(); ++e)
        for (int i = e->second.first; i < e->second.second; i++)
          dp[i] += e->first;
      for (int i = 0; i < mx; i++)
        cout << dp[i] << ' ';
      cout << endl;
    }
    */
    cout << area << endl;
    if (r == 2)
      cout << circumference << endl;
  }
}

Fixed submission:
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int dp[10001], p[10001];

typedef pair<int, pair<int, int>> Event;

int main() {
  for (;;) {
    int n, r;
    cin >> n >> r;
    if (!(n || r))
      return 0;
    map<int, vector<Event>> events;
    int mx = 0, c = 0, area = 0, circumference = 0;
    for (int i = 0; i < n; i++) {
      int sx, sy, ex, ey;
      cin >> sx >> sy >> ex >> ey;
      events[sy].push_back(make_pair(1, make_pair(sx, ex)));
      events[ey].push_back(make_pair(-1, make_pair(sx, ex)));
      mx = max(mx, ex);
    }
    fill(dp, dp + mx + 1, 0);
    fill(p, p + mx + 1, 0);
    for (map<int, vector<Event>>::iterator v = events.begin();
         v != events.end(); ++v) {
      int count = 0, a = 0, b = 0;
      for (int i = 0; i < mx; i++) {
        if (dp[i]) {
          count++;
          if (dp[i + 1])
            a++;
          if (p[i])
            b++;
        }
        p[i] = dp[i];
      }
      circumference += ((v->first - c) * (count - a) + count - b) * 2;
      area += (v->first - c) * count;
      c = v->first;
      for (vector<Event>::iterator e = v->second.begin(); e != v->second.end();
           ++e)
        for (int i = e->second.first; i < e->second.second; i++)
          dp[i] += e->first;
    }
    /*
    fill(dp, dp+mx, 0);
    for (map<int, vector<Event> >::iterator v = events.begin();
         v != events.end(); ++v) {
      int count = 0;
      for (int i = 0; i < mx; i++) if (dp[i]) count++;
      area += (v->first - c) * count;
      c = v->first;
      for (vector<Event>::iterator e = v->second.begin();
           e != v->second.end(); ++e)
        for (int i = e->second.first; i < e->second.second; i++)
          dp[i] += e->first;
      for (int i = 0; i < mx; i++)
        cout << dp[i] << ' ';
      cout << endl;
    }
    */
    cout << area << endl;
    if (r == 2)
      cout << circumference << endl;
  }
}