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

/*?????*/

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL> PLL;
typedef unsigned long long uLL;

template <typename T>
void print(T *p, T *q, string Gap = " ", bool flag = false) {
  int d = p < q ? 1 : -1;
  while (p != q) {
    if (flag)
      cout << Gap[0] << *p << Gap[1];
    else
      cout << *p;
    p += d;
    if (p != q && !flag)
      cout << Gap;
  }
  cout << endl;
}

template <typename T> void print(const T &a, string bes = "") {
  int len = bes.length();
  if (len >= 2)
    cout << bes[0] << a << bes[1] << endl;
  else
    cout << a << endl;
}

template <typename T>
void debug(T *p, T *q, string Gap = " ", bool flag = false) {
  int d = p < q ? 1 : -1;
  cout << "Debug out:";
  while (p != q) {
    if (flag)
      cout << Gap[0] << *p << Gap[1];
    else
      cout << *p;
    p += d;
    if (p != q && !flag)
      cout << Gap;
  }
  cout << endl;
}

template <typename T> void debug(const T &a, string bes = "") {
  int len = bes.length();
  cout << "Debug out:";
  if (len >= 2)
    cout << bes[0] << a << bes[1] << endl;
  else
    cout << a << endl;
}

void IO_Init() { ios::sync_with_stdio(false); }

LL LLabs(const LL a) { return a >= 0 ? a : -a; }

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;

/*?????*/

int W, H, N;
int X1[MAXN], X2[MAXN], Y1[MAXN], Y2[MAXN];
bool fld[MAXN * 6][MAXN * 6];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int compress(int *x1, int *x2, int w) {
  vector<int> xs;
  for (int i = 0; i < N; i++) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i] + d, tx2 = x2[i] + d;
      if (1 <= tx1 && tx1 <= w)
        xs.pb(tx1);
      if (1 <= tx2 && tx2 <= w)
        xs.pb(tx2);
    }
  }

  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < N; i++) {
    x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  return xs.size();
}

void solve() {
  W = compress(X1, X2, W);
  H = compress(Y1, Y2, H);

  mem(fld, false);

  for (int i = 0; i < N; i++) {
    for (int y = Y1[i]; y <= Y2[i]; y++) {
      for (int x = X1[i]; x <= X2[i]; x++) {
        fld[y][x] = true;
      }
    }
  }
  int ans = 0;

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (fld[y][x])
        continue;
      ans++;
      fld[y][x] = true;
      queue<PII> que;
      que.push(mp(x, y));
      while (!que.empty()) {
        int sx = que.front().first, sy = que.front().second;
        que.pop();
        for (int i = 0; i < 4; i++) {
          int nx = sx + dx[i];
          int ny = sy + dy[i];
          if (nx < 0 || ny < 0 || nx >= W || ny >= H)
            continue;
          if (fld[ny][nx])
            continue;
          que.push(mp(nx, ny));
          fld[ny][nx] = true;
        }
      }
    }
  }

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

int main() {
#ifndef ONLINE_JUDGE
  // FIN;
  // FOUT;
#endif

  while (~scanf("%d%d", &W, &H), W && H) {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
      X1[i]++, Y1[i]++;
    }
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

// Caution!!!!!!sort first,then -1

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct rect {
  int sx, sy, tx, ty;
};
struct dic {
  int data, id, st;
  friend bool operator<(dic a, dic b) { return a.data < b.data; }
};
dic dicx[2200], dicy[2200];
rect rec[2000];
bool f[220][220];
int n;
int maxx, maxy;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int getpos(int *a, int x, int l, int r) {
  if (l > r)
    return -1;
  if (l == r)
    return l;
  int mid = (l + r) >> 1;
  if (a[mid] > x)
    return getpos(a, x, l, mid - 1);
  if (a[mid] == x)
    return mid;
  if (a[mid] < x)
    return getpos(a, x, mid + 1, r);
}
void paint_color(int x, int y) {
  f[x][y] = true;
  for (int i = 0; i <= 3; i++) {
    int tx = x + dx[i], ty = y + dy[i];
    if (tx >= 0 && tx < maxx)
      if (ty >= 0 && ty < maxy)
        if (!f[tx][ty])
          paint_color(tx, ty);
  }
}
void adddic(dic *a, int &now, int data, int id, int st) {
  a[++now].data = data;
  a[now].id = id;
  a[now].st = st;
}
int main() {
  while (scanf("%d%d", &maxx, &maxy) == 2) {
    // maxx--;
    // maxy--;
    if (maxx == 0 && maxy == 0)
      break;
    memset(f, 0, sizeof(f));
    scanf("%d", &n);
    int topx = -1;
    int topy = -1;
    adddic(dicx, topx, 0, -1, 0);
    adddic(dicx, topx, maxx, -1, 1);
    adddic(dicy, topy, 0, -1, 0);
    adddic(dicy, topy, maxy, -1, 1);
    for (int i = 0; i < n; i++) {
      int sx, sy, tx, ty;
      scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
      // tx;
      // ty;
      rec[i].sx = sx;
      rec[i].sy = sy;
      rec[i].tx = tx;
      rec[i].ty = ty;
      adddic(dicx, topx, sx, i, 0);
      adddic(dicy, topy, sy, i, 0);
      adddic(dicx, topx, tx, i, 1);
      adddic(dicy, topy, ty, i, 1);
    }
    sort(dicx, dicx + topx + 1);
    sort(dicy, dicy + topy + 1);
    // cout<<"topx:"<<endl;
    // for (int i=0;i<=topx;i++) cout<<dicx[i].data<<" ";
    // cout<<endl<<"topy:"<<endl;
    // for (int i=0;i<=topy;i++) cout<<dicy[i].data<<" ";
    // cout<<endl;
    int pos = 0, now = dicx[0].data;
    for (int i = 0; i <= topx; i++) {
      if (dicx[i].data > now) {
        pos++;
        now = dicx[i].data;
      }
      int id = dicx[i].id;
      if (id < 0 && dicx[i].st == 1)
        maxx = pos;
      else if (id < 0)
        continue;
      if (dicx[i].st == 0)
        rec[id].sx = pos;
      else
        rec[id].tx = pos;
    }
    pos = 0, now = dicy[0].data;
    for (int i = 0; i <= topy; i++) {
      if (dicy[i].data > now) {
        pos++;
        now = dicy[i].data;
      }
      int id = dicy[i].id;
      if (id < 0 && dicy[i].st == 1)
        maxy = pos;
      else if (id < 0)
        continue;
      if (dicy[i].st == 0)
        rec[id].sy = pos;
      else
        rec[id].ty = pos;
    }

    //  for (int i=0;i<n;i++)
    //  {
    //     cout<<rec[i].sx<<" "<<rec[i].sy<<" "<<rec[i].tx<<"
    //     "<<rec[i].ty<<endl;
    // }
    //        maxx=getpos(dicx,maxx,0,topx);
    //      maxy=getpos(dicy,maxy,0,topy);

    for (int i = 0; i < n; i++)
      for (int j = rec[i].sx; j < rec[i].tx; j++)
        for (int k = rec[i].sy; k < rec[i].ty; k++)
          f[j][k] = true;
    int cou = 0;
    for (int i = 0; i < maxx; i++)
      for (int j = 0; j < maxy; j++) {
        if (!f[i][j]) {
          cou++;
          paint_color(i, j);
        }
      }
    printf("%d\\n", cou);
  }
  return 0;
}