#include <bits/stdc++.h>
using namespace std;
long long X, Y;
int N;
long long WYNIK1, WYNIK2;
long long dp[1000004];
bool DEBUG = false;
struct interval{
long long S, T;
int e1, e2;
};
vector<interval> A;
vector<interval> B;
struct event{
int nr;
bool B;
long long x;
};
vector<event> eventy;
bool operator<(event const &a, event const &b){
if(a.nr == b.nr){
if(!a.B) return false;
if(b.B) return false;
return true;
} else {
return a.x < b.x;
}
}
void input(){
cin.tie(NULL);
ios_base::sync_with_stdio(0);
cin >> N >> X >> Y;
long long x1, y1, x2, y2;
interval I1, I2;
for(int i = 0; i < N; i++){
cin >> x1 >> y1 >> x2 >> y2;
if(x1 > x2) swap(x1, x2);
if(y1 > y2) swap(y1, y2);
I1.S = x1;
I1.T = x2;
I2.S = y1;
I2.T = y2;
A.push_back(I1);
B.push_back(I2);
}
}
long long process(){
eventy.clear();
for(int i = 0; i < N; i++){
event E;
E.nr = i;
E.B = true;
E.x = A[i].S;
eventy.push_back(E);
E.B = false;
E.x = A[i].T;
eventy.push_back(E);
}
sort(eventy.begin(), eventy.end());
for(int i = 0; i < eventy.size(); i++){
if(eventy[i].B){
A[eventy[i].nr].e1 = i;
} else {
A[eventy[i].nr].e2 = i;
}
}
long long RES;
dp[0] = eventy[0].x;
RES = dp[0];
stack<pair<int, int> > intervals;
stack<pair<int, int> > last;
for(int i = 0; i < eventy.size(); i++){
if(i == eventy.size() - 1){
dp[i + 1] = X - eventy[i].x;
} else {
dp[i + 1] = eventy[i + 1].x - eventy[i].x;
}
int beg = A[eventy[i].nr].e1;
int end = A[eventy[i].nr].e2;
if(eventy[i].B){
while(!intervals.empty() && intervals.top().second < end){
intervals.pop();
}
intervals.push(make_pair(beg, end));
} else {
while(!last.empty() && last.top().second > beg){
beg = min(beg, last.top().first);
last.pop();
}
last.push(make_pair(beg, end));
if(!intervals.empty() && intervals.top().second == i){
intervals.pop();
}
if(intervals.empty() || intervals.top().first < beg){
dp[i + 1] += dp[beg];
}
}
RES = max(RES, dp[i + 1]);
}
return RES;
}
void swap_all(){
for(int i = 0; i < N; i++){
swap(A[i], B[i]);
}
swap(X, Y);
}
int main(){
input();
WYNIK1 = process();
swap_all();
WYNIK2 = process();
cout << WYNIK1 * WYNIK2 << endl;
}
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 | #include <bits/stdc++.h> using namespace std; long long X, Y; int N; long long WYNIK1, WYNIK2; long long dp[1000004]; bool DEBUG = false; struct interval{ long long S, T; int e1, e2; }; vector<interval> A; vector<interval> B; struct event{ int nr; bool B; long long x; }; vector<event> eventy; bool operator<(event const &a, event const &b){ if(a.nr == b.nr){ if(!a.B) return false; if(b.B) return false; return true; } else { return a.x < b.x; } } void input(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> N >> X >> Y; long long x1, y1, x2, y2; interval I1, I2; for(int i = 0; i < N; i++){ cin >> x1 >> y1 >> x2 >> y2; if(x1 > x2) swap(x1, x2); if(y1 > y2) swap(y1, y2); I1.S = x1; I1.T = x2; I2.S = y1; I2.T = y2; A.push_back(I1); B.push_back(I2); } } long long process(){ eventy.clear(); for(int i = 0; i < N; i++){ event E; E.nr = i; E.B = true; E.x = A[i].S; eventy.push_back(E); E.B = false; E.x = A[i].T; eventy.push_back(E); } sort(eventy.begin(), eventy.end()); for(int i = 0; i < eventy.size(); i++){ if(eventy[i].B){ A[eventy[i].nr].e1 = i; } else { A[eventy[i].nr].e2 = i; } } long long RES; dp[0] = eventy[0].x; RES = dp[0]; stack<pair<int, int> > intervals; stack<pair<int, int> > last; for(int i = 0; i < eventy.size(); i++){ if(i == eventy.size() - 1){ dp[i + 1] = X - eventy[i].x; } else { dp[i + 1] = eventy[i + 1].x - eventy[i].x; } int beg = A[eventy[i].nr].e1; int end = A[eventy[i].nr].e2; if(eventy[i].B){ while(!intervals.empty() && intervals.top().second < end){ intervals.pop(); } intervals.push(make_pair(beg, end)); } else { while(!last.empty() && last.top().second > beg){ beg = min(beg, last.top().first); last.pop(); } last.push(make_pair(beg, end)); if(!intervals.empty() && intervals.top().second == i){ intervals.pop(); } if(intervals.empty() || intervals.top().first < beg){ dp[i + 1] += dp[beg]; } } RES = max(RES, dp[i + 1]); } return RES; } void swap_all(){ for(int i = 0; i < N; i++){ swap(A[i], B[i]); } swap(X, Y); } int main(){ input(); WYNIK1 = process(); swap_all(); WYNIK2 = process(); cout << WYNIK1 * WYNIK2 << endl; } |
English