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

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};

struct Rect {
  int lx, ly;
  int rx, ry;
  int id;
  Rect(int rx, int ry, int lx, int ly, int id)
      : rx(rx), ry(ry), lx(lx), ly(ly), id(id) {}
  Rect() {}

  Rect(const Rect &r) {
    lx = r.lx;
    ly = r.ly;
    rx = r.rx;
    ry = r.ry;
    id = r.id;
  }

  bool operator>(const Rect &r) const {
    if (rx == r.rx) {
      return ry > r.ry;
    }
    return rx > r.rx;
  }
};

bool used[10];
int order[10];
int idx;
int total_pages;
void dfs(int border_x, int lower_bound, const vector<Rect> &pages) {
  for (int round = 0; round < total_pages; round++) {
    priority_queue<Rect, vector<Rect>, greater<Rect>> candidates;

    for (int i = 0; i < total_pages; i++) {
      if (used[i])
        continue;
      if (lower_bound < pages[i].ly)
        continue;
      if (pages[i].lx > border_x)
        continue;
      // if(pages[i].rx < last_rx) continue;
      candidates.push(pages[i]);
    }

    if (candidates.empty())
      return;

    Rect last = candidates.top();
    candidates.pop();

    // while(!candidates.empty()) candidates.pop();
    // printf("border:%d id:%d lower:%d\\n",border_x,last.id,lower_bound);
    order[last.id] = idx++;
    used[last.id] = true;

    // last_rx = last.rx;

    int next_lower_bound = last.ly;
    int next_border_x = 1000;
    // cout << "last:" << last.id<< endl;
    for (int i = 0; i < total_pages; i++) {
      if (pages[i].ly > last.ly && pages[i].ry < last.ly &&
          pages[i].rx >= last.lx) {
        next_border_x = min(pages[i].rx, next_border_x);
      }
    }
    // printf("next:border:%d id:%d lower:%d\\n",border_x,last.id,lower_bound);
    dfs(next_border_x, next_lower_bound, pages);
  }
}

int main() {
  bool is_first = true;
  while (~scanf("%d", &total_pages)) {
    if (total_pages == 0)
      break;
    vector<Rect> pages;
    for (int i = 0; i < total_pages; i++) {
      int lx, ly;
      int rx, ry;
      scanf("%d %d %d %d", &rx, &ry, &lx, &ly);
      pages.push_back(Rect(rx, ry, lx, ly, i));
    }

    memset(used, false, sizeof(used));
    memset(order, 0, sizeof(order));
    idx = 1;
    dfs(1000, 1000, pages);

    if (!is_first) {
      printf("\\n");
    }
    for (int i = 0; i < total_pages; i++) {
      printf("%d\\n", order[i]);
    }
    is_first = false;
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};

struct Rect {
  int lx, ly;
  int rx, ry;
  int id;
  Rect(int rx, int ry, int lx, int ly, int id)
      : rx(rx), ry(ry), lx(lx), ly(ly), id(id) {}
  bool operator<(const Rect &r) const { return ry < r.ry; }
  bool operator>(const Rect &r) const { return ry > r.ry; }
};

int main() {
  int n;
  bool is_first = true;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    vector<Rect> pages;
    for (int i = 0; i < n; i++) {
      int lx, ly;
      int rx, ry;
      scanf("%d %d %d %d", &rx, &ry, &lx, &ly);
      pages.push_back(Rect(rx, ry, lx, ly, i));
    }

    bool used[10];
    memset(used, false, sizeof(used));

    int order[10];
    int idx = 1;
    priority_queue<Rect, vector<Rect>, greater<Rect>> candidates;
    for (int round = 0; round < n; round++) {
      int min_rx = 1000;

      if (candidates.empty()) {
        for (int i = 0; i < n; i++) {
          if (used[i])
            continue;
          if (pages[i].rx < min_rx) {
            while (!candidates.empty())
              candidates.pop();
            min_rx = pages[i].rx;
            candidates.push(pages[i]);
          } else if (pages[i].rx == min_rx) {
            candidates.push(pages[i]);
          }
        }
      }

      if (candidates.empty())
        break;

      int min_ry = 1000;
      int selected = -1;
      while (!candidates.empty()) {
        Rect r = candidates.top();
        candidates.pop();
        if (used[r.id])
          continue;
        used[r.id] = true;
        order[r.id] = idx++;
        break;
      }
      while (!candidates.empty())
        candidates.pop();

      Rect last = pages[selected];
      int border_x = last.lx;
      bool visited[10];
      memset(visited, false, sizeof(visited));
      for (int round2 = 0; round2 < n; round2++) {
        bool over = true;
        for (int i = 0; i < n; i++) {
          if (visited[i])
            continue;
          if (border_x != pages[i].rx)
            continue;
          if (pages[i].ly != last.ly)
            continue;
          border_x = pages[i].lx;
          visited[i] = true;
          over = false;
        }
        if (over)
          break;
      }
      for (int i = 0; i < n; i++) {
        if (used[i])
          continue;
        if (pages[i].lx <= border_x && pages[i].ly >= last.ly) {
          candidates.push(pages[i]);
        }
      }
    }

    if (!is_first) {
      printf("\\n");
    }
    for (int i = 0; i < n; i++) {
      printf("%d\\n", order[i]);
    }
    is_first = false;
  }
}