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
//
//  main.cpp
//  PA_2014_PLE
//
//  Created by Michal Kowalski on 16/05/14.
//  Copyright (c) 2014 Michal Kowalski. All rights reserved.
//

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <functional>
#include <algorithm>
#include <set>

using namespace std;

typedef pair<int,int> PINT;

///#define DBG 0

#define MAX_PLE 100000

int X1[MAX_PLE+10];
int X2[MAX_PLE+10];
int Y1[MAX_PLE+10];
int Y2[MAX_PLE+10];

int N;

set<pair<int,int> > SX;
set<pair<int,int> > SY;
set<int> SR; /// result
void print_SX();
void print_SY();
bool isAinB(int a,int b);
void remove_all_in();
int main()
{
    scanf("%d",&N);
    for(int x=1;x<=N;++x)
    {
        scanf("%d %d %d %d",&X1[x],&X2[x],&Y1[x],&Y2[x]);
		X1[x]=X1[x]*2+1;
		X2[x]=X2[x]*2;
		Y1[x]=Y1[x]*2+1;
		Y2[x]=Y2[x]*2;
		SX.insert(pair<int,int>(X1[x],x));
		SX.insert(pair<int,int>(X2[x],x));
    }

    set<pair<int,int> >::iterator itL,itU,itSX;
    itSX = SX.begin();

	while(itSX != SX.end()){
/*		print_SX();*/
        if ((*itSX).first%2 == 0)///koniec kwadratu
        {
            int id = (*itSX).second;
			#ifdef DBG
				print_SY();
			#endif
            itL = SY.lower_bound(pair<int,int>(Y1[id], 0));
            itU = SY.upper_bound(pair<int,int>(Y2[id],MAX_PLE+10));
			vector<int> common;
			set<int> commonSet;
			commonSet.clear();
            while(itL != itU)
            {
				#ifdef DBG
					printf("%d+",(*itL).second);
				#endif
				commonSet.insert((*itL).second);
                ++itL;
            }
            if (commonSet.size() == 0 || commonSet.size() == 1)
            {
    			SY.erase(pair<int,int>(Y1[id],id));
    			SY.erase(pair<int,int>(Y2[id],id));
        		++itSX;
        		continue;
            }
			int minX,maxX,minY,maxY;
			int newId = -1;
			set<int>::iterator commonIt;
			//printf("commonsetSize: %d\n",commonSet.size());
			commonIt = commonSet.begin();

			id = (*commonIt);
			newId = id;
			minX = X1[id];maxX = X2[id];
			minY = Y1[id];maxY = Y2[id];
			SY.erase(pair<int,int>(Y1[id],id));
			SY.erase(pair<int,int>(Y2[id],id));
			SX.erase(pair<int,int>(X1[id],id));
			SX.erase(pair<int,int>(X2[id],id));

			commonIt++;
			while(commonIt != commonSet.end())
			{
				id = (*commonIt);
				SY.erase(pair<int,int>(Y1[id],id));
				SY.erase(pair<int,int>(Y2[id],id));
				SX.erase(pair<int,int>(X1[id],id));
				SX.erase(pair<int,int>(X2[id],id));
				minX = min(minX,X1[id]);
				maxX = max(maxX,X2[id]);
				minY = min(minY,Y1[id]);
				maxY = max(maxY,Y2[id]);
				commonIt++;
			}
			if (newId > 0)
			{
				X1[newId] = minX;
				X2[newId] = maxX;
				Y1[newId] = minY;
				Y2[newId] = maxY;
				#ifdef DBG
					printf("Dodaje PLE z %d %d %d %d dla id %d\n",minX,maxX,minY,maxY,newId);
				#endif
				SX.insert(pair<int,int>(X1[newId],newId));
				SX.insert(pair<int,int>(X2[newId],newId));
				SY.clear();
				itSX = SX.begin();
				continue;
			}
            //printf("\n");
        }
        else
        {
			#ifdef DBG
        		print_SX();
			#endif
            int id = (*itSX).second;
/*        	printf("%d\n",id);*/
            SY.insert(pair<int,int>(Y1[id],id));
            SY.insert(pair<int,int>(Y2[id],id));
        }
		++itSX;
    }
	itSX = SX.begin();
	remove_all_in();
	vector<pair<PINT,PINT > > vOUT;
	set<int>::iterator itR = SR.begin();
	while(itR != SR.end())
	{
			int id = (*itR);
			vOUT.push_back(
					make_pair(
							make_pair((X1[id]-1)/2,(X2[id])/2),
							make_pair((Y1[id]-1)/2,(Y2[id])/2)
							)
					);
		itR++;
	}
	sort(vOUT.begin(),vOUT.end());
	printf("%d\n",vOUT.size());
	for(int x=0;x<vOUT.size();++x)
		printf("%d %d %d %d\n",vOUT[x].first.first,vOUT[x].first.second,vOUT[x].second.first,vOUT[x].second.second);
    return 0;
}

void remove_all_in()
{
	set<PINT>::iterator itSX = SX.begin();
	while(itSX != SX.end())
	{
		SR.insert((*itSX).second);
		itSX++;
	}
	set<int>::iterator itR = SR.begin();
	set<int> newR;
	while(itR != SR.end())
	{
		set<int>::iterator itR2 = SR.begin();
		bool isIn = false;
		while(itR2 != SR.end())
		{
			int a = (*itR);
			int b = (*itR2);
			if (a != b)
			{
				if (isAinB(a,b))
				{
					isIn = true;
					break;
				}
			}
			itR2++;
		}
		if (!isIn)
		{
			newR.insert((*itR));
		}
		++itR;
	}
	SR = newR;
}

bool isAinB(int a,int b)
{
	return (X1[b] <= X1[a] && X2[a] <= X2[b] && Y1[b] <= Y1[a] && Y2[a] <= Y2[b]);
}

void print_SX()
{
	printf("===SX===\n");
    set<pair<int,int> >::iterator itSX;
    itSX = SX.begin();
	while(itSX != SX.end()){
		printf("%d ",(*itSX).second);
		++itSX;
	}
	printf("========\n");
}

void print_SY()
{
	printf("===SY===\n");
    set<pair<int,int> >::iterator itSX;
    itSX = SY.begin();
	while(itSX != SY.end()){
		printf("<%d,%d>  ",(*itSX).first,(*itSX).second);
		++itSX;
	}
	printf("========\n");
}