#include <algorithm> #include <iostream> #include <list> #include <map> #include <set> #include <vector> using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; namespace { int compress(vector<int> const& v, int x) { auto it = lower_bound(v.begin(), v.end(), x); return it - v.begin(); } struct Rect { int x1; int x2; int y1; int y2; Rect(): x1{0}, x2{0}, y1{0}, y2{0} { } Rect(int x1_, int x2_, int y1_, int y2_): x1{x1_}, x2{x2_}, y1{y1_}, y2{y2_} { } explicit operator bool() const { return x1 < x2 && y1 < y2; } }; Rect operator|(Rect const& a, Rect const& b) { return Rect{min(a.x1, b.x1), max(a.x2, b.x2), min(a.y1, b.y1), max(a.y2, b.y2)}; } Rect operator&(Rect const& a, Rect const& b) { return Rect{max(a.x1, b.x1), min(a.x2, b.x2), max(a.y1, b.y1), min(a.y2, b.y2)}; } Rect& operator|=(Rect& a, Rect const& b) { return a = a | b; } Rect& operator&=(Rect& a, Rect const& b) { return a = a & b; } template <typename OS> OS& operator<<(OS& s, Rect const& r) { return s << r.x1 << ' ' << r.x2 << ' ' << r.y1 << ' ' << r.y2; } bool operator<(Rect const& a, Rect const& b) { if (a.x1 != b.x1) return a.x1 < b.x1; if (a.x2 != b.x2) return a.x2 < b.x2; if (a.y1 != b.y1) return a.y1 < b.y1; if (a.y2 != b.y2) return a.y2 < b.y2; return false; } bool operator==(Rect const& a, Rect const& b) { return a.x1 == b.x1 && a.x2 == b.x2 && a.y1 == b.y1 && a.y2 == b.y2; } int round_up(int x) { int p = 1; while (p < x) p *= 2; return p; } struct Rect1DLess { bool operator()(Rect const& a, Rect const& b) { if (a.y1 != b.y1) return a.y1 < b.y1; return a < b; } }; template <typename CI, typename I, typename C, typename A> class Rect1DUpdate { protected: void operator()(I it, CI end) { int m = (*it)->y2; if (it.get_l_child() != end) m = max(m, it.get_l_child().get_metadata()); if (it.get_r_child() != end) m = max(m, it.get_r_child().get_metadata()); const_cast<int&>(it.get_metadata()) = m; } virtual CI node_begin() const = 0; virtual CI node_end() const = 0; virtual ~Rect1DUpdate() { } public: typedef int metadata_type; bool query(Rect const& r, Rect& res) { CI it = node_begin(); CI end = node_end(); while (it != end) { if (r.y2 >= (*it)->y1 && r.y1 <= (*it)->y2) { res = **it; return true; } if (it.get_l_child() != end && it.get_l_child().get_metadata() >= r.y1) { it = it.get_l_child(); } else { it = it.get_r_child(); } } return false; } }; class Rects { private: typedef tree<Rect, null_mapped_type, Rect1DLess, rb_tree_tag, Rect1DUpdate> lower; int m; vector<lower> all; vector<lower> any; public: Rects(int m_): m(round_up(m_)), all(2*m), any(2*m) { } void add(Rect const& r) { int a = m + r.x1; int b = m + r.x2; all[a].insert(r); all[b].insert(r); while (a/2 != b/2) { if (a % 2 == 0) all[a+1].insert(r); if (b % 2 == 1) all[b-1].insert(r); a /= 2; b /= 2; any[a].insert(r); any[b].insert(r); } while (a/2 > 0) { a /= 2; any[a].insert(r); } } void remove(Rect const& r) { int a = m + r.x1; int b = m + r.x2; all[a].erase(all[a].find(r)); all[b].erase(all[b].find(r)); while (a/2 != b/2) { if (a % 2 == 0) all[a+1].erase(all[a+1].find(r)); if (b % 2 == 1) all[b-1].erase(all[b-1].find(r)); a /= 2; b /= 2; any[a].erase(any[a].find(r)); any[b].erase(any[b].find(r)); } while (a/2 > 0) { a /= 2; any[a].erase(any[a].find(r)); } } bool query(Rect const& r, Rect& res) { int a = m + r.x1; int b = m + r.x2; if (all[a].query(r, res)) return true; if (all[b].query(r, res)) return true; while (a/2 != b/2) { if (a % 2 == 0) { if (any[a+1].query(r, res)) return true; if (all[a+1].query(r, res)) return true; } if (b % 2 == 1) { if (any[b-1].query(r, res)) return true; if (all[b-1].query(r, res)) return true; } a /= 2; b /= 2; if (all[a].query(r, res)) return true; if (all[b].query(r, res)) return true; } while (a/2 > 0) { a /= 2; if (all[a].query(r, res)) return true; } return false; } }; set<Rect> reduce(vector<Rect> const& rects, int mx) { set<Rect> res; Rects cur{mx}; for (auto r: rects) { Rect s; while (cur.query(r, s)) { r |= s; cur.remove(s); res.erase(s); } res.insert(r); cur.add(r); } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<Rect> rects; vector<int> xs; vector<int> ys; for (int i = 0; i < n; ++i) { int x1, x2, y1, y2; cin >> x1 >> x2 >> y1 >> y2; x1 *= 2; ++x1; y1 *= 2; ++y1; x2 *= 2; y2 *= 2; rects.push_back(Rect{x1, x2, y1, y2}); xs.push_back(x1); xs.push_back(x2); ys.push_back(y1); ys.push_back(y2); } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); for (auto& rect: rects) { rect.x1 = compress(xs, rect.x1); rect.x2 = compress(xs, rect.x2); rect.y1 = compress(ys, rect.y1); rect.y2 = compress(ys, rect.y2); } auto res = reduce(rects, xs.size()); cout << res.size() << '\n'; for (auto const& rect: res) { cout << xs[rect.x1]/2 << ' ' << xs[rect.x2]/2 << ' '; cout << ys[rect.y1]/2 << ' ' << ys[rect.y2]/2 << '\n'; } return 0; }
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | #include <algorithm> #include <iostream> #include <list> #include <map> #include <set> #include <vector> using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; namespace { int compress(vector<int> const& v, int x) { auto it = lower_bound(v.begin(), v.end(), x); return it - v.begin(); } struct Rect { int x1; int x2; int y1; int y2; Rect(): x1{0}, x2{0}, y1{0}, y2{0} { } Rect(int x1_, int x2_, int y1_, int y2_): x1{x1_}, x2{x2_}, y1{y1_}, y2{y2_} { } explicit operator bool() const { return x1 < x2 && y1 < y2; } }; Rect operator|(Rect const& a, Rect const& b) { return Rect{min(a.x1, b.x1), max(a.x2, b.x2), min(a.y1, b.y1), max(a.y2, b.y2)}; } Rect operator&(Rect const& a, Rect const& b) { return Rect{max(a.x1, b.x1), min(a.x2, b.x2), max(a.y1, b.y1), min(a.y2, b.y2)}; } Rect& operator|=(Rect& a, Rect const& b) { return a = a | b; } Rect& operator&=(Rect& a, Rect const& b) { return a = a & b; } template <typename OS> OS& operator<<(OS& s, Rect const& r) { return s << r.x1 << ' ' << r.x2 << ' ' << r.y1 << ' ' << r.y2; } bool operator<(Rect const& a, Rect const& b) { if (a.x1 != b.x1) return a.x1 < b.x1; if (a.x2 != b.x2) return a.x2 < b.x2; if (a.y1 != b.y1) return a.y1 < b.y1; if (a.y2 != b.y2) return a.y2 < b.y2; return false; } bool operator==(Rect const& a, Rect const& b) { return a.x1 == b.x1 && a.x2 == b.x2 && a.y1 == b.y1 && a.y2 == b.y2; } int round_up(int x) { int p = 1; while (p < x) p *= 2; return p; } struct Rect1DLess { bool operator()(Rect const& a, Rect const& b) { if (a.y1 != b.y1) return a.y1 < b.y1; return a < b; } }; template <typename CI, typename I, typename C, typename A> class Rect1DUpdate { protected: void operator()(I it, CI end) { int m = (*it)->y2; if (it.get_l_child() != end) m = max(m, it.get_l_child().get_metadata()); if (it.get_r_child() != end) m = max(m, it.get_r_child().get_metadata()); const_cast<int&>(it.get_metadata()) = m; } virtual CI node_begin() const = 0; virtual CI node_end() const = 0; virtual ~Rect1DUpdate() { } public: typedef int metadata_type; bool query(Rect const& r, Rect& res) { CI it = node_begin(); CI end = node_end(); while (it != end) { if (r.y2 >= (*it)->y1 && r.y1 <= (*it)->y2) { res = **it; return true; } if (it.get_l_child() != end && it.get_l_child().get_metadata() >= r.y1) { it = it.get_l_child(); } else { it = it.get_r_child(); } } return false; } }; class Rects { private: typedef tree<Rect, null_mapped_type, Rect1DLess, rb_tree_tag, Rect1DUpdate> lower; int m; vector<lower> all; vector<lower> any; public: Rects(int m_): m(round_up(m_)), all(2*m), any(2*m) { } void add(Rect const& r) { int a = m + r.x1; int b = m + r.x2; all[a].insert(r); all[b].insert(r); while (a/2 != b/2) { if (a % 2 == 0) all[a+1].insert(r); if (b % 2 == 1) all[b-1].insert(r); a /= 2; b /= 2; any[a].insert(r); any[b].insert(r); } while (a/2 > 0) { a /= 2; any[a].insert(r); } } void remove(Rect const& r) { int a = m + r.x1; int b = m + r.x2; all[a].erase(all[a].find(r)); all[b].erase(all[b].find(r)); while (a/2 != b/2) { if (a % 2 == 0) all[a+1].erase(all[a+1].find(r)); if (b % 2 == 1) all[b-1].erase(all[b-1].find(r)); a /= 2; b /= 2; any[a].erase(any[a].find(r)); any[b].erase(any[b].find(r)); } while (a/2 > 0) { a /= 2; any[a].erase(any[a].find(r)); } } bool query(Rect const& r, Rect& res) { int a = m + r.x1; int b = m + r.x2; if (all[a].query(r, res)) return true; if (all[b].query(r, res)) return true; while (a/2 != b/2) { if (a % 2 == 0) { if (any[a+1].query(r, res)) return true; if (all[a+1].query(r, res)) return true; } if (b % 2 == 1) { if (any[b-1].query(r, res)) return true; if (all[b-1].query(r, res)) return true; } a /= 2; b /= 2; if (all[a].query(r, res)) return true; if (all[b].query(r, res)) return true; } while (a/2 > 0) { a /= 2; if (all[a].query(r, res)) return true; } return false; } }; set<Rect> reduce(vector<Rect> const& rects, int mx) { set<Rect> res; Rects cur{mx}; for (auto r: rects) { Rect s; while (cur.query(r, s)) { r |= s; cur.remove(s); res.erase(s); } res.insert(r); cur.add(r); } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<Rect> rects; vector<int> xs; vector<int> ys; for (int i = 0; i < n; ++i) { int x1, x2, y1, y2; cin >> x1 >> x2 >> y1 >> y2; x1 *= 2; ++x1; y1 *= 2; ++y1; x2 *= 2; y2 *= 2; rects.push_back(Rect{x1, x2, y1, y2}); xs.push_back(x1); xs.push_back(x2); ys.push_back(y1); ys.push_back(y2); } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); for (auto& rect: rects) { rect.x1 = compress(xs, rect.x1); rect.x2 = compress(xs, rect.x2); rect.y1 = compress(ys, rect.y1); rect.y2 = compress(ys, rect.y2); } auto res = reduce(rects, xs.size()); cout << res.size() << '\n'; for (auto const& rect: res) { cout << xs[rect.x1]/2 << ' ' << xs[rect.x2]/2 << ' '; cout << ys[rect.y1]/2 << ' ' << ys[rect.y2]/2 << '\n'; } return 0; } |