#include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <deque> #include <list> #include <set> #include <map> #include <cstdlib> #include <cassert> #include <cmath> #include <cstdarg> using namespace std; #define ALL(x) (x).begin(), (x).end() #ifdef DBG #define DBGprint(format, ...) do { fprintf(stderr, "[%s:%d] " format "\n", __func__, __LINE__, ## __VA_ARGS__); } while (0) #else #define DBGprint(...) do { } while (0) #endif typedef long double LD; typedef double DL; typedef long long LL; typedef unsigned long long ULL; typedef pair<LD,LD> PLD; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; void memcheck() { #ifdef DBG char c[100]; FILE *p = fopen("/proc/self/status","r"); while (fgets(c,100,p) ) if (strncmp("VmPeak",c,6)==0) fputs(c, stderr); fclose(p); #endif } //------ /headers -------- struct Tribe { int x1, x2, y1, y2; Tribe() = default; Tribe(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) { } tuple<int, int, int, int> tuplify() const { return tuple<int, int, int, int>(x1, x2, y1, y2); } }; int tribes_cnt; vector<Tribe> tribes; bool intersect(const Tribe& t1, const Tribe& t2) { return t1.x1 < t2.x2 && t1.x2 > t2.x1 && t1.y1 < t2.y2 && t1.y2 > t2.y1; } bool intersect(int t1_no, int t2_no) { return intersect(tribes[t1_no], tribes[t2_no]); } struct CompareTribes { bool operator () (int a, int b) { if (tribes[a].x2 == tribes[b].x2) { return a < b; } return tribes[a].x2 < tribes[b].x2; } }; struct Node { // trzymamy numery plemion, posortowane po x2 set<int, CompareTribes> past; // to sa te, ktore powstaly przez pojscie w gore drzewa set<int, CompareTribes> past_up; }; set<int> monitored{1434, 1371}; struct Tree { // do 10^6 static const int OFFSET = (1 << 20); static const int SIZE = 2 * OFFSET; vector<Node> nodes; // w ktorych wezlach siedzi dane plemie vector<VI> tribes_pos_past; vector<VI> tribes_pos_past_up; Tree() : nodes(SIZE), tribes_pos_past(SIZE), tribes_pos_past_up(SIZE) {} // zwraca numer plemiona, ktore przecina lub -1 int insert_node(int pos, int tribe_no) { // jest cos ciekawego? if (!nodes[pos].past.empty()) { auto it = nodes[pos].past.end(); // idziemy 2 (ostatni mozemy byc my) for (int i = 0; i < 2; ++i) { --it; int last = *it; if (last != tribe_no && intersect(last, tribe_no)) { // weee!!! return last; } } } nodes[pos].past.insert(tribe_no); tribes_pos_past[tribe_no].push_back(pos); #ifdef DBG DBGprint(" nodes[%d].past_up: ", pos); fprintf(stderr, " "); for (int i : nodes[pos].past) { fprintf(stderr, " %d", i); } fprintf(stderr, "\n"); #endif // idziemy do hory while (pos > 0) { // jest cos ciekawego? if (!nodes[pos].past_up.empty()) { auto it = nodes[pos].past_up.end(); // idziemy 2 (ostatni mozemy byc my) for (int i = 0; i < 2; ++i) { --it; int last = *it; if (last != tribe_no && intersect(last, tribe_no)) { // weee!!! return last; } } } if (!nodes[pos].past.empty()) { auto it = nodes[pos].past.end(); // idziemy 2 (ostatni mozemy byc my) for (int i = 0; i < 2; ++i) { --it; int last = *it; if (last != tribe_no && intersect(last, tribe_no)) { // weee!!! return last; } } } auto it = nodes[pos].past_up.find(tribe_no); if (it == nodes[pos].past_up.end()) { nodes[pos].past_up.insert(tribe_no); tribes_pos_past_up[tribe_no].push_back(pos); } DBGprint(" nodes[%d].past_up: ", pos); #ifdef DBG fprintf(stderr, " "); for (int i : nodes[pos].past_up) { fprintf(stderr, " %d", i); } fprintf(stderr, "\n"); #endif pos /= 2; } return -1; } // zwraca numer plemiona, ktore przecina lub -1 int insert_aux(int tribe_no, int from, int to, int pos, int left, int right) { // DBGprint(" insert tribe %d [%d-%d] {%d %d-%d}", tribe_no, from, to, pos, left, right); if (from == left && to == right) { DBGprint("insert tribe %d [%d-%d] {%d %d-%d}", tribe_no, from, to, pos, left, right); int k = insert_node(pos, tribe_no); // DBGprint("insert tribe %d [%d-%d] {%d %d-%d} ---> %d", tribe_no, from, to, pos, left, right, k); return k; } if (left >= right - 1) { return -1; } int m = (left + right) / 2; int ret_left = -1, ret_right = -1; if (from < m) { ret_left = insert_aux(tribe_no, from, min(m, to), pos * 2, left, m); } if (to > m) { ret_right = insert_aux(tribe_no, max(from, m), to, pos * 2 + 1, m, right); } // if (ret_left != -1) { // DBGprint("found left tribe %d {%d %d-%d}", ret_left, pos, left, right); // } // if (ret_right != -1) { // DBGprint("found right tribe %d {%d %d-%d}", ret_right, pos, left, right); // } return ret_left != -1 ? ret_left : ret_right; } void connect_tribes(int tribe, int incorpd) { tribes[tribe].x1 = min(tribes[tribe].x1, tribes[incorpd].x1); tribes[tribe].y1 = min(tribes[tribe].y1, tribes[incorpd].y1); tribes[tribe].x2 = max(tribes[tribe].x2, tribes[incorpd].x2); tribes[tribe].y2 = max(tribes[tribe].y2, tribes[incorpd].y2); tribes[incorpd].x1 = tribes[incorpd].x2 = tribes[incorpd].y1 = tribes[incorpd].y2 = -1; } void erase(int tribe_no) { for (int pos : tribes_pos_past[tribe_no]) { nodes[pos].past.erase(tribe_no); } for (int pos : tribes_pos_past_up[tribe_no]) { nodes[pos].past_up.erase(tribe_no); } tribes_pos_past[tribe_no].clear(); tribes_pos_past_up[tribe_no].clear(); } // usuwa lub wstawia, przy czym poki jest przeciecie, usuwa je void insert(int tribe_no) { // wstawiamy DBGprint("insert %d : [%d-%d] [%d-%d]", tribe_no, tribes[tribe_no].x1, tribes[tribe_no].x2, tribes[tribe_no].y1, tribes[tribe_no].y2); int k = insert_aux(tribe_no, tribes[tribe_no].y1, tribes[tribe_no].y2, 1, 0, OFFSET); if (k != -1) { DBGprint(" join %d [%d-%d] [%d-%d] <> %d [%d-%d] [%d-%d]", k, tribes[k].x1, tribes[k].x2, tribes[k].y1, tribes[k].y2, tribe_no, tribes[tribe_no].x1, tribes[tribe_no].x2, tribes[tribe_no].y1, tribes[tribe_no].y2); // usuwamy oba erase(tribe_no); erase(k); // laczymy connect_tribes(tribe_no, k); // wstawiamy nowe insert(tribe_no); } } }; Tree tree; void sweep() { // for (const Event& event : events) { // tree.insert(event.tribe); // } for (int i = 0; i < tribes.size(); ++i) { tree.insert(i); #ifdef DBG for (int j = 0; j <= i; ++j) { if (tribes[j].x1 != -1) { DBGprint("rects[%d] [%d-%d] [%d-%d]", j, tribes[j].x1, tribes[j].x2, tribes[j].y1, tribes[j].y2); } if (i < j && intersect(i, j)) { DBGprint("ERROR: %d ([%d-%d] [%d-%d]) %d ([%d-%d] [%d-%d])", i, tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2, j, tribes[j].x1, tribes[j].x2, tribes[j].y1, tribes[j].y2); } } #endif } } int main() { scanf("%d", &tribes_cnt); for (int i = 0; i < tribes_cnt; ++i) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); tribes.emplace_back(a, b, c, d); } sort(ALL(tribes), [] (const Tribe& t1, const Tribe& t2) { return t1.x2 < t2.x2; }); #ifdef DBG for (int i = 0; i < min(2000, (int) tribes.size()); ++i) { fprintf(stderr, "%d %d %d %d\n", tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2); } #endif sweep(); // co ja tu robie? sort(ALL(tribes), [] (const Tribe& t1, const Tribe& t2) { return t1.tuplify() < t2.tuplify(); }); vector<tuple<int, int, int, int>> results; for (int i = 0; i < tribes.size(); ++i) { if (tribes[i].x1 == -1) { continue; } if (i == 0 || tribes[i-1].tuplify() < tribes[i].tuplify()) { results.emplace_back( tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2); } } #ifdef DBG for (int i = 0; i < results.size(); ++i) { for (int j = 0; j < results.size(); ++j) { if (i == j) { continue; } Tribe t1(get<0>(results[i]), get<1>(results[i]), get<2>(results[i]), get<3>(results[i])); Tribe t2(get<0>(results[j]), get<1>(results[j]), get<2>(results[j]), get<3>(results[j])); if (intersect(t1, t2)) { DBGprint("ERROR: %d ([%d-%d] [%d-%d]) %d ([%d-%d] [%d-%d])", i, t1.x1, t1.x2, t1.y1, t1.y2, j, t2.x1, t2.x2, t2.y1, t2.y2); } } } #endif sort(ALL(results)); printf("%lu\n", results.size()); for (tuple<int, int, int, int>& t : results) { printf("%d %d %d %d\n", get<0>(t), get<1>(t), get<2>(t), get<3>(t)); } memcheck(); }
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <deque> #include <list> #include <set> #include <map> #include <cstdlib> #include <cassert> #include <cmath> #include <cstdarg> using namespace std; #define ALL(x) (x).begin(), (x).end() #ifdef DBG #define DBGprint(format, ...) do { fprintf(stderr, "[%s:%d] " format "\n", __func__, __LINE__, ## __VA_ARGS__); } while (0) #else #define DBGprint(...) do { } while (0) #endif typedef long double LD; typedef double DL; typedef long long LL; typedef unsigned long long ULL; typedef pair<LD,LD> PLD; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; void memcheck() { #ifdef DBG char c[100]; FILE *p = fopen("/proc/self/status","r"); while (fgets(c,100,p) ) if (strncmp("VmPeak",c,6)==0) fputs(c, stderr); fclose(p); #endif } //------ /headers -------- struct Tribe { int x1, x2, y1, y2; Tribe() = default; Tribe(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) { } tuple<int, int, int, int> tuplify() const { return tuple<int, int, int, int>(x1, x2, y1, y2); } }; int tribes_cnt; vector<Tribe> tribes; bool intersect(const Tribe& t1, const Tribe& t2) { return t1.x1 < t2.x2 && t1.x2 > t2.x1 && t1.y1 < t2.y2 && t1.y2 > t2.y1; } bool intersect(int t1_no, int t2_no) { return intersect(tribes[t1_no], tribes[t2_no]); } struct CompareTribes { bool operator () (int a, int b) { if (tribes[a].x2 == tribes[b].x2) { return a < b; } return tribes[a].x2 < tribes[b].x2; } }; struct Node { // trzymamy numery plemion, posortowane po x2 set<int, CompareTribes> past; // to sa te, ktore powstaly przez pojscie w gore drzewa set<int, CompareTribes> past_up; }; set<int> monitored{1434, 1371}; struct Tree { // do 10^6 static const int OFFSET = (1 << 20); static const int SIZE = 2 * OFFSET; vector<Node> nodes; // w ktorych wezlach siedzi dane plemie vector<VI> tribes_pos_past; vector<VI> tribes_pos_past_up; Tree() : nodes(SIZE), tribes_pos_past(SIZE), tribes_pos_past_up(SIZE) {} // zwraca numer plemiona, ktore przecina lub -1 int insert_node(int pos, int tribe_no) { // jest cos ciekawego? if (!nodes[pos].past.empty()) { auto it = nodes[pos].past.end(); // idziemy 2 (ostatni mozemy byc my) for (int i = 0; i < 2; ++i) { --it; int last = *it; if (last != tribe_no && intersect(last, tribe_no)) { // weee!!! return last; } } } nodes[pos].past.insert(tribe_no); tribes_pos_past[tribe_no].push_back(pos); #ifdef DBG DBGprint(" nodes[%d].past_up: ", pos); fprintf(stderr, " "); for (int i : nodes[pos].past) { fprintf(stderr, " %d", i); } fprintf(stderr, "\n"); #endif // idziemy do hory while (pos > 0) { // jest cos ciekawego? if (!nodes[pos].past_up.empty()) { auto it = nodes[pos].past_up.end(); // idziemy 2 (ostatni mozemy byc my) for (int i = 0; i < 2; ++i) { --it; int last = *it; if (last != tribe_no && intersect(last, tribe_no)) { // weee!!! return last; } } } if (!nodes[pos].past.empty()) { auto it = nodes[pos].past.end(); // idziemy 2 (ostatni mozemy byc my) for (int i = 0; i < 2; ++i) { --it; int last = *it; if (last != tribe_no && intersect(last, tribe_no)) { // weee!!! return last; } } } auto it = nodes[pos].past_up.find(tribe_no); if (it == nodes[pos].past_up.end()) { nodes[pos].past_up.insert(tribe_no); tribes_pos_past_up[tribe_no].push_back(pos); } DBGprint(" nodes[%d].past_up: ", pos); #ifdef DBG fprintf(stderr, " "); for (int i : nodes[pos].past_up) { fprintf(stderr, " %d", i); } fprintf(stderr, "\n"); #endif pos /= 2; } return -1; } // zwraca numer plemiona, ktore przecina lub -1 int insert_aux(int tribe_no, int from, int to, int pos, int left, int right) { // DBGprint(" insert tribe %d [%d-%d] {%d %d-%d}", tribe_no, from, to, pos, left, right); if (from == left && to == right) { DBGprint("insert tribe %d [%d-%d] {%d %d-%d}", tribe_no, from, to, pos, left, right); int k = insert_node(pos, tribe_no); // DBGprint("insert tribe %d [%d-%d] {%d %d-%d} ---> %d", tribe_no, from, to, pos, left, right, k); return k; } if (left >= right - 1) { return -1; } int m = (left + right) / 2; int ret_left = -1, ret_right = -1; if (from < m) { ret_left = insert_aux(tribe_no, from, min(m, to), pos * 2, left, m); } if (to > m) { ret_right = insert_aux(tribe_no, max(from, m), to, pos * 2 + 1, m, right); } // if (ret_left != -1) { // DBGprint("found left tribe %d {%d %d-%d}", ret_left, pos, left, right); // } // if (ret_right != -1) { // DBGprint("found right tribe %d {%d %d-%d}", ret_right, pos, left, right); // } return ret_left != -1 ? ret_left : ret_right; } void connect_tribes(int tribe, int incorpd) { tribes[tribe].x1 = min(tribes[tribe].x1, tribes[incorpd].x1); tribes[tribe].y1 = min(tribes[tribe].y1, tribes[incorpd].y1); tribes[tribe].x2 = max(tribes[tribe].x2, tribes[incorpd].x2); tribes[tribe].y2 = max(tribes[tribe].y2, tribes[incorpd].y2); tribes[incorpd].x1 = tribes[incorpd].x2 = tribes[incorpd].y1 = tribes[incorpd].y2 = -1; } void erase(int tribe_no) { for (int pos : tribes_pos_past[tribe_no]) { nodes[pos].past.erase(tribe_no); } for (int pos : tribes_pos_past_up[tribe_no]) { nodes[pos].past_up.erase(tribe_no); } tribes_pos_past[tribe_no].clear(); tribes_pos_past_up[tribe_no].clear(); } // usuwa lub wstawia, przy czym poki jest przeciecie, usuwa je void insert(int tribe_no) { // wstawiamy DBGprint("insert %d : [%d-%d] [%d-%d]", tribe_no, tribes[tribe_no].x1, tribes[tribe_no].x2, tribes[tribe_no].y1, tribes[tribe_no].y2); int k = insert_aux(tribe_no, tribes[tribe_no].y1, tribes[tribe_no].y2, 1, 0, OFFSET); if (k != -1) { DBGprint(" join %d [%d-%d] [%d-%d] <> %d [%d-%d] [%d-%d]", k, tribes[k].x1, tribes[k].x2, tribes[k].y1, tribes[k].y2, tribe_no, tribes[tribe_no].x1, tribes[tribe_no].x2, tribes[tribe_no].y1, tribes[tribe_no].y2); // usuwamy oba erase(tribe_no); erase(k); // laczymy connect_tribes(tribe_no, k); // wstawiamy nowe insert(tribe_no); } } }; Tree tree; void sweep() { // for (const Event& event : events) { // tree.insert(event.tribe); // } for (int i = 0; i < tribes.size(); ++i) { tree.insert(i); #ifdef DBG for (int j = 0; j <= i; ++j) { if (tribes[j].x1 != -1) { DBGprint("rects[%d] [%d-%d] [%d-%d]", j, tribes[j].x1, tribes[j].x2, tribes[j].y1, tribes[j].y2); } if (i < j && intersect(i, j)) { DBGprint("ERROR: %d ([%d-%d] [%d-%d]) %d ([%d-%d] [%d-%d])", i, tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2, j, tribes[j].x1, tribes[j].x2, tribes[j].y1, tribes[j].y2); } } #endif } } int main() { scanf("%d", &tribes_cnt); for (int i = 0; i < tribes_cnt; ++i) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); tribes.emplace_back(a, b, c, d); } sort(ALL(tribes), [] (const Tribe& t1, const Tribe& t2) { return t1.x2 < t2.x2; }); #ifdef DBG for (int i = 0; i < min(2000, (int) tribes.size()); ++i) { fprintf(stderr, "%d %d %d %d\n", tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2); } #endif sweep(); // co ja tu robie? sort(ALL(tribes), [] (const Tribe& t1, const Tribe& t2) { return t1.tuplify() < t2.tuplify(); }); vector<tuple<int, int, int, int>> results; for (int i = 0; i < tribes.size(); ++i) { if (tribes[i].x1 == -1) { continue; } if (i == 0 || tribes[i-1].tuplify() < tribes[i].tuplify()) { results.emplace_back( tribes[i].x1, tribes[i].x2, tribes[i].y1, tribes[i].y2); } } #ifdef DBG for (int i = 0; i < results.size(); ++i) { for (int j = 0; j < results.size(); ++j) { if (i == j) { continue; } Tribe t1(get<0>(results[i]), get<1>(results[i]), get<2>(results[i]), get<3>(results[i])); Tribe t2(get<0>(results[j]), get<1>(results[j]), get<2>(results[j]), get<3>(results[j])); if (intersect(t1, t2)) { DBGprint("ERROR: %d ([%d-%d] [%d-%d]) %d ([%d-%d] [%d-%d])", i, t1.x1, t1.x2, t1.y1, t1.y2, j, t2.x1, t2.x2, t2.y1, t2.y2); } } } #endif sort(ALL(results)); printf("%lu\n", results.size()); for (tuple<int, int, int, int>& t : results) { printf("%d %d %d %d\n", get<0>(t), get<1>(t), get<2>(t), get<3>(t)); } memcheck(); } |