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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int lenx, leny, vis[210][210], dist[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
void dfs(int i, int j) {
  if (vis[i][j] || i < 0 || i > lenx * 2 + 3 || j < 0 || j >= leny * 2 + 3)
    return;
  vis[i][j] = 1;
  for (int k = 0; k < 4; k++) {
    int ii = i + dist[k][0];
    int jj = j + dist[k][1];
    dfs(ii, jj);
  }
}
int main() {
  int n;
  while (scanf("%d", &n) && n) {
    memset(vis, 0, sizeof(vis));
    int i, j, sum = 0, a[153][5], v[55][5] = {0}, x[210], y[210];
    for (i = 0; i < n; i++) {
      scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
      x[i * 2] = a[i][0], x[i * 2 + 1] = a[i][2];
      y[i * 2] = a[i][1], y[i * 2 + 1] = a[i][3];
    }
    sort(x, x + n * 2);
    sort(y, y + n * 2);
    lenx = unique(x, x + n * 2) - x;
    leny = unique(y, y + n * 2) - y;
    for (i = 0; i < n; i++) {
      for (j = 0; j < lenx; j++) {
        if (a[i][0] == x[j] && !v[i][0])
          a[i][0] = j * 2 + 2, v[i][0] = 1;
        if (a[i][2] == x[j] && !v[i][2])
          a[i][2] = j * 2 + 2, v[i][2] = 1;
      }
      for (j = 0; j < leny; j++) {
        if (a[i][1] == y[j] && !v[i][1])
          a[i][1] = j * 2 + 2, v[i][1] = 1;
        if (a[i][3] == y[j] && !v[i][3])
          a[i][3] = j * 2 + 2, v[i][3] = 1;
      }
    }
    for (i = 0; i < n; i++) {
      for (j = a[i][0]; j <= a[i][2]; j++)
        vis[j][a[i][1]] = 1, vis[j][a[i][3]] = 1;
      for (j = a[i][3]; j <= a[i][1]; j++)
        vis[a[i][0]][j] = 1, vis[a[i][2]][j] = 1;
    }
    /*for(i=0; i<lenx*2+2; i++)
        for(j=0; j<leny*2+2; j++)
            if(!vis[i][j]) dfs(i,j),sum++;
    printf("%d\\n",sum);*/
    for (int i = 0; i < lenx * 2 + 2; i++)
      for (int j = 0; j < leny * 2 + 2; j++)
        if (!vis[i][j]) {
          dfs(i, j);
          sum++;
        }
    cout << sum << endl;
  }
  return 0;
}

/*
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int vis[200][10],dis[4][2]={1,0,-1,0,0,1,0,-1};
int sum=0;
void dfs(int i,int j)
{
    if(vis[i][j]) return ;
    vis[i][j]=1;
    for(int k=0;k<4;k++)
    {
        int ix=i+dis[k][0];
        int iy=j+dis[k][1];
        dfs(ix,iy);
    }
}
int main()
{
    int n,lenx,leny;
    while(scanf("%d",&n)&&n)
    {
        memset(vis,0,sizeof(vis));
        int a[200][10],x[500],y[500],v;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i][2],&a[i][3]);
            x[i*2]=a[i][0];
            x[i*2+1]=a[i][2];
            y[i*2]=a[i][1];
            y[i*2+1]=a[i][3];
        }
        sort(x,x+n*2);
        sort(y,y+n*2);
        lenx=unique(x,x+n*2)-x;
        leny=unique(y,y+n*2)-y;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<lenx;j++)
            {
                if(a[i][0]==x[j]&&!vis[i][0])
                {
                    a[i][0]=2*j+2;
                    vis[i][0]=1;
                }
                if(a[i][2]==x[j]&&!vis[i][2])
                {
                    a[i][2]=2*j+2;
                    vis[i][2]=1;
                }
            }
            for(int j=0;j<leny;j++)
            {
                if(a[i][1]==y[j]&&!vis[i][1])
                {
                    a[i][1]=2*j+2;
                    vis[i][1]=1;
                }
                if(a[i][3]==y[j]&&!vis[i][3])
                {
                    a[i][3]=2*j+2;
                    vis[i][3]=1;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=a[i][0];j<=a[i][2];j++)
            {
                vis[j][a[i][1]]=1;
                vis[j][a[i][3]]=1;
            }
            for(int j=a[i][3];j<=a[i][1];j++)
            {
                vis[a[i][0]][j]=1;
                vis[a[i][2]][j]=1;
            }
        }
        for(int i=0;i<lenx*2+2;i++)
            for(int j=0;j<leny*2+2;j++)
                  if(!vis[i][j])
                    {
                        dfs(i,j);
                         sum++;
                    }
        cout<<sum<<endl;
    }
    return 0;
}*/

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #define ll long long
#define clr(a, b) memset(a, b, sizeof(a))

using namespace std;
struct point {
  int x, y;
  point(int a = 0, int b = 0) { x = a, y = b; }
  friend bool operator==(const point &a, const point &b) {
    return b.x == a.x && b.y == a.y;
  }
  friend bool operator<(const point &a, const point &b) {
    if (a.x == b.x)
      return a.y < b.y;
    return a.x < b.x;
  }
  void input() { scanf("%d%d", &x, &y); }
};
struct line {
  point u, v;
  friend bool operator==(const line &a, const line &b) {
    return (b.u == a.u && b.v == a.v) || (b.u == a.v && b.v == a.u);
  }
  friend bool operator<(const line &a, const line &b) {
    if (a.u == b.u)
      return a.v < b.v;
    return a.u < b.u;
  }
};
struct recnt {
  point t[4];
  void input() {
    t[1].input();
    t[3].input();
    t[0].x = t[1].x;
    t[0].y = t[3].y;
    t[2].x = t[3].x;
    t[2].y = t[1].y;
  }
} rec[60];
point inter_seg_seg(point u1, point u2, point v1, point v2) {
  point ret = u1;
  if (u1.x == u2.x) {
    ret.x = u1.x, ret.y = v1.y;
  } else {
    ret.x = v1.x, ret.y = u1.y;
  }
  return ret;
}
set<point> mysp;
set<line> mysl;
set<line>::iterator it;
vector<int> myv[60];
int pa[60];
bool inter(point a, point b, point c, point d) {
  if (a.x == b.x) {
    if (min(a.y, b.y) <= c.y && max(a.y, b.y) >= c.y && min(c.x, d.x) <= a.x &&
        max(c.x, d.x) >= a.x)
      return 1;
  } else {
    if (min(a.x, b.x) <= c.x && max(a.x, b.x) >= c.x && min(c.y, d.y) <= a.y &&
        max(c.y, d.y) >= a.y)
      return 1;
  }
}
bool judge(recnt &a, recnt &b) {
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (inter(a.t[i], a.t[(i + 1) % 4], b.t[j], b.t[(j + 1) % 4])) {
        return 1;
      }
    }
  }
  return 0;
}
int find(int x) {
  if (x != pa[x])
    pa[x] = find(pa[x]);
  return pa[x];
}
vector<point> tv;
void solve(vector<int> &vv) {
  line tmp;
  for (int i = 0; i < 4; i++) {
    if (mysp.find(rec[vv[0]].t[i]) == mysp.end())
      mysp.insert(rec[vv[0]].t[i]);
    tmp.u = rec[vv[0]].t[i];
    tmp.v = rec[vv[0]].t[(i + 1) % 4];
    if (tmp.u < tmp.v)
      swap(tmp.u, tmp.v);
    if (mysl.find(tmp) == mysl.end())
      mysl.insert(tmp);
  }
  for (int i = 1; i < vv.size(); i++) {
    for (int j = 0; j < 4; j++) {
      if (mysp.find(rec[vv[i]].t[i]) == mysp.end())
        mysp.insert(rec[vv[i]].t[j]);
      tmp.u = rec[vv[i]].t[j];
      tmp.v = rec[vv[i]].t[(j + 1) % 4];
      if (tmp.u < tmp.v)
        swap(tmp.u, tmp.v);
      if (mysl.find(tmp) == mysl.end())
        mysl.insert(tmp);
    }
    int c = 0;
    for (int j = 0; j < 4; j++) {
      tv.clear();
      for (it = mysl.begin(); it != mysl.end(); ++it) {
        line tt = *it;
        if (tt.u < tt.v)
          swap(tt.u, tt.v);
        tmp.u = rec[vv[i]].t[j], tmp.v = rec[vv[i]].t[(j + 1) % 4];
        if (tmp.u < tmp.v)
          swap(tmp.u, tmp.v);
        if (inter(tmp.u, tmp.v, tt.u, tt.v)) {
          point t1 = inter_seg_seg(tmp.u, tmp.v, tt.u, tt.v);
          tv.push_back(t1);
          if (mysp.find(t1) == mysp.end())
            mysp.insert(t1);
          if (!(tt.u == t1) && !(tt.v == t1)) {
            mysl.erase(tt);
            line t2 = tt;
            t2.v = t1;
            if (t2.u < t2.v)
              swap(t2.u, t2.v);
            if (mysl.find(t2) == mysl.end())
              mysl.insert(t2);
            t2 = tt;
            t2.u = t1;
            if (t2.u < t2.v)
              swap(t2.u, t2.v);
            if (mysl.find(t2) == mysl.end())
              mysl.insert(t2);
          }
        }
      }
      sort(tv.begin(), tv.end());
      tv.erase(unique(tv.begin(), tv.end()), tv.end());
      mysl.erase(tmp);
      line l = tmp;
      for (int ii = 0; ii < tv.size() - 1; ii++) {
        l.u = tv[ii], l.v = tv[ii + 1];
        if (l.u < l.v)
          swap(l.u, l.v);
        if (mysl.find(l) == mysl.end())
          mysl.insert(l);
      }
    }
  }
}
int main() {
  int n;
  while (scanf("%d", &n) && n) {
    for (int i = 0; i < n; i++)
      rec[i].input();
    for (int i = 0; i < 60; i++) {
      pa[i] = i;
      myv[i].clear();
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int x1 = find(i);
        int y1 = find(j);
        if (x1 == y1)
          continue;
        else {
          if (judge(rec[i], rec[j])) {
            if (x1 < y1)
              pa[y1] = x1;
            else
              pa[x1] = y1;
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      myv[pa[i]].push_back(i);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (myv[i].size() == 0)
        continue;
      mysp.clear();
      mysl.clear();
      solve(myv[i]);
      ans += mysl.size() - mysp.size() + 2 - 1;
    }
    printf("%d\\n", ans + 1);
  }
  return 0;
}