1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct rectangle
{
	int x1;
	int x2;
	int y1;
	int y2;
};

int k=0,n,gumis=100000;
rectangle t[100005];
int tmax[200005];
vector <int> v[1000005];
int tt[100005];
bool d[100005];
bool f;

void ADD (int i)
     {
            int p=gumis+i;
            tmax[p]=tt[i];
            p=p/2;

            while(p>0)
                {
                tmax[p]=max(tmax[2*p],tmax[2*p+1]);
                p=p/2;
                }
       }


int MAX(int a,int b)
     {
     a=a+gumis;
     b=b+gumis;
     int w;
     w=tmax[a];
     if(a!=b)w=max(w,tmax[b]);
     while(a/2 != b/2)
        {
        if(a%2==0)  w=max(w,tmax[a+1]);
        if(b%2==1)  w=max(w,tmax[b-1]);
        a=a/2;
        b=b/2;
        }
    return w;
    }

bool cmp(rectangle a,rectangle b)
{
	if (a.x1==b.x1)
	{
		if (a.x2==b.x2)
		{
			if (a.y1==b.y1)
			{
				if (a.y2==b.y2)
				{
					return 1;		
				}
				else return a.y2<b.y2;
			}
			else return a.y1<b.y1;	
		}
		else return a.x2<b.x2;
	}
	else return a.x1<b.x1;
}

int main()
{
	ios_base::sync_with_stdio(0);
	int x;
	cin >>n;
	for (int i=0;i<200002;i++) {d[i/2]=false;   tmax[i]=0; tt[i/2]=0; }
	for (int i=0;i<n;i++)
	{
		cin >>t[i].x1>>t[i].x2>>t[i].y1>>t[i].y2;
                for (int j=t[i].x1+1;j<=t[i].x2;j++)
                {
                        v[j].push_back(i);
                        tt[j]++;
                        ADD(j);
                }
		f=true;
		int e=i;
                while (tmax[1]>1 && f==true)
                {
			f=false;
			for (int j=t[e].x1+1;j<=t[e].x2;j++)
			{
				if (tt[j]>1)
				{
					for (int q=0;q<v[j].size();q++)
					{
						x=v[j][q];
						if ( (    (( t[x].x1<=t[e].x1 && t[e].x1<=t[x].x2   ) && (t[x].y1<=t[e].y1 && t[e].y1<=t[x].y2 ))  ||     (( t[e].x1<=t[x].x1 && t[x].x1<=t[e].x2   ) && (t[e].y1<=t[x].y1 && t[x].y1<=t[e].y2 ))     )&& x!=e)
						{
							q=v[j].size()+1;
							for (int ii=min(t[x].x2,t[e].x2)+1;ii<=max(t[x].x2,t[e].x2);ii++)				
							{	
								
								v[ii].push_back(x);
															
							}
							for (int j=max(t[e].x1,t[x].x1)+1;j<=min(t[e].x2,t[x].x2);j++)
					                {
                 					        tt[j]--;
                        					ADD(j);
                					}
							f=true;
							t[x].x1=min(t[e].x1,t[x].x1);
							t[x].x2=max(t[e].x2,t[x].x2);
							t[x].y1=min(t[e].y1,t[x].y1);
							t[x].y2=max(t[e].y2,t[x].y2);
							t[e].x1=0;
							t[e].x2=0;
							t[e].y1=0;
							t[e].y2=0;
							e=x;													
						}
					}	
				}
			}

                }
		
	}
	for (int i=0;i<n;i++)
	{
	if (t[i].x1!=0 || t[i].x2!=0 || t[i].y1!=0 || t[i].y2!=0 )  k++;
	}
	cout <<k<<endl;
	sort (t,t+n,cmp);
	for (int i=0;i<n;i++)
	{
	if (t[i].x1!=0 && t[i].x2!=0 && t[i].y1!=0 && t[i].y2!=0 )	cout <<t[i].x1 <<" "<<t[i].x2 <<" "<< t[i].y1<< " " <<t[i].y2 <<endl;
	}	


	return 0;
}