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
#include<bits/stdc++.h>
#define ll long long
using namespace std;

struct prost {
    ll x1, y1, x2, y2;
    prost(ll _x1, ll _y1, ll _x2, ll _y2) {x1=_x1; y1=_y1; x2=_x2; y2=_y2;}
    bool operator==(prost a) {return a.x1 == x1 and a.x2 == x2 and a.y1 == y1 and a.y2 == y2;}
    bool operator<(prost a){return (x2-x1)*(y2-y1) < (a.x2-a.x1)*(a.y2-a.y1);}
};

inline ll area(prost a) {
    if(a.x2 - a.x1 <= 0 or a.y2 - a.y1 <= 0)
        return 0;
    return(a.x2 - a.x1) * (a.y2 - a.y1);
}

prost inter(prost a, prost b) {
    long long xl, yl, xp, yp;
    xl = max(a.x1, b.x1);
    xp = min(a.x2, b.x2);
    yl = max(a.y1, b.y1);
    yp = min(a.y2, b.y2);
    if(xl >= xp or yl >= yp)
        return prost(0,0,0,0);
    return prost(xl, yl, xp, yp);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    ll x, y;
    cin >> n >> x >> y;
    ll xl = 0, yl = 0, xp = x, yp = y;
    vector<prost> v;
    vector<prost> tab;
    for(int i = 0; i < n; i++) {
        ll x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if(x1 > x2)
            swap(x1, x2);
        if(y1 > y2)
            swap(y1, y2);
        tab.push_back(prost(x1,y1,x2,y2));
    }
    sort(tab.begin(), tab.end());
    for(int i = 0; i < n; i++) {
        ll x1 = tab[i].x1, x2 = tab[i].x2, y1 = tab[i].y1, y2 = tab[i].y2;
//cout << "(" << x1 << "," << y1 << ") (" << x2 << "," << y2 << ")" << endl;
        if(i == 0) {
            prost p5 = prost(x1, y1, x2, y2); //in

            prost p4 = prost(xl, y1, x1, y2); //hor
            prost p6 = prost(x2, y1, xp, y2);

            prost p2 = prost(x1, y2, x2, yp); //ver
            prost p8 = prost(x1, yl, x2, y1);

            prost p7 = prost(xl, yl, x1, y1); //out
            prost p1 = prost(xl, y2, x1, yp);
            prost p3 = prost(x2, y2, xp, yp);
            prost p9 = prost(x2, yl, xp, y1);
            
            ll inside = area(p5);
            ll hor = area(p4) + area(p6);
            ll ver = area(p2) + area(p8);
            ll outside = area(p1) + area(p3) + area(p7) + area(p9);

            ll m = max(outside, max(hor, max(ver, inside)));
            vector<prost> v1;
            if(m == inside) {
                v1.push_back(p5);
            }
            else if(m == hor) {
                v1.push_back(p4);
                v1.push_back(p6);
            }
            else if(m == hor) {
                v1.push_back(p2);
                v1.push_back(p8);
            }
            else if(m == outside) {
                v1.push_back(p1);
                v1.push_back(p3);
                v1.push_back(p7);
                v1.push_back(p9);
            }
            for(auto x : v1) {
                if(inter(x, x) == prost(0,0,0,0))
                    continue;
                v.push_back(x);
            }
        }
        else {
            prost p5 = prost(x1, y1, x2, y2); //in

            prost p4 = prost(xl, y1, x1, y2); //hor
            prost p6 = prost(x2, y1, xp, y2);

            prost p2 = prost(x1, y2, x2, yp); //ver
            prost p8 = prost(x1, yl, x2, y1);

            prost p7 = prost(xl, yl, x1, y1); //out
            prost p1 = prost(xl, y2, x1, yp);
            prost p3 = prost(x2, y2, xp, yp);
            prost p9 = prost(x2, yl, xp, y1);

            ll inside = 0;
            ll hor = 0;
            ll ver = 0;
            ll outside = 0;

            for(auto x : v) {
                inside += area(inter(p5, x));
                hor += area(inter(p4, x));
                hor += area(inter(p6, x));
                ver += area(inter(p2, x));
                ver += area(inter(p8, x));
                outside += area(inter(p1, x));
                outside += area(inter(p3, x));
                outside += area(inter(p7, x));
                outside += area(inter(p9, x));
            }
            ll m = max(outside, max(hor, max(ver, inside)));
            vector<prost> v1;
            if(m == inside) {
                for(auto x : v) {
                    v1.push_back(inter(p5, x));
                }
            }
            else if(m == hor) {
                for(auto x : v) {
                    v1.push_back(inter(p4, x));
                    v1.push_back(inter(p6, x));
                }
            }
            else if(m == hor) {
                for(auto x : v) {
                    v1.push_back(inter(p2, x));
                    v1.push_back(inter(p8, x));
                }
            }
            else if(m == outside) {
                for(auto x : v) {
                    v1.push_back(inter(p1, x));
                    v1.push_back(inter(p3, x));
                    v1.push_back(inter(p7, x));
                    v1.push_back(inter(p9, x));
                }
            }
            v.erase(v.begin(), v.end());
            for(auto x : v1) {
                if(x == prost(0,0,0,0))
                    continue;
                v.push_back(x);
            }
        }
/*cout << endl;
for(auto x : v) {
cout << x.x1 << " " << x.y1 << ", " << x.x2 << " " << x.y2 << endl;
}*/
    }
    ll s = 0;
    for(auto x : v)
        s += area(x);
    cout << s << endl;
}