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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Author: Piotr Grzegorczyk (ud2@interia.eu)
// Task: PA2018/MAG

#include <bits/stdc++.h>
using namespace std;

 // TODO: optymalizacja, precyzja :(

typedef long double dbl;

dbl PI = 4.0*atan(1.0);
dbl PI_2 = 0.5*PI;
dbl _2_PI = 2.0*PI;

dbl A_MIN = 0.0;
dbl A_MAX = _2_PI;

class Set {
public:
	Set() {};
	
	Set(dbl a, dbl b) {
		create(a, b);
	};
	
	bool isempty() const {
		return m.size() == 0;
	}

	bool isfull() {
		return (m.size() == 1) && (m.count(A_MIN)==1) && (m[A_MIN] == A_MAX);
	}
	
	void create(dbl a, dbl b) {
		if (a == b)
			return;
			
		if (a > b) {
			create(a, A_MAX);
			create(A_MIN, b);
			return;
		}
		
		m[a] = b;
	}
	
	void mix(map<dbl, int> & m, const map<dbl, dbl> & m2) {
	
		for (auto it=m2.begin(); it!=m2.end(); ++it) {
			if (m.count(it->first)==0)
				m[it->first] = 1;
			else
				m[it->first]++;

			if (m.count(it->second)==0)
				m[it->second] = -1;
			else
				m[it->second]--;
		}
	}
	
	Set & operator &= (const Set & s) {
		map<dbl, int> mx;
		mix(mx, m);
		mix(mx, s.m);

		m.clear();
		
		int c = 0;
		dbl a, b;

		for (auto it=mx.begin(); it!=mx.end(); ++it) {			
			dbl x = it->first;
			int dc = it->second;
			int cp = c;
			c += dc;
			
			if (c==2 && cp<2) {
				a = x;
			} else if (c < 2 && cp==2) {
				b = x;
				if (b > a) 
					m[a] = b;
			}
		}
		
		return *this;
	}
	
	Set & operator |= (const Set & s) {
		map<dbl, int> mx;
		mix(mx, m);
		mix(mx, s.m);

		m.clear();
		
		int c = 0;
		dbl a, b;

		for (auto it=mx.begin(); it!=mx.end(); ++it) {			
			dbl x = it->first;
			int dc = it->second;
			int cp = c;
			c += dc;
			
			if (c>=1 && cp==0) {
				a = x;
			} else if (c==0 && cp>0) {
				b = x;
				if (b > a) 
					m[a] = b;
			}
		}

		return *this;
	}
	
	Set & operator ^= (const Set & s) {
		map<dbl, int> mx;
		mix(mx, m);
		mix(mx, s.m);

		m.clear();
		
		int c = 0;
		dbl a, b;

		for (auto it=mx.begin(); it!=mx.end(); ++it) {			
			dbl x = it->first;
			int dc = it->second;
			int cp = c;
			c += dc;
			
			if (c==1 && cp!=1) {
				a = x;
			} else if (c==2 && cp==1 || c==0 && cp==1) {
				b = x;
				if (b > a) 
					m[a] = b;
			}
		}

		return *this;
	}

	void print() {
		cout << "SET ";
		for (auto it=m.begin(); it!=m.end(); ++it) {
			cout << "[" << it->first << ", " << it->second << "] ";
		} 
		cout << endl;
	}

	map<dbl, dbl> m;
};

Set EMPTY;
Set FULL(A_MIN, A_MAX);

class Point {
public:
	Point(dbl x, dbl y) : x(x), y(y) {}

	void print() const {
		cout << "PT " << x << ", " << y << endl;
	}

	dbl x, y;
};

Point middle(const Point & p1, const Point & p2) {
	return Point( 0.5*(p1.x + p2.x), 0.5*(p1.y + p2.y) );
}

vector<Point> vpta, vptb;

#define EPS 1e-16

dbl fix(dbl a) {
	while (a < 0)
		a += _2_PI;
	while (a >= _2_PI)
		a -= _2_PI;

	long long x = 0.5 + a / 1e-8;
	a = x * 1e-8;	

	return a;
}

Set calc(const Point & p, const Point & p0) {
	dbl dx = p.x - p0.x;
	dbl dy = p.y - p0.y;
	
	if (fabs(dx) + fabs(dy) < EPS)
		return EMPTY;
		
	dbl a = atan2(dy, dx);

	return move( Set( fix(a - PI_2), fix(a + PI_2) ) );
}

Set calc(const Point & p, int i) {
	Set s1 = calc(p, vpta[i]);
	Set s2 = calc(p, vptb[i]);
	
	s1 &= s2;
	
	return move(s1);
}

