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
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using ull = unsigned long long;
int n;
ull X, Y;

struct rect
{
	ull x1, y1;
	ull x2, y2;

	ull area1() const {
		return (x2 - x1) * (y2 - y1);
	}

	ull area2() const {
		return (X + x1 - x2) * (y2 - y1);
	}

	ull area3() const {
		return (x2 - x1) * (Y + y1 - y2);
	}

	ull area4() const {
		return (X + x1 - x2) * (Y + y1 - y2);
	}
};


std::vector<std::vector<rect>> calculate_rectangles(const rect& r)
{
	std::vector<std::vector<rect>> out;
	std::vector<rect> result;

	result.push_back(r);
	out.push_back(result);
	result.clear();

	if (0 != r.x1 && r.y1 != r.y2)
		result.push_back(rect{ 0, r.y1, r.x1, r.y2 });
	if (r.x2 != X && r.y1 != r.y2)
		result.push_back(rect{ r.x2, r.y1, X, r.y2 });
	out.push_back(result);
	result.clear();

	if (r.x1 != r.x2 && 0 != r.y1)
		result.push_back(rect{ r.x1, 0, r.x2, r.y1 });
	if (r.x1 != r.x2 && r.y2 != Y)
		result.push_back(rect{ r.x1, r.y2, r.x2, Y });
	out.push_back(result);
	result.clear();

	if (0 != r.x1 && 0 != r.y1)
		result.push_back(rect{ 0, 0, r.x1, r.y1 });
	if (0 != r.x1 && r.y2 != Y)
		result.push_back(rect{ 0, r.y2, r.x1, Y });
	if (r.x2 != X && r.y2 != Y)
		result.push_back(rect{ r.x2, r.y2, X, Y });
	if (r.x2 != X && 0 != r.y1)
		result.push_back(rect{ r.x2, 0, X, r.y1 });

	out.push_back(result);

	return out;
}

bool doOverlap(const rect& r1, const rect& r2)
{
	return r1.y1 < r2.y2 && r2.y1 < r1.y2 && r1.x1 < r2.x2 && r2.x1 < r1.x2;
}

rect calculate_intersection(const rect& r1, const rect& r2)
{
	const auto leftX = std::max(r1.x1, r2.x1);
	const auto rightX = std::min(r1.x2, r2.x2);
	const auto topY = std::min(r1.y2, r2.y2);
	const auto bottomY = std::max(r1.y1, r2.y1);
	return rect{ leftX, bottomY, rightX, topY };
}

struct solution
{
	std::vector<rect> rects;
	ull area;
};

solution tmp_solution;
inline void findNewOverlaps(
	const solution& existingOverlap,
	const rect& r2)
{
	tmp_solution.rects.clear();
	tmp_solution.area = 0;
	for (const auto& r1 : existingOverlap.rects)
	{
		if (doOverlap(r1, r2))
		{
			tmp_solution.rects.push_back(calculate_intersection(r1, r2));
			tmp_solution.area += tmp_solution.rects.back().area1();
		}
	}
}

inline void findNewOverlaps(
	const solution& existingOverlap,
	rect&& r2, rect&& r3)
{
	tmp_solution.rects.clear();
	tmp_solution.area = 0;
	for (const auto& r1 : existingOverlap.rects)
	{
		if (doOverlap(r1, r2))
		{
			tmp_solution.rects.push_back(calculate_intersection(r1, r2));
			tmp_solution.area += tmp_solution.rects.back().area1();
		}

		if (doOverlap(r1, r3))
		{
			tmp_solution.rects.push_back(calculate_intersection(r1, r3));
			tmp_solution.area += tmp_solution.rects.back().area1();
		}
	}
}

