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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <cstdio>
#include <algorithm>

using namespace std;

int D[ 1000005 ];
int size;
int P[ 100005 ][4];

bool tree_cmp_func( int i, int j )
{return P[i][3]<P[j][3];}

void tree_set( int p, int val )
{
    p+=size; D[p]=val; p>>=1;
    while ( p )
       {
           D[p]=D[p*2];
           if ( tree_cmp_func( D[p*2+1], D[p] ) )
               D[p]=D[p*2+1];
           p>>=1;
       }
}

int tree_get( int p )
{
    p+=size;
    
    int w=D[p];
    
    while ( p )
          {
             if (!( p%2 ))
                {
                    if ( tree_cmp_func( D[p+1], w ) )
                        w=D[p+1];
                }
             p>>=1;
          }
    return w;
} 

int x_rev;

int SX    [ 200005 ];
int SX_rev[ 200005 ];

int SY    [ 100005 ];
int SY_rev[ 100005 ];

bool sort_func_timeline( int i, int j )
{
    int pi=i%2, pj=j%2;
    i/=2; j/=2;
    if ( P[i][pi] != P[j][pj] ) return (P[i][pi] < P[j][pj])^x_rev;
    if ( pi != pj ) return pi < pj;
    return P[i][2]>P[j][2];
}

bool sort_funcy( int i, int j )
{
    return P[i][3]<P[j][3];
}

bool sort_funcy2( int i, int j )
{
    return i<j;
}

int SW[ 100005 ];
bool is_wyn[ 100005 ];

bool sort_func_wyn( int i, int j )
{
    if ( P[i][0] != P[j][0] ) return P[i][0] < P[j][0];
    if ( P[i][1] != P[j][1] ) return P[i][1] < P[j][1];
    if ( P[i][2] != P[j][2] ) return P[i][2] < P[j][2];
    return P[i][3] < P[j][3];
}

int main()
{
    int n,ilw;
    scanf("%d",&n);
    int _prev_n=n;
    x_rev=1;
    P[0][0]=-1; P[0][1]=-1; P[0][2]=1000000009; P[0][3]=2000000009;
    for ( int i=1; i<=n; i++ ) scanf("%d%d%d%d",P[i]+0,P[i]+1,P[i]+2,P[i]+3);
    
    int _recalc_begin=1;
    while ( _recalc_begin ) {
    _recalc_begin=0;
    
    for ( int i=0; i<=n; i++ ) is_wyn[i]=1;
    size=1; while ( size<=n+1 ) size*=2;
    for ( int i=0; i<=size*2; i++ ) D[i]=0;
    
    int ilsx=0;
    SY[0]=0;
    for ( int i=1; i<=n; i++ )
        {
            SX[ilsx++]=i*2;
            SX[ilsx++]=i*2+1;
            SY[i]=i;
        }
    //printf("%d\n",ilsx);
    
    sort( SX, SX+ilsx, sort_func_timeline );
    for ( int i=0; i<ilsx; i++ ) SX_rev[SX[i]]=i;
    
    sort( SY, SY+n+1, sort_funcy );
    for ( int i=0; i<=n; i++ ) SY_rev[SY[i]]=i;
    for ( int i=0; i<=n; i++ ) SY[i]=P[SY[i]][3];
    
    //printf("%d\n",ilsx);
    int ii=0;
    //while ( ii<ilsx )
    //for ( int i=0; i<ilsx; i++ )
        {
            _rep_beg:;
            int e = SX[ii]/2;
            //printf( "    %d %d\n", e, SX[ii]%2 );
            
            if ( ! ((SX[ii]%2)^x_rev) )
               {
                   while ( 1 )
                         {
                             int _p = lower_bound( SY, SY+n+1, P[e][2], sort_funcy2 ) - SY;
                             if ( SY[ _p ] < P[e][2] ) _p++;
                             int a = tree_get( _p );
                             //printf("        %d\n",a);
                             if ( P[e][3] < P[a][2] ) goto _cont;
                             //printf( "%d %d\n", e, a );
                             
                             tree_set( SY_rev[a], 0 );
                             is_wyn[a]=0;
                             
                             int x_swap;
                             if ( !x_rev ) x_swap=(P[a][1]>P[e][1]);
                                      else x_swap=(P[a][0]<P[e][0]);
                             
                             if ( x_swap )
                                {
                                    swap( SX[ SX_rev[2*e+1] ], SX[ SX_rev[2*a+1] ] );
                                    swap( SX_rev[2*e+1], SX_rev[2*a+1]);
                                }
                             
                             if ( P[a][3]>P[e][3] )
                                 SY_rev[e] = SY_rev[a];
                             
                             P[e][0]=min(P[e][0],P[a][0]);
                             P[e][1]=max(P[e][1],P[a][1]);
                             P[e][2]=min(P[e][2],P[a][2]);
                             P[e][3]=max(P[e][3],P[a][3]);
                         }
                   _cont:;
                   
                   tree_set( SY_rev[e], e );
               } else
                 if ( is_wyn[ e ] )
                    tree_set( SY_rev[e], 0 );
            ii++;
            if ( ii != ilsx ) goto _rep_beg;
        }
    
    ilw=0;
    for ( int i=0; i<=n; i++ ) if ( is_wyn[ i ] ) SW[ ilw++ ] = i;
    
    //printf("%d %d\n",ilw,_prev_n);
    if ( ilw-1 != _prev_n )
       {
           _prev_n=n;
           n=ilw-1;
           for ( int i=0; i<=n; i++ )
               {
                   int _sw=SW[i];
                   P[i][0]=P[_sw][0];
                   P[i][1]=P[_sw][1];
                   P[i][2]=P[_sw][2];
                   P[i][3]=P[_sw][3];
               }
           x_rev ^= 1;
           _recalc_begin=1;
       }
    }
    
    sort( SW, SW+ilw, sort_func_wyn );
    
    printf("%d\n",ilw-1);
    for ( int i=1; i<ilw; i++ )
        {
            int _w=SW[i];
            printf("%d %d %d %d\n",P[_w][0],P[_w][1],P[_w][2],P[_w][3]);
        }
        
    return 0;
}