vector<Set> calc(const Point & p) {
	vector<Set> v;
	for (int i=0; i<vpta.size(); i++)
		v.push_back( calc(p, i) );

	return move(v);
}

bool issafe(const vector<Set> & v) {
	Set s = EMPTY;
	for (int i=0; i<v.size(); i++)
		s |= v[i];
	return s.isfull();
}

dbl R = 0.0;

void f(
	const Point & p1, 
	const Point & p2, 
	const Point & p3,
	const Point & p4, 
	const vector<Set> & v1,
	const vector<Set> & v2,
	const vector<Set> & v3,
	const vector<Set> & v4,
	bool s1,
	bool s2,
	bool s3,
	bool s4,
	int depth) 
{
	Set NN = FULL;
	Set CC = EMPTY;	
	
	int sign = 0;
	
	for (int i=0; i<vpta.size(); i++) {
			
		Set N = EMPTY;
		N |= v1[i];
		N |= v2[i];
		N |= v3[i];
		N |= v4[i];
		N ^= FULL;
		NN &= N;
		
		Set C = FULL;
		C &= v1[i];
		C &= v2[i];
		C &= v3[i];
		C &= v4[i];
		CC |= C;				
	}

	if (!NN.isempty()) {
		// pole niebezpieczne
	} else if (CC.isfull()) {
		// pole bezpieczne
		R += (p4.x - p1.x)*(p4.y - p1.y);
	} else {

		// TODO: poprawic
		
		if (depth > 10) {
			int c = 0;
			if (s1) c++;
			if (s2) c++;
			if (s3) c++;
			if (s4) c++;
					
			R += 0.25*c*(p4.x - p1.x)*(p4.y - p1.y);
			return ;
		}
			
		Point p12 = move( middle(p1, p2) );
		Point p34 = move( middle(p3, p4) );
		Point p13 = move( middle(p1, p3) );
		Point p24 = move( middle(p2, p4) );
		Point p14 = move( middle(p1, p4) );
		
		vector<Set> v12 = move( calc(p12) );
		vector<Set> v34 = move( calc(p34) );
		vector<Set> v13 = move( calc(p13) );
		vector<Set> v24 = move( calc(p24) );
		vector<Set> v14 = move( calc(p14) );
		
		bool s12 = issafe(v12);
		bool s34 = issafe(v34);
		bool s13 = issafe(v13);
		bool s24 = issafe(v24);
		bool s14 = issafe(v14);

		f(p1,  p12, p13, p14, v1,  v12, v13, v14, s1,  s12, s13, s14, depth+1);
		f(p12, p2,  p14, p24, v12, v2,  v14, v24, s12, s2,  s14, s24, depth+1);		
		f(p13, p14, p3,  p34, v13, v14, v3,  v34, s13, s14, s3,  s34, depth+1);		
		f(p14, p24, p34, p4,  v14, v24, v34,  v4, s14, s24, s34,  s4, depth+1);	
	}
}

int main() {

	int n;
	scanf("%d", &n);
			
	for (int i=0; i<n; i++) {
		int ax, ay, bx, by;
	
		scanf("%d%d%d%d", &ax, &ay, &bx, &by);

		vpta.push_back( Point(ax, ay) );
		vptb.push_back( Point(bx, by) );
	}

	dbl x_min = 500;
	dbl y_min = 500;
	dbl x_max = -500;
	dbl y_max = -500;

	for (int i=0; i<n; i++) {
		x_min = min(x_min, vpta[i].x);
		x_min = min(x_min, vptb[i].x);

		y_min = min(y_min, vpta[i].y);
		y_min = min(y_min, vptb[i].y);

		x_max = max(x_max, vpta[i].x);
		x_max = max(x_max, vptb[i].x);

		y_max = max(y_max, vpta[i].y);
		y_max = max(y_max, vptb[i].y);
	}

	Point p1(x_min, y_min);
	Point p2(x_max, y_min);
	Point p3(x_min, y_max);
	Point p4(x_max, y_max);

	vector<Set> v1 = move( calc(p1) );
	vector<Set> v2 = move( calc(p2) );
	vector<Set> v3 = move( calc(p3) );
	vector<Set> v4 = move( calc(p4) );

	bool s1 = issafe(v1);
	bool s2 = issafe(v1);
	bool s3 = issafe(v1);
	bool s4 = issafe(v1);

	f(p1, p2, p3, p4, v1, v2, v3, v4, s1, s2, s3, s4, 0);
			
	printf("%.9Lf\n", R);
//	printf("%.9lf\n", R);

	return 0;
}