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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
typedef short int sint;
#define FOR(x, b, e) for(int x=(b); x<=(e); ++x)
#define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define ALL(c) c.begin(),c.end()
#define SIZE(x) ((int)((x).size()))
#define PB push_back
#define ST first
#define ND second
#define DEBUG 1
#define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }}
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));

const int K = 4;
const int N = 501001;
int X, Y;
int n;

struct square {
	int xl,xr,yb,yt;
	void print() const {
		if (DEBUG) {
			printf(" [%d;%d] x [%d;%d]\n",xl, xr, yb, yt);
		}
	}
	ll getSize() const {
		return (ll)(xr - xl) * (yt - yb);
	}
	bool operator<(const square& a) const {
		if (xl != a.xl) {
			return xl < a.xl;
		}
		if (xr != a.xr) {
			return xr < a.xr;
		}
		if (yb != a.yb) {
			return yb < a.yb;
		}
		return yt < a.yt;
	}
	string getHash() const {
		return to_string(xl) + ";" + to_string(xr) + ":" + to_string(yb) + ";" + to_string(yt);
	}
};
square tab[N];

inline bool isCross(const square &a, const square &b) {
	if (a.xr <= b.xl || a.xl >= b.xr) {
		return false;
	}
	if (a.yb >= b.yt || a.yt <= b.yb) {
		return false;
	}
	return true;
}

inline square cross(const square &a, const square &b) {
	return {max(a.xl, b.xl), min(a.xr, b.xr), max(a.yb, b.yb), min(a.yt, b.yt)};
}

inline vector<square> getSquares(const square &a, int typ) {
	// take care of points on edges of TORUS, when x = 0 | y = 0
	vector<square> res;
	switch (typ) {
		case 0: {
			if (a.xl > 0)
				res.PB({0, a.xl, a.yt, Y});
			if (a.xl > 0 && a.yb > 0)
				res.PB({0, a.xl, 0, a.yb});
			if (a.yb > 0)
				res.PB({a.xr, X, 0, a.yb});
			res.PB({a.xr, X, a.yt, Y});
			return res;
		}
		case 1: {
			res.PB(a);
			return res;
		}
		case 2: {
			if (a.xl > 0)
				res.PB({0, a.xl, a.yb, a.yt});
			res.PB({a.xr, X, a.yb, a.yt});
			return res;
		}
		default: {
		// case 3: {
			if (a.yb > 0)
				res.PB({a.xl, a.xr, 0, a.yb});
			res.PB({a.xl, a.xr, a.yt, Y});
			return res;
		}
	}
}

struct State {
	vector<square> squares;
	bool crosses(const square &a, int typ) const {
		auto newSquares = getSquares(a, typ);
		for (auto newSquare : newSquares) {
			for (auto stateSquare : squares) {
				if (isCross(newSquare, stateSquare)) {
					return true;
				}
			}
		}
		return false;
	}

	ll getSize() const {
		ll res = 0;
		for (auto stateSquare : squares) {
			res += stateSquare.getSize();
		}
		return res;
	}
	string getHash() const {
		string res = squares[0].getHash();
		for (int i=1;i<SIZE(squares) ; ++i) {
			res = res + ":" + squares[i].getHash();
		}
		return res;
	}
};

State addSquare(const State& a, const square &b, int typ) {
	State newState;
	auto newSquares = getSquares(b, typ);
	for (auto newSquare : newSquares) {
		for (auto stateSquare : a.squares) {
			if (isCross(newSquare, stateSquare)) {
				newState.squares.PB(cross(newSquare, stateSquare));
			}
		}
	}
	sort(ALL(newState.squares));
	return newState;
}
vector<State> states;
unordered_map<string, int> seen;

void printState(const State &a) {
	if (DEBUG) {
		for (auto stateSquare : a.squares) {
			printf("\t\t"); stateSquare.print();
		}
	}
}

int main() {
	scanf("%d %d %d", &n, &X, &Y);
	REP(i, n) {
		int x1,x2,y1,y2;
		scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
		tab[i] = {min(x1, x2), max(x1, x2), min(y1,y2), max(y1, y2)};
	}
	priority_queue<pair<ll, pii>> Q;
	square initSquare({0, X, 0, Y});
	State initState;
	initState.squares.PB(initSquare);
	states.PB(initState);
	seen[initState.getHash()] = 0;
	Q.push({(ll)X * Y, {0, SIZE(states) - 1}});
	int stepCnt = 0;
	while (!Q.empty()) {
		auto top = Q.top(); Q.pop();
		int pos = -top.ND.ST;
		if (pos == n) {
			printf("%lld\n", top.ST);
			return 0;
		}
		auto state = states[top.ND.ND];
		if (seen[state.getHash()] > pos) {
			continue;
		}
		++stepCnt;
		REP(j, K) {
			if (state.crosses(tab[pos], j)) {
				State newState = addSquare(state, tab[pos], j);
				string newHash = newState.getHash();
				int powPos = pos + 1;
				if ((seen.find(newHash) == seen.end() || seen[newHash] < powPos)) {
					seen[newHash] = powPos;
					states.PB(newState);
					Q.push({newState.getSize(), {-powPos, SIZE(states) - 1}});
				}
			}
		}
	}
	return 0;
}