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
#include <cstdio>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
using lint = long long;

const int lim = 500001;

lint X1[lim], X2[lim], Y1[lim], Y2[lim];

lint bestOf(lint [], lint []);

int n;
int main() {
	lint X, Y;
	scanf("%d%lld%lld", &n, &X, &Y);
	
	for(int i = 0; i < n; ++i) {
		lint x1, y1, x2, y2;
		scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
		if(x2 < x1) std::swap(x1, x2);
		if(y2 < y1) std::swap(y1, y2);
		X1[i] = x1;
		X2[i] = x2;
		Y1[i] = y1;
		Y2[i] = y2;
	}
	
	X1[n] = 0ll;
	X2[n] = X;
	Y1[n] = 0ll;
	Y2[n] = Y;
	
	printf("%lld\n", bestOf(X1, X2) * bestOf(Y1, Y2));
	
	return 0;
}

lint bestOf(lint A1[], lint A2[]) {
	auto cmpAddQ = [&A1](int a, int b){ 		
		return A1[a] == A1[b] ? a > b : A1[a] < A1[b]; 
	};
	auto addQ = std::set<int,decltype(cmpAddQ)>(cmpAddQ);
	
	auto cmpRemQ = [&A2](int a, int b){ 		
		return A2[a] == A2[b] ? a > b : A2[a] < A2[b]; 
	};
	auto remQ = std::set<int,decltype(cmpRemQ)>(cmpRemQ);
	
	for(int i = 0; i <= n; ++i) {
		addQ.insert(i);
		remQ.insert(i);
	}
	
	std::unordered_map<lint, lint> results;
	auto stack = std::set<int,decltype(cmpAddQ)>(cmpAddQ);
	
	auto updateResults = [&results, &stack] (lint d) {
		if(!stack.empty()) {
			results[(1000000ll * *stack.rbegin()) + (lint)stack.size()] += d;
		}
	};
	
	lint prevP = 0ll;
	while(!remQ.empty()) {
		if(!addQ.empty() && A1[*addQ.begin()] <= A2[*remQ.begin()]) {
			auto i = *addQ.begin();
			addQ.erase(addQ.begin());
			lint currP = A1[i];
			lint d = currP - prevP;
			updateResults(d);
			prevP = currP;
			stack.insert(i);
		} else {
			auto i = *remQ.begin();
			remQ.erase(remQ.begin());
			lint currP = A2[i];
			lint d = currP - prevP;
			updateResults(d);
			prevP = currP;		
			stack.erase(i);
		}
	}
	
	lint best = 0ll;
	for(auto [key, val] : results) {
		best = std::max(best, val);
	}
	return best;
}