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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, r;
  while (cin >> n >> r, n || r) {
    vector<pair<pair<int, int>, pair<int, int>>> v;
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      v.emplace_back(make_pair(make_pair(x1, y1), make_pair(x2, y2)));
    }
    int s[10001] = {0};
    int p[10001] = {0};
    int edge = 0;
    for (int y = 0; y <= 10000; y++) {
      int w[10001] = {0};
      for (int unsigned j = 0; j < v.size(); j++) {
        if (v[j].first.second <= y && y < v[j].second.second) {
          w[v[j].first.first]++;
          w[v[j].second.first]--;
        }
      }
      int c = 0;
      for (int unsigned x = 0; x <= 10000; x++) {
        int px = c;
        c += w[x];
        if ((px > 0 && c == 0) || (px == 0 && c > 0)) {
          // cout << "left: ("  << x << "," << y << ")" << endl;
          edge++;
        }
        if ((c > 0 && p[x] == 0) || (c == 0 && p[x] > 0)) {
          // cout << "up: ("  << x << "," << y << ")" << endl;
          edge++;
        }
        p[x] = c;
        if (c)
          s[x]++;
      }
    }
    int area = 0;
    for (int x = 0; x <= 10000; x++) {
      area += s[x];
    }
    cout << area << endl;
    if (r == 2) {
      cout << edge << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define at(x, y) (x * 10000 + y)
#define pos_X(x) (x / 10000)
#define pos_Y(y) (y % 10000)
struct rect {
  int x1, y1, x2, y2;
};

using namespace std;

int main() {
  int n, r;
  while (cin >> n >> r, n || r) {
    vector<int> xpos;
    vector<int> ypos;
    queue<pair<int, int>> q;
    unordered_multimap<int, int> m;
    for (int i = 0; i < n; i++) {
      rect A;
      cin >> A.x1 >> A.y1 >> A.x2 >> A.y2;
      // m.insert(at(A.x1,A.y1),at(A.x2,A.y2));
      q.push(make_pair(at(A.x1, A.y1), at(A.x2, A.y2)));
      xpos.emplace_back(A.x1);
      xpos.emplace_back(A.x2);
      ypos.emplace_back(A.y1);
      ypos.emplace_back(A.y2);
    }

    sort(xpos.begin(), xpos.end());
    sort(ypos.begin(), ypos.end());
    unordered_map<int, int> X_next;
    unordered_map<int, int> Y_next;
    for (int unsigned i = 0; i < xpos.size() - 1; i++) {
      X_next[xpos[i]] = xpos[i + 1];
    }
    for (int unsigned i = 0; i < ypos.size() - 1; i++) {
      Y_next[ypos[i]] = ypos[i + 1];
    }
    set<pair<pair<int, int>, pair<int, int>>> s;
    while (!q.empty()) {
      auto u = q.front();
      q.pop();
      int x1 = pos_X(u.first);
      int y1 = pos_Y(u.first);
      int x2 = pos_X(u.second);
      int y2 = pos_Y(u.second);
      if (X_next[x1] < x2 && Y_next[y1] < y2) {
        q.push(make_pair(at(X_next[x1], y1), at(x2, Y_next[y1])));
        q.push(make_pair(at(x1, Y_next[y1]), at(X_next[x1], y2)));
        q.push(make_pair(at(X_next[x1], Y_next[y1]), at(x2, y2)));
      } else if (X_next[x1] < x2) {
        q.push(make_pair(at(X_next[x1], y1), at(x2, Y_next[y1])));
      } else if (Y_next[y1] < y2) {
        q.push(make_pair(at(x1, Y_next[y1]), at(X_next[x1], y2)));
      }
      if (x1 == x2 || y1 == y2)
        continue;
      s.insert(make_pair(make_pair(x1, y1), make_pair(X_next[x1], Y_next[y1])));
    }
    int sum = 0;
    for (auto x : s) {
      // cout << x.first.first << " " << x.first.second << " " << x.second.first
      // << " " << x.second.second << endl;
      sum +=
          (x.second.first - x.first.first) * (x.second.second - x.first.second);
    }
    cout << sum << endl;
    if (r == 2)
      cout << "0" << endl;
  }

  return 0;
}