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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef pair<pair<int, int>, pair<int, int>> DATA;
DATA in[10000];

bool cmp1(const DATA &a, const DATA &b) { return a.first < b.first; }
bool cmp2(const DATA &a, const DATA &b) { return a.second < b.second; }
int main() {
  int n, q;
  while (cin >> n >> q && n) {
    vector<pair<int, int>> tr[10001], tc[10001];
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      in[i].first.first = x1;
      in[i].first.second = x2;
      in[i].second.first = y1;
      in[i].second.second = y2;
    }
    sort(in, in + n, cmp1);
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      x1 = in[i].first.first;
      x2 = in[i].first.second;
      y1 = in[i].second.first;
      y2 = in[i].second.second;
      for (int i = y1; i < y2; i++)
        tr[i].push_back(make_pair(x1, x2));
    }
    sort(in, in + n, cmp2);
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      x1 = in[i].first.first;
      x2 = in[i].first.second;
      y1 = in[i].second.first;
      y2 = in[i].second.second;
      if (q == 2)
        for (int i = x1; i < x2; i++)
          tc[i].push_back(make_pair(y1, y2));
    }
    int area = 0, perimeter = 0;
    // merge
    for (int i = 0; i < 10001; i++) {
      if (tr[i].size() == 0)
        continue;
      // sort(tr[i].begin(),tr[i].end());

      int s = tr[i][0].first, e = tr[i][0].second;
      for (int j = 1; j < tr[i].size(); j++) {
        int S = tr[i][j].first, E = tr[i][j].second;
        if (S <= e) {
          e = max(E, e);
        } else {
          area += e - s;
          perimeter += 2;
          s = S;
          e = E;
        }
      }
      perimeter += 2;
      area += e - s;
    }

    if (q == 2) {
      for (int i = 0; i < 10001; i++) {
        if (tc[i].size() == 0)
          continue;
        // sort(tc[i].begin(),tc[i].end());
        int s = tc[i][0].first, e = tc[i][0].second;
        for (int j = 1; j < tc[i].size(); j++) {
          int S = tc[i][j].first, E = tc[i][j].second;
          if (S <= e) {
            e = max(E, e);
          } else {
            // cerr << s << "|" << e << endl;
            perimeter += 2;
            // c[i].push_back( make_pair(s,e) );
            s = S;
            e = E;
          }
        }
        // cerr << s << "|" << e << endl;
        perimeter += 2;
        // c[i].push_back( make_pair(s,e) );
      }
    }
    if (q == 1) {
      cout << area << endl;
    } else {
      cout << area << endl;
      cout << perimeter << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, q;
  while (cin >> n >> q && n) {
    vector<pair<int, int>> tr[10001], tc[10001];

    int mm[100][100] = {0};
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      for (int i = y1; i < y2; i++)
        for (int j = x1; j < x2; j++)
          mm[i][j] = 1;
      for (int i = y1; i < y2; i++)
        tr[i].push_back(make_pair(x1, x2));
      if (q == 2)
        for (int i = x1; i < x2; i++)
          tc[i].push_back(make_pair(y1, y2));
    }
    int area = 0, perimeter = 0;
    // merge
    for (int i = 0; i < 10001; i++) {
      if (tr[i].size() == 0)
        continue;
      sort(tr[i].begin(), tr[i].end());
      int s = tr[i][0].first, e = tr[i][0].second;
      for (int j = 1; j < tr[i].size(); j++) {
        int S = tr[i][j].first, E = tr[i][j].second;
        if (S <= e) {
          e = max(E, e);
        } else {
          area += e - s;
          perimeter += 2;
          s = S;
          e = E;
        }
      }
      perimeter += 2;
      area += e - s;
    }

    if (q == 2) {
      for (int i = 0; i < 10001; i++) {
        if (tc[i].size() == 0)
          continue;
        sort(tc[i].begin(), tc[i].end());
        int s = tc[i][0].first, e = tc[i][0].second;
        for (int j = 1; j < tc[i].size(); j++) {
          int S = tc[i][j].first, E = tc[i][j].second;
          if (S <= e) {
            e = E;
          } else {
            // cerr << s << "|" << e << endl;
            perimeter += 2;
            // c[i].push_back( make_pair(s,e) );
            s = S;
            e = E;
          }
        }
        // cerr << s << "|" << e << endl;
        perimeter += 2;
        // c[i].push_back( make_pair(s,e) );
      }
    }
    if (q == 1) {
      cout << area << endl;
    } else {
      cout << area << endl;
      cout << perimeter << endl;
    }
  }
}