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

#include <algorithm>
#include <iostream>
using namespace std;
int n, t, ax[10000], ay[10000], bx[10000], by[10000], J[3][10007];
int mx, my;
pair<int, int> solve(int row) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < my + 4; j++)
      J[i][j] = 0;
  }
  for (int i = 0; i < 3; i++) {
    int Plus = i - 1 + row;
    for (int j = 0; j < n; j++) {
      if (ax[j] <= Plus && Plus < bx[j]) {
        J[i][ay[j]]++;
        J[i][by[j]]--;
      }
    }
    for (int j = 1; j < my + 4; j++)
      J[i][j] += J[i][j - 1];
    for (int j = 1; j < my + 4; j++) {
      if (J[i][j] >= 1)
        J[i][j] = 1;
    }
  }
  int sum1 = 0;
  for (int i = 0; i < my + 4; i++)
    sum1 += J[1][i];
  int sum2 = 0;
  for (int i = 0; i < my + 4; i++) {
    if (i >= 1 && J[1][i] + J[1][i - 1] == 1) {
      sum2++;
    }
    if (i < my + 3 && J[1][i] + J[1][i + 1] == 1) {
      sum2++;
    }
  }
  for (int i = 0; i < my + 4; i++) {
    if (J[0][i] + J[1][i] == 1)
      sum2++;
    if (J[1][i] + J[2][i] == 1)
      sum2++;
  }
  return make_pair(sum1, sum2);
}
int main() {
  while (true) {
    cin >> n >> t;
    if (n == 0 && t == 0)
      break;
    my = 0;
    mx = 0;
    for (int i = 0; i < n; i++) {
      cin >> ax[i] >> ay[i] >> bx[i] >> by[i];
      ax[i]++;
      ay[i]++;
      bx[i]++;
      by[i]++;
    }
    for (int i = 0; i < n; i++) {
      mx = max(mx, bx[i]);
      my = max(my, by[i]);
    }
    int s1 = 0, s2 = 0;
    for (int i = 0; i <= mx + 3; i++) {
      pair<int, int> F = solve(i);
      s1 += F.first;
      s2 += F.second;
    }
    if (t == 1)
      cout << s1 << endl;
    if (t == 2)
      cout << s1 << endl << s2 / 2 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, t, ax[10000], ay[10000], bx[10000], by[10000], J[3][10004];
pair<int, int> solve(int row) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 10004; j++)
      J[i][j] = 0;
  }
  for (int i = 0; i < 3; i++) {
    int Plus = i - 1 + row;
    for (int j = 0; j < n; j++) {
      if (ax[j] <= Plus && Plus < bx[j]) {
        J[i][ay[j]]++;
        J[i][by[j]]--;
      }
    }
    for (int j = 1; j < 10004; j++)
      J[i][j] += J[i][j - 1];
    for (int j = 1; j < 10004; j++) {
      if (J[i][j] >= 1)
        J[i][j] = 1;
    }
  }
  int sum1 = 0;
  for (int i = 0; i < 10004; i++)
    sum1 += J[1][i];
  int sum2 = 0;
  for (int i = 0; i < 10004; i++) {
    if (i >= 1 && J[1][i] + J[1][i - 1] == 1) {
      sum2++;
    }
    if (i < 10003 && J[1][i] + J[1][i + 1] == 1) {
      sum2++;
    }
  }
  for (int i = 0; i < 10004; i++) {
    if (J[0][i] + J[1][i] == 1)
      sum2++;
    if (J[1][i] + J[2][i] == 1)
      sum2++;
  }
  return make_pair(sum1, sum2);
}
int main() {
  while (true) {
    cin >> n >> t;
    if (n == 0 && t == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> ax[i] >> ay[i] >> bx[i] >> by[i];
      ax[i]++;
      ay[i]++;
      bx[i]++;
      by[i]++;
    }
    int s1 = 0, s2 = 0;
    for (int i = 0; i <= 10003; i++) {
      pair<int, int> F = solve(i);
      s1 += F.first;
      s2 += F.second;
    }
    if (t == 1)
      cout << s1 << endl;
    if (t == 2)
      cout << s1 << endl << s2 / 2 << endl;
  }
  return 0;
}