Problem p00454 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>

using namespace std;

vector<pair<pair<int, int>, int>> points;
vector<int> x_coodinate;
vector<int> y_coodinate;
vector<pair<int, int>> x_newind;
vector<pair<int, int>> y_newind;
vector<int> new_coodinate[2005];
int x_max;
int y_max;

pair<int, int> search(pair<int, int> point) {
  int x = point.first;
  int y = point.second;
  int min = 0;
  int now;
  int max = x_coodinate.size();
  pair<int, int> new_point;

  while (1) {
    now = (min + max) / 2;
    if (x < x_coodinate[now]) {
      max = now;
    } else if (x > x_coodinate[now]) {
      min = now;
    } else {
      new_point.first = x_newind[now].second;
      break;
    }

    if (max - min <= 1) {
      new_point.first = x_newind[now].second;
      break;
    }
  }

  min = 0;
  max = y_coodinate.size();

  while (1) {
    now = (min + max) / 2;
    if (y < y_coodinate[now]) {
      max = now;
    } else if (y > y_coodinate[now]) {
      min = now;
    } else {
      new_point.second = y_newind[now].second;
      break;
    }

    if (max - min <= 1) {
      new_point.second = y_newind[now].second;
      break;
    }
  }

  return new_point;
}

int rec(int i, int j) {
  if (new_coodinate[j][i] != 0)
    return 0;

  new_coodinate[j][i] = 1;

  if (j > 0)
    rec(i, j - 1);
  if (j < y_max - 1)
    rec(i, j + 1);
  if (i > 0)
    rec(i - 1, j);
  if (i < x_max - 1)
    rec(i + 1, j);

  return 1;
}

