#include <bits/stdc++.h> using namespace std; #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; template<typename T> struct Point { T x, y; Point() {} Point(T _x, T _y) : x(_x), y(_y) {} Point rotated(double fi) const { return Point(x * cos(fi) - y * sin(fi), x * sin(fi) + y * cos(fi)); } void rotate(double fi) { (*this) = this->rotated(fi); } void operator+=(const Point &p) { x += p.x, y += p.y; } double norm() const { return sqrt(x * x + y * y); } double norm_squared() const { return x * x + y * y; } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } double distance_to(const Point &p) const { return (*this - p).norm(); } double distance_to_squared(const Point &p) const { return (*this - p).norm_squared(); } double angle() const { return atan2(y, x); } double angle2() const { // ??? result always positive? double k = atan2(y, x); if (k < 0) { k += 0.5 * M_PI; } return k; } void operator*=(double a) { x *= a, y *= a; } Point operator*(double a) const { Point result = *this; result *= a; return result; } Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } bool operator<(const Point &p) const { if (x == p.x) { return y < p.y; } return x < p.x; } bool operator==(const Point &p) const { return x == p.x && y == p.y; } }; template<typename T> std::ostream &operator<<(std::ostream &os, const Point<T> &p) { return os << "Point(" << p.x << ", " << p.y << ")"; } template<typename T> T my_abs(const T &x) { if (x < 0) { return -x; } return x; } template<typename T> struct Segment : public pair<Point<T>, Point<T>> { // Can be used as rectangle as well. Segment(const Point<T> &p1, const Point<T> &p2) : pair<Point<T>, Point<T>>(p1, p2) { } T area_of_rectangle_between() const { //rectangle T diff_x = my_abs(this->first.x - this->second.x); T diff_y = my_abs(this->first.y - this->second.y); return diff_x * diff_y; } bool is_forward() const { return this->second.x > this->first.x && this->second.y > this->first.y; } vector <Segment<T>> split_to_rectangles_by_rolling_over_torus(bool roll_over_x, bool roll_over_y, T max_x, T max_y) const { //rectangle vector <T> xs, ys; if (roll_over_x) { xs.PB(0); xs.PB(min(this->first.x, this->second.x)); xs.PB(max(this->first.x, this->second.x)); xs.PB(max_x); } else { xs.PB(min(this->first.x, this->second.x)); xs.PB(max(this->first.x, this->second.x)); } if (roll_over_y) { ys.PB(0); ys.PB(min(this->first.y, this->second.y)); ys.PB(max(this->first.y, this->second.y)); ys.PB(max_y); } else { ys.PB(min(this->first.y, this->second.y)); ys.PB(max(this->first.y, this->second.y)); } vector <Segment> result; for (int i = 0; i < SZ(xs); i += 2) { for (int j = 0; j < SZ(ys); j += 2) { result.PB(Segment(Point(xs[i], ys[j]), Point(xs[i + 1], ys[j + 1]))); } } return result; } Segment rectangles_intersection(const Segment &rectangle) { // rectangle T left_x1 = min(this->first.x, this->second.x); T left_x2 = min(rectangle.first.x, rectangle.second.x); T right_x1 = max(this->first.x, this->second.x); T right_x2 = max(rectangle.first.x, rectangle.second.x); T bottom_y1 = min(this->first.y, this->second.y); T bottom_y2 = min(rectangle.first.y, rectangle.second.y); T top_y1 = max(this->first.y, this->second.y); T top_y2 = max(rectangle.first.y, rectangle.second.y); return Segment( Point(max(left_x1, left_x2), max(bottom_y1, bottom_y2)), Point(min(right_x1, right_x2), min(top_y1, top_y2)) ); } }; template<typename T> std::ostream &operator<<(std::ostream &os, const Segment<T> &p) { return os << "Segment(" << p.first << ", " << p.second << ")"; } ll solve_one(const vector <Segment<ll>> &input_segments, ll max_x, ll max_y) { vector <Segment<ll>> result; result.PB(Segment(Point<ll>(0, 0), Point<ll>(max_x, max_y))); for (auto &s: input_segments) { ll best_area = 0; vector <Segment<ll>> chosen; REP(bx, 2) { REP(by, 2) { // DEB("bxy: " << bx << ", " << by << "\n"); auto rectangles = s.split_to_rectangles_by_rolling_over_torus(bool(bx), bool(by), max_x, max_y); ll total_area = 0; for (auto &rectangle : rectangles) { // DEB(rectangle << ", "); total_area += rectangle.area_of_rectangle_between(); } // DEB("\n"); // DEB("total_area: " << total_area << "\n"); vector <Segment<ll>> intersected; ll intersected_area = 0; for (auto &rectangle: rectangles) { for (auto &other: result) { auto inter = rectangle.rectangles_intersection(other); if (inter.is_forward()) { intersected.PB(inter); intersected_area += inter.area_of_rectangle_between(); } } } if (intersected_area > best_area) { best_area = intersected_area; chosen = intersected; } } } result = chosen; } ll result_area = 0; for (auto &rectangle: result) { result_area += rectangle.area_of_rectangle_between(); } return result_area; } void inline one() { int n; ll max_x, max_y; cin >> n >> max_x >> max_y; vector <Segment<ll>> input_segments; REP (i, n) { Point<ll> p1, p2; cin >> p1.x >> p1.y >> p2.x >> p2.y; Segment<ll> s(p1, p2); if (!s.is_forward()) { s = Segment<ll>(p2, p1); } // DEB(s << "\n"); // DEB("area: " << s.area_of_rectangle_between() << "\n"); input_segments.PB(s); } sort(input_segments.begin(), input_segments.end()); ll result_area = solve_one(input_segments, max_x, max_y); REP(i, 4) { random_shuffle(input_segments.begin(), input_segments.end()); ll area = solve_one(input_segments, max_x, max_y); DEB("random_area: " << area << "\n"); result_area = max(result_area, area); } cout << result_area << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); //int z; cin >> z; while(z--) one(); }
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 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; template<typename T> struct Point { T x, y; Point() {} Point(T _x, T _y) : x(_x), y(_y) {} Point rotated(double fi) const { return Point(x * cos(fi) - y * sin(fi), x * sin(fi) + y * cos(fi)); } void rotate(double fi) { (*this) = this->rotated(fi); } void operator+=(const Point &p) { x += p.x, y += p.y; } double norm() const { return sqrt(x * x + y * y); } double norm_squared() const { return x * x + y * y; } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } double distance_to(const Point &p) const { return (*this - p).norm(); } double distance_to_squared(const Point &p) const { return (*this - p).norm_squared(); } double angle() const { return atan2(y, x); } double angle2() const { // ??? result always positive? double k = atan2(y, x); if (k < 0) { k += 0.5 * M_PI; } return k; } void operator*=(double a) { x *= a, y *= a; } Point operator*(double a) const { Point result = *this; result *= a; return result; } Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } bool operator<(const Point &p) const { if (x == p.x) { return y < p.y; } return x < p.x; } bool operator==(const Point &p) const { return x == p.x && y == p.y; } }; template<typename T> std::ostream &operator<<(std::ostream &os, const Point<T> &p) { return os << "Point(" << p.x << ", " << p.y << ")"; } template<typename T> T my_abs(const T &x) { if (x < 0) { return -x; } return x; } template<typename T> struct Segment : public pair<Point<T>, Point<T>> { // Can be used as rectangle as well. Segment(const Point<T> &p1, const Point<T> &p2) : pair<Point<T>, Point<T>>(p1, p2) { } T area_of_rectangle_between() const { //rectangle T diff_x = my_abs(this->first.x - this->second.x); T diff_y = my_abs(this->first.y - this->second.y); return diff_x * diff_y; } bool is_forward() const { return this->second.x > this->first.x && this->second.y > this->first.y; } vector <Segment<T>> split_to_rectangles_by_rolling_over_torus(bool roll_over_x, bool roll_over_y, T max_x, T max_y) const { //rectangle vector <T> xs, ys; if (roll_over_x) { xs.PB(0); xs.PB(min(this->first.x, this->second.x)); xs.PB(max(this->first.x, this->second.x)); xs.PB(max_x); } else { xs.PB(min(this->first.x, this->second.x)); xs.PB(max(this->first.x, this->second.x)); } if (roll_over_y) { ys.PB(0); ys.PB(min(this->first.y, this->second.y)); ys.PB(max(this->first.y, this->second.y)); ys.PB(max_y); } else { ys.PB(min(this->first.y, this->second.y)); ys.PB(max(this->first.y, this->second.y)); } vector <Segment> result; for (int i = 0; i < SZ(xs); i += 2) { for (int j = 0; j < SZ(ys); j += 2) { result.PB(Segment(Point(xs[i], ys[j]), Point(xs[i + 1], ys[j + 1]))); } } return result; } Segment rectangles_intersection(const Segment &rectangle) { // rectangle T left_x1 = min(this->first.x, this->second.x); T left_x2 = min(rectangle.first.x, rectangle.second.x); T right_x1 = max(this->first.x, this->second.x); T right_x2 = max(rectangle.first.x, rectangle.second.x); T bottom_y1 = min(this->first.y, this->second.y); T bottom_y2 = min(rectangle.first.y, rectangle.second.y); T top_y1 = max(this->first.y, this->second.y); T top_y2 = max(rectangle.first.y, rectangle.second.y); return Segment( Point(max(left_x1, left_x2), max(bottom_y1, bottom_y2)), Point(min(right_x1, right_x2), min(top_y1, top_y2)) ); } }; template<typename T> std::ostream &operator<<(std::ostream &os, const Segment<T> &p) { return os << "Segment(" << p.first << ", " << p.second << ")"; } ll solve_one(const vector <Segment<ll>> &input_segments, ll max_x, ll max_y) { vector <Segment<ll>> result; result.PB(Segment(Point<ll>(0, 0), Point<ll>(max_x, max_y))); for (auto &s: input_segments) { ll best_area = 0; vector <Segment<ll>> chosen; REP(bx, 2) { REP(by, 2) { // DEB("bxy: " << bx << ", " << by << "\n"); auto rectangles = s.split_to_rectangles_by_rolling_over_torus(bool(bx), bool(by), max_x, max_y); ll total_area = 0; for (auto &rectangle : rectangles) { // DEB(rectangle << ", "); total_area += rectangle.area_of_rectangle_between(); } // DEB("\n"); // DEB("total_area: " << total_area << "\n"); vector <Segment<ll>> intersected; ll intersected_area = 0; for (auto &rectangle: rectangles) { for (auto &other: result) { auto inter = rectangle.rectangles_intersection(other); if (inter.is_forward()) { intersected.PB(inter); intersected_area += inter.area_of_rectangle_between(); } } } if (intersected_area > best_area) { best_area = intersected_area; chosen = intersected; } } } result = chosen; } ll result_area = 0; for (auto &rectangle: result) { result_area += rectangle.area_of_rectangle_between(); } return result_area; } void inline one() { int n; ll max_x, max_y; cin >> n >> max_x >> max_y; vector <Segment<ll>> input_segments; REP (i, n) { Point<ll> p1, p2; cin >> p1.x >> p1.y >> p2.x >> p2.y; Segment<ll> s(p1, p2); if (!s.is_forward()) { s = Segment<ll>(p2, p1); } // DEB(s << "\n"); // DEB("area: " << s.area_of_rectangle_between() << "\n"); input_segments.PB(s); } sort(input_segments.begin(), input_segments.end()); ll result_area = solve_one(input_segments, max_x, max_y); REP(i, 4) { random_shuffle(input_segments.begin(), input_segments.end()); ll area = solve_one(input_segments, max_x, max_y); DEB("random_area: " << area << "\n"); result_area = max(result_area, area); } cout << result_area << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); //int z; cin >> z; while(z--) one(); } |