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

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

using namespace std;

int x1[50], y1[50], x2[50], y2[50];
long long M[105][105];
vector<int> vx, vy;

int get(vector<int> v, int x) {
  for (int i = v.size() - 1; i >= 0; --i)
    if (v[i] == x)
      return i;
  return -1;
}

int dr[] = {-1, 1, 0, 0}, dc[] = {0, 0, -1, 1};
int nx, ny;

void dfs(int r, int c, long long cur) {
  M[r][c] = -1;

  for (int k = 0; k < 4; ++k) {
    int r2 = r + dr[k], c2 = c + dc[k];

    if (r2 >= 0 && r2 < nx && c2 >= 0 && c2 < ny && M[r2][c2] == cur)
      dfs(r2, c2, cur);
  }
}

int main() {
  int n;

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    vx.clear();
    vy.clear();
    vx.push_back(-1);
    vy.push_back(-1);

    for (int i = 0; i < n; ++i) {
      scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);
      vx.push_back(x1[i]);
      vx.push_back(x2[i]);
      vy.push_back(y1[i]);
      vy.push_back(y2[i]);
    }

    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    vx.erase(unique(vx.begin(), vx.end()), vx.end());
    vy.erase(unique(vy.begin(), vy.end()), vy.end());
    nx = vx.size();
    ny = vy.size();

    memset(M, 0, sizeof M);

    for (int i = 0; i < n; ++i) {
      x1[i] = get(vx, x1[i]);
      x2[i] = get(vx, x2[i]);
      y1[i] = get(vy, y1[i]);
      y2[i] = get(vy, y2[i]);

      for (int j = x1[i]; j < x2[i]; ++j)
        for (int k = y2[i]; k < y1[i]; ++k)
          M[j][k] |= (1LL << i);
    }

    int ans = 0;

    for (int i = 0; i < nx; ++i)
      for (int j = 0; j < ny; ++j)
        if (M[i][j] >= 0) {
          dfs(i, j, M[i][j]);
          ++ans;
        }

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

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define nl printf("\\n")
#define ll long long
#define FOR(it, x) for (__typeof(x.begin()) it = x.begin(); it != x.end(); it++)

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

inline int ini() {
  int temp;
  scanf("%d", &temp);
  return temp;
}
inline ll inll() {
  ll temp;
  scanf("%lld", &temp);
  return temp;
}
inline int stoi(string x) {
  int temp;
  sscanf(x.c_str(), "%d", &temp);
  return temp;
}
inline ll stoll(string x) {
  ll temp;
  sscanf(x.c_str(), "%lld", &temp);
  return temp;
}
inline string itos(int x) {
  char temp[100];
  sprintf(temp, "%d", x);
  return temp;
}
inline string lltos(ll x) {
  char temp[100];
  sprintf(temp, "%lld", x);
  return temp;
}
inline void outi(int x) { printf("%d", x); }
inline void outll(ll x) { printf("%lld", x); }
inline void OPEN(string a) {
  freopen((a + ".in").c_str(), "r", stdin);
  freopen((a + ".out").c_str(), "w", stdout);
}

int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
ll gcdll(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return (a * (b / gcd(a, b))); }
ll lcmll(ll a, ll b) { return (a * (b / gcd(a, b))); }

#define ini ini()
#define inll inll()

int px[] = {-1, 0, 1, 0};
int py[] = {0, 1, 0, -1};
int px8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int py8[] = {0, 1, 1, 1, 0, -1, -1, -1};

ll maze[105][105];

struct rect {
  int l, r, t, b;
  rect(int el, int er, int tt, int bb) { l = el, r = er, t = tt, b = bb; }
};

void dfs(int i, int j, int x) {
  maze[i][j] = -1;
  for (int k = 0; k < 4; k++) {
    int xx = px[k] + i;
    int yy = py[k] + j;
    if (xx >= 0 && xx < 105 && yy >= 0 && yy < 105 && maze[xx][yy] == x)
      dfs(xx, yy, x);
  }
}

vector<int> vx;
vector<int> vy;
vector<rect> vr;
// map<int,int> mx,my;
int l, r, t, b;
int L[55], R[55], T[55], B[55];
int x;

int main() {

  while (1) {
    scanf("%d", &x);
    if (x == 0)
      return 0;

    vx.clear(), vy.clear();

    memset(maze, 0, sizeof maze);

    for (int i = 0; i < x; i++) {
      // L[i]=ini,T[i]=ini,R[i]=ini,B[i]=ini;
      scanf("%d %d %d %d", &L[i], &T[i], &R[i], &B[i]);
      vx.pb(L[i]);
      vx.pb(R[i]);
      vy.pb(T[i]);
      vy.pb(B[i]);
    }
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());

    vx.erase(unique(vx.begin(), vx.end()), vx.end());
    vy.erase(unique(vy.begin(), vy.end()), vy.end());

    //	for(int i=0;i<vx.size();i++)mx[vx[i]]=(i+1);
    //	for(int i=0;i<vy.size();i++)my[vy[i]]=(i+1);

    for (int i = 0; i < x; i++) {
      l = lower_bound(vx.begin(), vx.end(), L[i]) - vx.begin() + 1;
      r = lower_bound(vx.begin(), vx.end(), R[i]) - vx.begin() + 1;
      t = lower_bound(vy.begin(), vy.end(), T[i]) - vy.begin() + 1;
      b = lower_bound(vy.begin(), vy.end(), B[i]) - vy.begin() + 1;

      for (int j = b; j < t; j++) {
        for (int k = l; k < r; k++) {
          maze[j][k] += (1LL << i);
        }
      }
    }

    int ans = 0;
    for (int i = 0; i <= vx.size(); i++) {
      for (int j = 0; j <= vy.size(); j++) {
        if (maze[i][j] >= 0)
          dfs(i, j, maze[i][j]), ans++;
      }
    }

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