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

// by team3
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define For(i, s, t) for (int i = (s); i <= (t); ++i)
#define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

typedef long long LL;
typedef pair<int, int> Pii;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;
const int Maxn = 105;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};

int N, Cntx, Cnty;
int X[Maxn], Y[Maxn], Idx[Maxn], Idy[Maxn];
long long Map[Maxn][Maxn];
bool f[Maxn][Maxn];

int Cmpx(const int &i, const int &j) { return X[i] < X[j]; }

int Cmpy(const int &i, const int &j) { return Y[i] < Y[j]; }

void dfs(int x, int y) {
  f[x][y] = 1;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx > Cntx)
      continue;
    if (ny < 0 || ny > Cnty)
      continue;
    if (Map[x][y] != Map[nx][ny])
      continue;
    if (!f[nx][ny])
      dfs(nx, ny);
  }
}

int main() {
  while (scanf("%d", &N) != EOF && N != 0) {
    for (int i = 0; i < N; ++i)
      scanf("%d%d%d%d", &X[i], &Y[i + N], &X[i + N], &Y[i]);
    for (int i = 0; i < 2 * N; ++i)
      Idx[i] = Idy[i] = i;
    sort(Idx, Idx + 2 * N, Cmpx);
    Cntx = 0, Cnty = 0;
    int last;
    for (int i = 0; i < 2 * N; ++i) {
      if (i == 0)
        ++Cntx, last = X[Idx[0]];
      else if (X[Idx[i]] != last)
        ++Cntx, last = X[Idx[i]];
      X[Idx[i]] = Cntx;
    }
    sort(Idy, Idy + 2 * N, Cmpy);
    for (int i = 0; i < 2 * N; ++i) {
      if (i == 0)
        ++Cnty, last = Y[Idy[0]];
      else if (Y[Idy[i]] != last)
        ++Cnty, last = Y[Idy[i]];
      Y[Idy[i]] = Cnty;
    }
    for (int i = 0; i <= Cntx; ++i)
      for (int j = 0; j <= Cnty; ++j)
        Map[i][j] = 0;
    // rep(i,N) cout << X[i] << " " << X[N+i] << " ";
    // cout << endl;
    // rep(i,N) cout << Y[i] << " " << Y[N+i] << " ";
    // cout << endl;
    for (int i = 0; i < N; ++i)
      for (int j = X[i]; j < X[i + N]; ++j)
        for (int k = Y[i]; k < Y[i + N]; ++k)
          Map[j][k] |= (1ll << i);
    // For(i,0,Cntx) {
    //     For(j,0,Cnty) cout << Map[i][j] << " ";
    //     cout << endl;
    // }
    for (int i = 0; i <= Cntx; ++i)
      for (int j = 0; j <= Cnty; ++j)
        f[i][j] = 0;
    int Ans = 0;
    for (int i = 0; i <= Cntx; ++i)
      for (int j = 0; j <= Cnty; ++j)
        if (!f[i][j]) {
          dfs(i, j);
          ++Ans;
        }
    printf("%d\\n", Ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repp(i, a, b) for (int i = a; i < a + b; ++i)
#define CLR(a) memset(a, 0, sizeof(a))
#define PB push_back
#define SZ size()

const int maxn = 55, maxN = 110 * 110 * 4;

vector<int> pos;
struct node {
  int x1, x2, y1, y2;
  void read() {
    scanf("%d%d%d%d", &x1, &y2, &x2, &y1);
    pos.PB(x1);
    pos.PB(x2);
    pos.PB(y1);
    pos.PB(y2);
  }
  void norm() {
    x1 = lower_bound(pos.begin(), pos.end(), x1) - pos.begin() + 1;
    x2 = lower_bound(pos.begin(), pos.end(), x2) - pos.begin() + 1;
    y1 = lower_bound(pos.begin(), pos.end(), y1) - pos.begin() + 1;
    y2 = lower_bound(pos.begin(), pos.end(), y2) - pos.begin() + 1;
  }
} a[maxn];

int n, len, id[maxn][maxn];
vector<int> e[maxN];
set<int> cant[maxN];
bool vis[maxN];

void adde(int u, int v) {
  cant[u].insert(v);
  cant[v].insert(u);
}
void bfs(int u) {
  int l = 0, r = 1;
  static int q[maxN];
  vis[u] = 1;
  q[l] = u;
  while (l < r) {
    int u = q[l++];
    rep(i, e[u].SZ) {
      int v = e[u][i];
      if (!vis[v])
        q[r++] = v, vis[v] = 1;
    }
  }
}

int main() {
  while (scanf("%d", &n), n) {
    pos.clear();
    rep(i, n) a[i].read();
    //		rep(i,n) printf("%d %d %d %d \\n", a[i].x1, a[i].x2, a[i].y1,
    //a[i].y2);

    sort(pos.begin(), pos.end());
    pos.erase(unique(pos.begin(), pos.end()), pos.end());
    //		rep(i,pos.SZ) printf("%d ", pos[i]); printf("\\n");
    len = pos.SZ + 4;
    rep(i, n) a[i].norm();
    // rep(i,n) printf("%d %d %d %d \\n", a[i].x1, a[i].x2, a[i].y1, a[i].y2);
    int tot = 0;
    rep(i, len) rep(j, len) id[i][j] = ++tot;
    assert(tot < maxN);
    repp(i, 1, tot) cant[i].clear();
    rep(o, n) {
      int x1 = a[o].x1, x2 = a[o].x2, y1 = a[o].y1, y2 = a[o].y2;
      for (int i = x1; i < x2; ++i) {
        adde(id[i][y1], id[i][y1 - 1]);
        adde(id[i][y2], id[i][y2 - 1]);
      }
      for (int i = y1; i < y2; ++i) {
        adde(id[x1][i], id[x1 - 1][i]);
        adde(id[x2][i], id[x2 - 1][i]);
      }
    }
    // printf("tot %d\\n", tot);
    repp(i, 1, tot) e[i].clear();
    rep(i, len) rep(j, len) {
      int ix = id[i][j];
      int iy;
      if (i > 0) {
        iy = id[i - 1][j];
        if (cant[ix].find(iy) == cant[ix].end())
          e[ix].PB(iy);
      }
      if (i + 1 < len) {
        iy = id[i + 1][j];
        if (cant[ix].find(iy) == cant[ix].end())
          e[ix].PB(iy);
      }
      if (j > 0) {
        iy = id[i][j - 1];
        if (cant[ix].find(iy) == cant[ix].end())
          e[ix].PB(iy);
      }
      if (j + 1 < len) {
        iy = id[i][j + 1];
        if (cant[ix].find(iy) == cant[ix].end())
          e[ix].PB(iy);
      }
    }

    // repp(i,1,tot) rep(j,e[i].SZ) printf("%d %d\\n", i, e[i][j]);
    int cnt = 0;
    CLR(vis);
    repp(i, 1, tot) if (!vis[i]) {
      // printf("vis %d\\n", i);
      ++cnt;
      bfs(i);
    }
    printf("%d\\n", cnt);
  }
}