Problem p00432 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int n, r;
    cin >> n >> r;
    if (n == 0)
      return 0;

    int sy = INT_MAX;
    int sx = INT_MAX;
    int gy = INT_MIN;
    int gx = INT_MIN;
    vector<int> x1(n), y1(n), x2(n), y2(n);
    for (int i = 0; i < n; ++i) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      sy = min(sy, y1[i]);
      sx = min(sx, x1[i]);
      gy = max(gy, y2[i]);
      gx = max(gx, x2[i]);
    }

    int ret1 = 0;
    int ret2 = 0;
    for (int a = 0; a < 11; ++a) {
      for (int b = 0; b < 11; ++b) {
        int yMin = a * 1000 - 1;
        int yMax = (a + 1) * 1000 - 1;
        int xMin = b * 1000 - 1;
        int xMax = (b + 1) * 1000 - 1;

        if (gy < yMin || gx < xMin || sy > yMax || sx > yMax)
          continue;

        vector<vector<int>> s(1001, vector<int>(1001, 0));
        for (int i = 0; i < n; ++i) {
          if (y2[i] < yMin || x2[i] < xMin || y1[i] > yMax || x1[i] > xMax)
            continue;

          ++s[max(yMin, y1[i]) - yMin][max(xMin, x1[i]) - xMin];
          if (x2[i] <= xMax)
            --s[max(yMin, y1[i]) - yMin][x2[i] - xMin];
          if (y2[i] <= yMax)
            --s[y2[i] - yMin][max(xMin, x1[i]) - xMin];
          if (y2[i] <= yMax && x2[i] <= xMax)
            ++s[y2[i] - yMin][x2[i] - xMin];
        }

        for (int i = 0; i < 1001; ++i) {
          for (int j = 0; j < 1000; ++j) {
            s[i][j + 1] += s[i][j];
          }
        }
        for (int i = 0; i < 1001; ++i) {
          for (int j = 0; j < 1000; ++j) {
            s[j + 1][i] += s[j][i];
          }
        }

        for (int i = 0; i < 1000; ++i) {
          for (int j = 0; j < 1000; ++j) {
            if (s[i][j] > 0)
              ++ret1;
            if ((s[i][j] > 0) ^ (s[i + 1][j] > 0))
              ++ret2;
            if ((s[i][j] > 0) ^ (s[i][j + 1] > 0))
              ++ret2;
          }
        }
      }
    }

    cout << ret1 << endl;
    if (r == 2)
      cout << ret2 << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int n, r;
    cin >> n >> r;
    if (n == 0)
      return 0;

    int sy = INT_MAX;
    int sx = INT_MAX;
    int gy = INT_MIN;
    int gx = INT_MIN;
    vector<int> x1(n), y1(n), x2(n), y2(n);
    for (int i = 0; i < n; ++i) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      sy = min(sy, y1[i]);
      sx = min(sx, x1[i]);
      gy = max(gy, y2[i]);
      gx = max(gx, x2[i]);
    }

    int ret1 = 0;
    int ret2 = 0;
    for (int a = 0; a < 11; ++a) {
      for (int b = 0; b < 11; ++b) {
        int yMin = a * 1000 - 1;
        int yMax = (a + 1) * 1000;
        int xMin = b * 1000 - 1;
        int xMax = (b + 1) * 1000;

        if (gy < yMin || gx < xMin || sy > yMax || sx > yMax)
          continue;

        vector<vector<int>> s(1001, vector<int>(1001, 0));
        for (int i = 0; i < n; ++i) {
          if (y2[i] < yMin || x2[i] < xMin || y1[i] > yMax || x1[i] > xMax)
            continue;

          ++s[max(yMin, y1[i]) - yMin][max(xMin, x1[i]) - xMin];
          if (x2[i] <= xMax)
            --s[max(yMin, y1[i]) - yMin][x2[i] - xMin];
          if (y2[i] <= yMax)
            --s[y2[i] - yMin][max(xMin, x1[i]) - xMin];
          if (y2[i] <= yMax && x2[i] <= xMax)
            ++s[y2[i] - yMin][x2[i] - xMin];
        }

        for (int i = 0; i < 1001; ++i) {
          for (int j = 0; j < 1000; ++j) {
            s[i][j + 1] += s[i][j];
          }
        }
        for (int i = 0; i < 1001; ++i) {
          for (int j = 0; j < 1000; ++j) {
            s[j + 1][i] += s[j][i];
          }
        }

        for (int i = 0; i < 1000; ++i) {
          for (int j = 0; j < 1000; ++j) {
            if (s[i][j] > 0)
              ++ret1;
            if ((s[i][j] > 0) ^ (s[i + 1][j] > 0))
              ++ret2;
            if ((s[i][j] > 0) ^ (s[i][j + 1] > 0))
              ++ret2;
          }
        }
      }
    }

    cout << ret1 << endl;
    if (r == 2)
      cout << ret2 << endl;
  }
}