// Krzysztof Piesiewicz // Plemiona [B] PA 2014 #include <cstdio> #include <algorithm> #include <set> using namespace std; const int MAX_N = 100007, MAX_SIZE = 1 << 20, DB = 0; class Rect { public: int id, x[ 2 ], y[ 2 ]; bool isCountry; }; class Apex { public: struct CmpY { inline bool operator() ( Rect *a, Rect *b ) { if( a->y[ 1 ] == b->y[ 1 ] ) return a < b; return a->y[ 1 ] > b->y[ 1 ]; } }; int l, r; Apex *left, *right; set< Rect*, CmpY > s[ 2 ]; Apex( int p, int k ) : l( p ), r( k ) { if( r - l >= 2 ) { left = new Apex( l, (l + r) / 2 ); right = new Apex( (l + r) / 2, r ); } } }; struct CmpLex { int num[ 2 ][ 4 ]; Rect *r[ 2 ]; inline bool operator()( Rect *a, Rect *b ) { r[ 0 ] = a; r[ 1 ] = b; for( int i = 0; i < 2; i++ ) for( int j = 0; j < 2; j++ ) { num[ i ][ j ] = r[ i ]->x[ j ]; num[ i ][ 2 + j ] = r[ i ]->y[ j ]; } for( int i = 0; i < 4; i++ ) if( num[ 0 ][ i ] != num[ 1 ][ i ] ) return num[ 0 ][ i ] < num[ 1 ][ i ]; return true; } } cmpLex; inline bool operator<( Rect a, Rect b ) { return a.y[ 0 ] < b.y[ 0 ]; } Rect* collis( Apex *t, Rect *r ) { Rect *res = NULL; if( !t->s[ 1 ].empty() && (*t->s[ 1 ].begin())->y[ 1 ] > r->y[ 0 ] ) res = *t->s[ 1 ].begin(); else { if( r->x[ 0 ] <= t->l && r->x[ 1 ] >= t->r ) { if( !t->s[ 0 ].empty() && (*t->s[ 0 ].begin())->y[ 1 ] > r->y[ 0 ] ) res = *t->s[ 0 ].begin(); } else { if( t->left->r > r->x[ 0 ] ) res = collis( t->left, r ); if( t->left->r < r->x[ 1 ] && res == NULL ) res = collis( t->right, r ); } } return res; } void insert( Apex *t, Rect *r ) { if( DB ) printf( "id %d, %d-%d\n", r->id, t->l, t->r); if( r->x[ 0 ] <= t->l && r->x[ 1 ] >= t->r ) t->s[ 1 ].insert( r ); else t->s[ 0 ].insert( r ); if( t->l < r->x[ 0 ] || t->r > r->x[ 1 ] ) { if( t->left->r > r->x[ 0 ] ) insert( t->left, r ); if( t->left->r < r->x[ 1 ] ) insert( t->right, r ); } } void erase( Apex *t, Rect *r ) { if( r->x[ 0 ] <= t->l && r->x[ 1 ] >= t->r ) t->s[ 1 ].erase( r ); else t->s[ 0 ].erase( r ); if( t->l < r->x[ 0 ] || t->r > r->x[ 1 ] ) { if( t->left->r > r->x[ 0 ] ) erase( t->left, r ); if( t->left->r < r->x[ 1 ] ) erase( t->right, r ); } } void countCountries( Apex *t ) { for( int i = 0; i < 2; i++ ) for( auto it = t->s[ i ].begin(); it != t->s[ i ].end(); it++ ) (*it)->isCountry = true; if( t->l + 1 < t->r ) { countCountries( t->left ); countCountries( t->right ); } } void print( Apex *t, int lvl ) { for( int i = 0; i < lvl; i++ ) printf( " " ); printf( "%d - %d\n", t->l, t->r ); for( int i = 0; i < 2; i++ ) { for( int j = 0; j < lvl; j++ ) printf( " " ); printf( " %d : ", i ); for( auto it = t->s[ i ].begin(); it != t->s[ i ].end(); it++ ) printf( "(%d, %d-%d) ", (*it)->id, (*it)->y[ 0 ], (*it)->y[ 1 ] ); printf( "\n" ); } if( t->left != NULL ) print( t->left, lvl + 1 ); if( t->right != NULL ) print( t->right, lvl + 1 ); } int n, size = 1; Apex *t; Rect r[ MAX_N ], *rct; vector< Rect* > res; int main() { scanf( "%d", &n ); for( int i = 0; i < n; i++ ) { for( int j = 0; j < 2; j++ ) scanf( "%d", &r[ i ].x[ j ] ); for( int j = 0; j < 2; j++ ) scanf( "%d", &r[ i ].y[ j ] ); r[ i ].id = i; } sort( r, r + n ); t = new Apex( 0, MAX_SIZE ); for( int i = 0; i < n; i++ ) { if( DB ) { printf( "i %d ->>\n", i ); print( t, 0 ); printf( "\n\n" ); } rct = collis( t, &r[ i ] ); if( rct != NULL ) { erase( t, rct ); r[ i ].x[ 0 ] = min( r[ i ].x[ 0 ], rct->x[ 0 ] ); r[ i ].y[ 0 ] = min( r[ i ].y[ 0 ], rct->y[ 0 ] ); r[ i ].x[ 1 ] = max( r[ i ].x[ 1 ], rct->x[ 1 ] ); r[ i ].y[ 1 ] = max( r[ i ].y[ 1 ], rct->y[ 1 ] ); i--; } else insert( t, &r[ i ] ); } countCountries( t ); for( int i = 0; i < n; i++ ) if( r[ i ].isCountry ) res.push_back( &r[ i ] ); sort( res.begin(), res.end(), cmpLex ); printf( "%ld\n", res.size() ); for( auto it = res.begin(); it != res.end(); it++ ) printf( "%d %d %d %d\n", (*it)->x[ 0 ], (*it)->x[ 1 ], (*it)->y[ 0 ], (*it)->y[ 1 ] ); return 0; }
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 | // Krzysztof Piesiewicz // Plemiona [B] PA 2014 #include <cstdio> #include <algorithm> #include <set> using namespace std; const int MAX_N = 100007, MAX_SIZE = 1 << 20, DB = 0; class Rect { public: int id, x[ 2 ], y[ 2 ]; bool isCountry; }; class Apex { public: struct CmpY { inline bool operator() ( Rect *a, Rect *b ) { if( a->y[ 1 ] == b->y[ 1 ] ) return a < b; return a->y[ 1 ] > b->y[ 1 ]; } }; int l, r; Apex *left, *right; set< Rect*, CmpY > s[ 2 ]; Apex( int p, int k ) : l( p ), r( k ) { if( r - l >= 2 ) { left = new Apex( l, (l + r) / 2 ); right = new Apex( (l + r) / 2, r ); } } }; struct CmpLex { int num[ 2 ][ 4 ]; Rect *r[ 2 ]; inline bool operator()( Rect *a, Rect *b ) { r[ 0 ] = a; r[ 1 ] = b; for( int i = 0; i < 2; i++ ) for( int j = 0; j < 2; j++ ) { num[ i ][ j ] = r[ i ]->x[ j ]; num[ i ][ 2 + j ] = r[ i ]->y[ j ]; } for( int i = 0; i < 4; i++ ) if( num[ 0 ][ i ] != num[ 1 ][ i ] ) return num[ 0 ][ i ] < num[ 1 ][ i ]; return true; } } cmpLex; inline bool operator<( Rect a, Rect b ) { return a.y[ 0 ] < b.y[ 0 ]; } Rect* collis( Apex *t, Rect *r ) { Rect *res = NULL; if( !t->s[ 1 ].empty() && (*t->s[ 1 ].begin())->y[ 1 ] > r->y[ 0 ] ) res = *t->s[ 1 ].begin(); else { if( r->x[ 0 ] <= t->l && r->x[ 1 ] >= t->r ) { if( !t->s[ 0 ].empty() && (*t->s[ 0 ].begin())->y[ 1 ] > r->y[ 0 ] ) res = *t->s[ 0 ].begin(); } else { if( t->left->r > r->x[ 0 ] ) res = collis( t->left, r ); if( t->left->r < r->x[ 1 ] && res == NULL ) res = collis( t->right, r ); } } return res; } void insert( Apex *t, Rect *r ) { if( DB ) printf( "id %d, %d-%d\n", r->id, t->l, t->r); if( r->x[ 0 ] <= t->l && r->x[ 1 ] >= t->r ) t->s[ 1 ].insert( r ); else t->s[ 0 ].insert( r ); if( t->l < r->x[ 0 ] || t->r > r->x[ 1 ] ) { if( t->left->r > r->x[ 0 ] ) insert( t->left, r ); if( t->left->r < r->x[ 1 ] ) insert( t->right, r ); } } void erase( Apex *t, Rect *r ) { if( r->x[ 0 ] <= t->l && r->x[ 1 ] >= t->r ) t->s[ 1 ].erase( r ); else t->s[ 0 ].erase( r ); if( t->l < r->x[ 0 ] || t->r > r->x[ 1 ] ) { if( t->left->r > r->x[ 0 ] ) erase( t->left, r ); if( t->left->r < r->x[ 1 ] ) erase( t->right, r ); } } void countCountries( Apex *t ) { for( int i = 0; i < 2; i++ ) for( auto it = t->s[ i ].begin(); it != t->s[ i ].end(); it++ ) (*it)->isCountry = true; if( t->l + 1 < t->r ) { countCountries( t->left ); countCountries( t->right ); } } void print( Apex *t, int lvl ) { for( int i = 0; i < lvl; i++ ) printf( " " ); printf( "%d - %d\n", t->l, t->r ); for( int i = 0; i < 2; i++ ) { for( int j = 0; j < lvl; j++ ) printf( " " ); printf( " %d : ", i ); for( auto it = t->s[ i ].begin(); it != t->s[ i ].end(); it++ ) printf( "(%d, %d-%d) ", (*it)->id, (*it)->y[ 0 ], (*it)->y[ 1 ] ); printf( "\n" ); } if( t->left != NULL ) print( t->left, lvl + 1 ); if( t->right != NULL ) print( t->right, lvl + 1 ); } int n, size = 1; Apex *t; Rect r[ MAX_N ], *rct; vector< Rect* > res; int main() { scanf( "%d", &n ); for( int i = 0; i < n; i++ ) { for( int j = 0; j < 2; j++ ) scanf( "%d", &r[ i ].x[ j ] ); for( int j = 0; j < 2; j++ ) scanf( "%d", &r[ i ].y[ j ] ); r[ i ].id = i; } sort( r, r + n ); t = new Apex( 0, MAX_SIZE ); for( int i = 0; i < n; i++ ) { if( DB ) { printf( "i %d ->>\n", i ); print( t, 0 ); printf( "\n\n" ); } rct = collis( t, &r[ i ] ); if( rct != NULL ) { erase( t, rct ); r[ i ].x[ 0 ] = min( r[ i ].x[ 0 ], rct->x[ 0 ] ); r[ i ].y[ 0 ] = min( r[ i ].y[ 0 ], rct->y[ 0 ] ); r[ i ].x[ 1 ] = max( r[ i ].x[ 1 ], rct->x[ 1 ] ); r[ i ].y[ 1 ] = max( r[ i ].y[ 1 ], rct->y[ 1 ] ); i--; } else insert( t, &r[ i ] ); } countCountries( t ); for( int i = 0; i < n; i++ ) if( r[ i ].isCountry ) res.push_back( &r[ i ] ); sort( res.begin(), res.end(), cmpLex ); printf( "%ld\n", res.size() ); for( auto it = res.begin(); it != res.end(); it++ ) printf( "%d %d %d %d\n", (*it)->x[ 0 ], (*it)->x[ 1 ], (*it)->y[ 0 ], (*it)->y[ 1 ] ); return 0; } |