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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define LONG long long
//#define LONG __int64

enum ESide
{
   eBegin = 0,
   eEnd = 1
};

class Point
{
public:
   long coord;
   long owner;
   ESide side;
   
   Point(long xy, long o, ESide s = eBegin) : coord(xy), owner(o), side(s) { };
   bool operator<(const Point& b) const 
   {
      if(coord != b.coord)
         return (coord < b.coord); 
      return owner < b.owner;
   }
};

struct pointGreater {
   bool operator() (const Point& lhc, const Point& rhc) const
   {
      if(lhc.coord != rhc.coord)
         return (lhc.coord > rhc.coord); 
      return lhc.owner < rhc.owner;
   }
};

set<Point> setX;
set<Point> setY;

class Tribe
{
public:
   set<Point>::iterator xit1, xit2;
   long x1, x2, y1, y2, myNr;
   Tribe(long xj, long xd, long yj, long yd, long o) : x1(xj), x2(xd), y1(yj), y2(yd), myNr(o) {};

   void PutTribeOnArea()
   {
      xit1 = setX.insert(Point(x1, myNr, eBegin)).first;
      xit2 = setX.insert(Point(x2, myNr, eEnd)).first;
   }

   void EraseFromArea()
   {
      setX.erase(xit1);
      setX.erase(xit2);
   }

};

struct tribePosComparator {
   bool operator() (const Tribe& lhc, const Tribe& rhc) const
   {
      if(lhc.x1 < rhc.x1)
         return true;
      else if(lhc.x1 == rhc.x1)
      {
         if(lhc.x2 < rhc.x2)
            return true;
         else if(lhc.x2 == rhc.x2)
         {
            if(lhc.y1 < rhc.y1)
               return true;
            else if(lhc.y1 == rhc.y1)
            {
               if(lhc.y2 < rhc.y2)
                  return true;
            }
         }
      }
      return false;
   }
};

vector<Tribe> vecTribes;

void PrintSolution()
{
   set<Point>::iterator sit = setX.begin();
   
   vector<long> collectedTribes;
   set<Tribe, tribePosComparator> finalSet;

   while(sit != setX.end())
   {
      if(vecTribes[sit->owner].myNr >= 0)
      {
         finalSet.insert(vecTribes[sit->owner]);
         vecTribes[sit->owner].myNr = -1L;
      }
      ++sit;
   }

   printf("%ld\n", finalSet.size());
   set<Tribe, tribePosComparator>::iterator fsit = finalSet.begin();
   for(set<Tribe, tribePosComparator>::iterator fsen = finalSet.end(); fsit != fsen; ++fsit)
      printf("%ld %ld %ld %ld\n", fsit->x1, fsit->x2, fsit->y1, fsit->y2);
}

set<Point> setAbove;
set<Point, pointGreater> setBelow;

bool ExpandStartTribe(Tribe& startTribe, Tribe& currTribe, bool& bExpandLeft)
{
   if(startTribe.myNr == currTribe.myNr)
      return false;
   if(startTribe.x1 >= currTribe.x2)
      return false;
   if(startTribe.x2 <= currTribe.x1)
      return false;

   bool bChanged = false;

   if(currTribe.y1 >= startTribe.y2) 
   {
      setAbove.insert(Point(currTribe.y1, currTribe.myNr));
   }
   else if(currTribe.y2 <= startTribe.y1) 
   {
      setBelow.insert(Point(currTribe.y2, currTribe.myNr));
   }
   else // expand current one
   {
      if(currTribe.x2 > startTribe.x2)
      {
         startTribe.x2 = currTribe.x2;
      }
      if(currTribe.x1 < startTribe.x1)
      {
         bExpandLeft = true;
         startTribe.x1 = currTribe.x1;
      }
      if(currTribe.y1 < startTribe.y1)
      {
         startTribe.y1 = currTribe.y1;
      }
      if(currTribe.y2 > startTribe.y2)
      {
         startTribe.y2 = currTribe.y2;
      }

      return true;
   }
   return false;
}

void FindSolution()
{
   set<Point>::iterator sitXstart = setX.begin();
   set<Point>::iterator sitCurrX = sitXstart;

   long xmax = 0L, xmin = 0L, ymax = 0L, ymin = 0L;
   
   bool bExpandLeft = false;

   while(sitXstart != setX.end())
   {
      setAbove.clear();
      setBelow.clear();
      sitCurrX = sitXstart;
      bExpandLeft = false;
      ++sitCurrX;


      Tribe& startTribe = vecTribes[sitXstart->owner];

      while(sitCurrX != setX.end() && sitCurrX->coord <= startTribe.x2)
      {
         if(ExpandStartTribe(startTribe, vecTribes[sitCurrX->owner], bExpandLeft))
         {
            while(!setBelow.empty() && setBelow.begin()->coord > startTribe.y1)
            {
               ExpandStartTribe(startTribe, vecTribes[setBelow.begin()->owner], bExpandLeft);
               vecTribes[setBelow.begin()->owner].EraseFromArea();
               setBelow.erase(setBelow.begin());
            }

            while(!setAbove.empty() && setAbove.begin()->coord < startTribe.y2)
            {
               ExpandStartTribe(startTribe, vecTribes[setAbove.begin()->owner], bExpandLeft);
               vecTribes[setAbove.begin()->owner].EraseFromArea();
               setAbove.erase(setAbove.begin());
            }

            Tribe& absorbed = vecTribes[sitCurrX->owner];

            do{
               ++sitCurrX;
            } while(sitCurrX != setX.end() && sitCurrX->owner == absorbed.myNr);
            absorbed.EraseFromArea();
         }
         else
            ++sitCurrX;
      }
      
      do{
         ++sitXstart;
      } while(sitXstart != setX.end() && sitXstart->owner == startTribe.myNr);
      startTribe.EraseFromArea();
      startTribe.PutTribeOnArea();

      if(bExpandLeft)
         sitXstart = setX.lower_bound(Point(startTribe.x1, 0));
   }
}

int main(int argc, char* argv[])
{
   long tribeCount = 0L;
   scanf("%ld\n", &tribeCount);
   vecTribes.reserve(tribeCount);

   long x1 = 0L, x2 = 0L, y1 = 0L, y2 = 0L;
   
   for (long tr = 0; tr < tribeCount; ++tr)
   {
      scanf("%ld %ld %ld %ld\n", &x1, &x2, &y1, &y2);
      vecTribes.push_back(Tribe(x1, x2, y1, y2, tr));
      vecTribes[tr].PutTribeOnArea();
   }

   FindSolution();

   PrintSolution();

   return 0;
}