inline void findNewOverlaps(
	const solution& existingOverlap,
	rect&& r2, rect&& r3, rect&& r4, rect&& r5)
{
	tmp_solution.rects.clear();
	tmp_solution.area = 0;

	for (const auto& r1 : existingOverlap.rects)
	{
		if (doOverlap(r1, r2))
		{
			tmp_solution.rects.push_back(calculate_intersection(r1, r2));
			tmp_solution.area += tmp_solution.rects.back().area1();
		}

		if (doOverlap(r1, r3))
		{
			tmp_solution.rects.push_back(calculate_intersection(r1, r3));
			tmp_solution.area += tmp_solution.rects.back().area1();
		}

		if (doOverlap(r1, r4))
		{
			tmp_solution.rects.push_back(calculate_intersection(r1, r4));
			tmp_solution.area += tmp_solution.rects.back().area1();
		}

		if (doOverlap(r1, r5))
		{
			tmp_solution.rects.push_back(calculate_intersection(r1, r5));
			tmp_solution.area += tmp_solution.rects.back().area1();
		}
	}
}

inline ull get_area(const std::vector<rect>& r1)
{
	ull area = 0;
	for (const auto& r : r1)
		area += r.area1();

	return area;
}

solution overlapsArray[4000000];
solution tmpOverlapsArray[4000000];

ull calculate_area(const std::vector<rect>& rects)
{
	solution* overlaps = overlapsArray;
	solution* tmpOverlaps = tmpOverlapsArray;
	const auto max_size = std::min(2000000ll, std::max(5ll, 25000000 / (long long)rects.size()));
	//const auto max_size = 100000;
	int overlaps_size = 0;
	int tmp_overlaps_size = 0;


	auto possibleShapes = calculate_rectangles(rects[0]);
	for (const auto& r : possibleShapes)
	{
		const auto area = get_area(r);
		overlaps[overlaps_size++] = { r, area };
	}

	std::size_t i = 0;
	while (++i < rects.size())
	{
		for(int overlapIndex = 0; overlapIndex < overlaps_size; ++overlapIndex)
		{
			const auto& overlap = overlaps[overlapIndex];
			const auto& r = rects[i];

			findNewOverlaps(overlap, r);
			if (!tmp_solution.rects.empty())
				tmpOverlaps[tmp_overlaps_size++] = std::move(tmp_solution);

			findNewOverlaps(overlap, rect{ 0, r.y1, r.x1, r.y2 }, rect{ r.x2, r.y1, X, r.y2 });
			if (!tmp_solution.rects.empty())
				tmpOverlaps[tmp_overlaps_size++] = std::move(tmp_solution);

			findNewOverlaps(overlap, rect{ r.x1, 0, r.x2, r.y1 }, rect{ r.x1, r.y2, r.x2, Y });
			if (!tmp_solution.rects.empty())
				tmpOverlaps[tmp_overlaps_size++] = std::move(tmp_solution);

			findNewOverlaps(overlap,
				rect{ 0, 0, r.x1, r.y1 },
				rect{ 0, r.y2, r.x1, Y },
				rect{ r.x2, r.y2, X, Y },
				rect{ r.x2, 0, X, r.y1 });
			if (!tmp_solution.rects.empty())
				tmpOverlaps[tmp_overlaps_size++] = std::move(tmp_solution);
		}
		std::swap(tmpOverlaps, overlaps);
		overlaps_size = tmp_overlaps_size;
		tmp_overlaps_size = 0;

		if(overlaps_size > max_size)
		{
			std::sort(overlaps, overlaps + overlaps_size, [](const auto& r1, const auto& r2)
				{
					return r1.area > r2.area;
				});

			overlaps_size = max_size;
		}
	}

	ull best = 0;
	for (int overlapIndex = 0; overlapIndex < overlaps_size; ++overlapIndex)
	{
		const auto& overlap = overlaps[overlapIndex];
		best = std::max(overlap.area, best);
	}

	return best;
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::vector<rect> rects;
	std::cin >> n >> X >> Y;

	rects.resize(n);

	for (int i = 0; i < n; ++i)
	{
		std::cin >> rects[i].x1 >> rects[i].y1 >> rects[i].x2 >> rects[i].y2;
		if (rects[i].x1 > rects[i].x2)
		{
			std::swap(rects[i].x1, rects[i].x2);
			std::swap(rects[i].y1, rects[i].y2);
		}
		if (rects[i].y1 > rects[i].y2)
		{
			std::swap(rects[i].y1, rects[i].y2);
		}
	}

	std::cout << calculate_area(rects) << std::endl;

	return 0;
}