int main() {
  while (1) {
    int i;
    int w, h;
    int n;
    int ind;
    int sum = 0;
    int x_erase = 0;
    int y_erase = 0;

    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      return 0;

    points.clear();
    x_coodinate.clear();
    y_coodinate.clear();
    x_newind.clear();
    y_newind.clear();
    for (i = 0; i < 2005; i++) {
      new_coodinate[i].clear();
      new_coodinate[i].resize(2005, 0);
    }

    scanf("%d", &n);

    x_coodinate.push_back(0);
    y_coodinate.push_back(0);
    for (i = 0; i < n; i++) {
      int x1, x2;
      int y1, y2;

      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      if (x2 == w)
        x_erase = 1;
      if (y2 == h)
        y_erase = 1;

      x_coodinate.push_back(x1);
      x_coodinate.push_back(x2);
      y_coodinate.push_back(y1);
      y_coodinate.push_back(y2);

      points.push_back(make_pair(make_pair(x1, y1), 1));
      points.push_back(make_pair(make_pair(x1, y2), -1));
      points.push_back(make_pair(make_pair(x2, y1), -1));
      points.push_back(make_pair(make_pair(x2, y2), 1));
    }

    sort(x_coodinate.begin(), x_coodinate.end());
    sort(y_coodinate.begin(), y_coodinate.end());

    ind = 0;
    x_newind.push_back(make_pair(x_coodinate[0], 0));
    for (i = 1; i < x_coodinate.size(); i++) {
      if (x_coodinate[i - 1] != x_coodinate[i])
        ind++;
      x_newind.push_back(make_pair(x_coodinate[i], ind));
    }

    ind = 0;
    y_newind.push_back(make_pair(y_coodinate[0], 0));
    for (i = 1; i < y_coodinate.size(); i++) {
      if (y_coodinate[i - 1] != y_coodinate[i])
        ind++;
      y_newind.push_back(make_pair(y_coodinate[i], ind));
    }

    sort(points.begin(), points.end());

    x_max = 0;
    y_max = 0;
    vector<pair<pair<int, int>, int>>::iterator it = points.begin();
    while (it != points.end()) {
      pair<int, int> new_point = search(it->first);
      if (new_point.first >= x_max)
        x_max = new_point.first + 1;
      if (new_point.second >= y_max)
        y_max = new_point.second + 1;
      new_coodinate[new_point.second][new_point.first] += it->second;
      it++;
    }

    for (i = 0; i < y_max; i++) {
      int j;
      for (j = 1; j < x_max; j++) {
        new_coodinate[i][j] += new_coodinate[i][j - 1];
      }
    }

    for (i = 0; i < x_max; i++) {
      int j;
      for (j = 1; j < y_max; j++) {
        new_coodinate[j][i] += new_coodinate[j - 1][i];
      }
    }

    if (x_erase)
      x_max--;
    if (y_erase)
      y_max--;

    for (i = 0; i < y_max; i++) {
      int j;
      for (j = 0; j < x_max; j++) {
        sum += rec(j, i);
      }
    }

    printf("%d\\n", sum);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>

using namespace std;

vector<pair<pair<int, int>, int>> points;
vector<int> x_coodinate;
vector<int> y_coodinate;
vector<pair<int, int>> x_newind;
vector<pair<int, int>> y_newind;
vector<int> new_coodinate[4003];
int x_max;
int y_max;

pair<int, int> search(pair<int, int> point) {
  int x = point.first;
  int y = point.second;
  int min = 0;
  int now;
  int max = x_coodinate.size();
  pair<int, int> new_point;

  while (1) {
    now = (min + max) / 2;
    if (x < x_coodinate[now]) {
      max = now;
    } else if (x > x_coodinate[now]) {
      min = now;
    } else {
      new_point.first = x_newind[now].second;
      break;
    }

    if (max - min <= 1) {
      new_point.first = x_newind[now].second;
      break;
    }
  }

  min = 0;
  max = y_coodinate.size();

  while (1) {
    now = (min + max) / 2;
    if (y < y_coodinate[now]) {
      max = now;
    } else if (y > y_coodinate[now]) {
      min = now;
    } else {
      new_point.second = y_newind[now].second;
      break;
    }

    if (max - min <= 1) {
      new_point.second = y_newind[now].second;
      break;
    }
  }

  return new_point;
}

int rec(int i, int j) {
  if (new_coodinate[j][i] != 0)
    return 0;

  new_coodinate[j][i] = 1;

  if (j > 0)
    rec(i, j - 1);
  if (j < y_max - 1)
    rec(i, j + 1);
  if (i > 0)
    rec(i - 1, j);
  if (i < x_max - 1)
    rec(i + 1, j);

  return 1;
}

int main() {
  while (1) {
    int i;
    int w, h;
    int n;
    int ind;
    int sum = 0;

    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      return 0;

    points.clear();
    x_coodinate.clear();
    y_coodinate.clear();
    x_newind.clear();
    y_newind.clear();
    for (i = 0; i < 4003; i++) {
      new_coodinate[i].clear();
      new_coodinate[i].resize(4003, 0);
    }

    scanf("%d", &n);

    x_coodinate.push_back(0);
    y_coodinate.push_back(0);
    for (i = 0; i < n; i++) {
      int x1, x2;
      int y1, y2;

      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      x_coodinate.push_back(x1);
      x_coodinate.push_back(x2);
      y_coodinate.push_back(y1);
      y_coodinate.push_back(y2);

      points.push_back(make_pair(make_pair(x1, y1), 1));
      points.push_back(make_pair(make_pair(x1, y2), -1));
      points.push_back(make_pair(make_pair(x2, y1), -1));
      points.push_back(make_pair(make_pair(x2, y2), 1));
    }

    sort(x_coodinate.begin(), x_coodinate.end());
    sort(y_coodinate.begin(), y_coodinate.end());

    ind = 0;
    x_newind.push_back(make_pair(x_coodinate[0], 0));
    for (i = 1; i < x_coodinate.size(); i++) {
      if (x_coodinate[i - 1] != x_coodinate[i])
        ind++;
      x_newind.push_back(make_pair(x_coodinate[i], ind));
    }

    ind = 0;
    y_newind.push_back(make_pair(y_coodinate[0], 0));
    for (i = 1; i < y_coodinate.size(); i++) {
      if (y_coodinate[i - 1] != y_coodinate[i])
        ind++;
      y_newind.push_back(make_pair(y_coodinate[i], ind));
    }

    sort(points.begin(), points.end());

    x_max = 0;
    y_max = 0;
    vector<pair<pair<int, int>, int>>::iterator it = points.begin();
    while (it != points.end()) {
      pair<int, int> new_point = search(it->first);
      if (new_point.first >= x_max)
        x_max = new_point.first + 1;
      if (new_point.second >= y_max)
        y_max = new_point.second + 1;
      new_coodinate[new_point.second][new_point.first] += it->second;
      it++;
    }

    for (i = 0; i < y_max; i++) {
      int j;
      for (j = 1; j < x_max; j++) {
        new_coodinate[i][j] += new_coodinate[i][j - 1];
      }
    }

    for (i = 0; i < x_max; i++) {
      int j;
      for (j = 1; j < y_max; j++) {
        new_coodinate[j][i] += new_coodinate[j - 1][i];
      }
    }

    if (x_max > w - 1)
      x_max = w - 1;
    if (y_max > h - 1)
      y_max = h - 1;

    for (i = 0; i < y_max; i++) {
      int j;
      for (j = 0; j < x_max; j++) {
        sum += rec(j, i);
      }
    }

    printf("%d\\n", sum);
  }
}