// Mateusz Kurek, PA 2014 // 5B FIO #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<vector> #include<string> #include<list> #include<deque> #include<map> #include<set> #include<queue> #include<stack> #include<utility> #include<sstream> #include<cstring> #include <string> #include <unistd.h> //#include <iostream> using namespace std; #define REP(i,v)for(int i=0;i<(v);++i) #define FOR(i,x,v)for(int i=x;i<=(v);++i) #define FORD(i,x,v)for(int i=x;i>=(v);--i) #define VAR(v,n) __typeof(n) v = (n) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), c.end() #define PB push_back #define SZ size #define MP make_pair #define FI first #define SE second #define CL clear() #define RS resize #define INFTY 1000000001 #define EPS 10e-9 #define SIZE(x) ((int)(x).size()) typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<bool> VB; typedef pair<int,int> PII; typedef long long LL; typedef vector<string> VS; void show(PII p) {printf("(%d %d)\n",p.FI,p.SE);} void show(VI e) {REP(i,SIZE(e)) printf("%d ",e[i]); printf("\n");} void show(vector<PII> e) {REP(i,SIZE(e)) printf("(%d %d) ",e[i].FI,e[i].SE); printf("\n");} void show(VS e) {REP(i,SIZE(e)) printf("%s\n",e[i].c_str());} void show(VVI e) {REP(i,SIZE(e)) show(e[i]);} #define MAXN 100001 #define MAXN2 300000 const int X2_ADD = 500000; int x1s[MAXN2], x2s[MAXN2], y1s[MAXN2], y2s[MAXN2]; struct picompcls{ bool operator()(const int &l, const int &r) const{ int va, vb, vc, vd; if(l >= X2_ADD){ va = x2s[l % X2_ADD]; vc = x1s[l % X2_ADD]; } else{ va = x1s[l % X2_ADD]; vc = x2s[l % X2_ADD]; } if(r >= X2_ADD){ vb = x2s[r-X2_ADD]; vd = x1s[r % X2_ADD]; } else{ vb = x1s[r % X2_ADD]; vd = x2s[r % X2_ADD]; } if(va == vb){ if(vc == vd){ if(y1s[l % X2_ADD] == y1s[r % X2_ADD]){ return y2s[l%X2_ADD] < y2s[r % X2_ADD]; } return y1s[l % X2_ADD] < y1s[r % X2_ADD]; } return vc < vd; } return va < vb; } }; bool resultcomp(const int &a, const int &b){ if(x1s[a] == x1s[b]){ if(x2s[a] == x2s[b]){ if(y1s[a] == y1s[b]){ return y2s[a] < y2s[b]; } return y1s[a] < y1s[b]; } return x2s[a] < x2s[b]; } return x1s[a] < x1s[b]; } string printpl(int x){ char buf[100]; sprintf(buf, "(%d, %d, %d, %d)", x1s[x], x2s[x], y1s[x], y2s[x]); string result = buf; return result; } int main(){ int n; int x1, x2, y1, y2, counter, index1, index2; // pl pl1, pl2, pl3; VI to_add, to_remove; bool found; set<int, picompcls> pls_set; set<int, picompcls>::iterator it1, it2; pair<set<int>::iterator, bool> res; bool still_in_use[MAXN2]; scanf("%d", &n); REP(i, n){ scanf("%d %d %d %d", &x1, &x2, &y1, &y2); // pl1 = {.x1=x1, .x2=x2, .y1=y1, .y2=y2}; x1s[i] = x1; x2s[i] = x2; y1s[i] = y1; y2s[i] = y2; res = pls_set.insert(i); // if(!res.second){ // printf("%d not inserted\n", i); // } res = pls_set.insert(i+X2_ADD); // if(!res.second){ // printf("%d not inserted\n", i + X2_ADD); // } still_in_use[i] = true; } counter = n; // printf("pls set: "); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d ", *it1); // } // printf("\n\n"); do{ // printf("clearing to_add\n"); to_add.clear(); // printf("clearing to_remove\n"); to_remove.clear(); // printf("testing\n"); for(it1=pls_set.begin(); it1 != pls_set.end();){ found = false; index1 = (*it1) % X2_ADD; if(*it1 < X2_ADD && still_in_use[index1]){ // pl1 = pls[index1]; // printf("looking for intersection with %d (%s)\n", index1, printpl(index1).c_str()); for(it2=pls_set.lower_bound(index1); it2 != pls_set.end(); ++it2){ index2 = (*it2) % X2_ADD; // pl2 = pls[index2]; // if((*it2 >= X2_ADD && x2s[index2] >= x2s[index1]) || (*it2 < X2_ADD && x1s[index2] >= x2s[index1])) { // break; // } if(index1 != index2 && still_in_use[index2]){ // printf("testing %d (%s)\n", index2, printpl(index2).c_str()); // if((y1s[index2] > y1s[index1] && y1s[index2] <= y2s[index1]) || ((y2s[index2] >= y1s[index1] && y2s[index2] < y2s[index1])) || (y1s[index2] <= y1s[index1] && y2s[index2] >= y2s[index1])){ // if((y1s[index2] > y1s[index1] && y2s[index2] < y1s[index1]) || (y1s[index2] >= y1s[index1] && y2s[index2] < y1s[index1]) || (y1s[index2] > y1s[index1] && y2s[index2] <= y1s[index1]) || (y1s[index2] <= y1s[index1] && y2s[index2] >= y2s[index1])){ if(x1s[index1] < x2s[index2] && x2s[index1] > x1s[index2] && y1s[index1] < y2s[index2] && y2s[index1] > y1s[index2]){ // printf("found intersection with %d (%s)\n", index2, printpl(index2).c_str()); // pl3 = {.x1=min(pl1.x1, pl2.x1), .x2=max(pl1.x2, pl2.x2), .y1=min(pl1.y1, pl2.y1), .y2=max(pl1.y2, pl2.y2)}; // pls.push_back(pl3); x1s[counter] = min(x1s[index1], x1s[index2]); x2s[counter] = max(x2s[index1], x2s[index2]); y1s[counter] = min(y1s[index1], y1s[index2]); y2s[counter] = max(y2s[index1], y2s[index2]); // printf("adding %d (%s)\n", counter, printpl(counter).c_str()); to_add.push_back(counter++); still_in_use[index2] = false; to_remove.push_back(index2); found = true; break; } } } } if(found){ still_in_use[index1] = false; // pls_set.erase(it1++); to_remove.push_back(index1); } else{ ++it1; } } // printf("removing from pls_set\n"); // remove for(int i=0;i<to_remove.size();i++){ // printf("removing %d\n", to_remove[i] % X2_ADD); // if(to_remove[i] == 303){ // printf("pls_set: "); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d ", *it1); // } // } // printf("\n"); it1 = pls_set.find(to_remove[i] % X2_ADD); // if(to_remove[i] == 303){ // printf("it != pls_set.end(): %d\n", it1 != pls_set.end()); // } pls_set.erase(it1); // printf("removing %d\n", to_remove[i] % X2_ADD + X2_ADD); it1 = pls_set.find((to_remove[i] % X2_ADD) + X2_ADD); pls_set.erase(it1); } // printf("after remove; adding new to pls_set\n"); // add for(int i=0;i<to_add.size();i++){ // printf("adding %d (%s) to pls_set\n", to_add[i], printpl(to_add[i]).c_str()); pls_set.insert(to_add[i]); pls_set.insert(to_add[i] + X2_ADD); still_in_use[to_add[i]] = true; } // printf("after adding new to pls_set\n"); // printf("after intersecting\npls_set: "); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d ", *it1); // } // printf("\nstill in use:\n"); // for(int i=0;i<counter;i++){ // if(still_in_use[i]){ // printf("%d (%s) ", i, printpl(i).c_str()); // } // } // printf("\n\n"); }while(to_add.size() > 0); // printf("after final intersecting\n"); VI result; for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ index1 = (*it1) % X2_ADD; if(still_in_use[index1]){ result.push_back(index1); still_in_use[index1] = false; } } // printf("sorting\n"); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d (%s)", *it1, printpl(*it1)); // } // printf("\n"); sort(result.begin(), result.end(), resultcomp); printf("%d\n", result.size()); for(int i=0;i<result.size();i++){ // pl1 = pls[result[i]]; printf("%d %d %d %d\n", x1s[result[i]], x2s[result[i]], y1s[result[i]], y2s[result[i]]); } // printf("end\n"); // to_add.clear(); // to_remove.clear(); // still_in_use.clear(); // pls_set.clear(); // pls.clear(); // result.clear(); 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 284 285 286 287 | // Mateusz Kurek, PA 2014 // 5B FIO #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<vector> #include<string> #include<list> #include<deque> #include<map> #include<set> #include<queue> #include<stack> #include<utility> #include<sstream> #include<cstring> #include <string> #include <unistd.h> //#include <iostream> using namespace std; #define REP(i,v)for(int i=0;i<(v);++i) #define FOR(i,x,v)for(int i=x;i<=(v);++i) #define FORD(i,x,v)for(int i=x;i>=(v);--i) #define VAR(v,n) __typeof(n) v = (n) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), c.end() #define PB push_back #define SZ size #define MP make_pair #define FI first #define SE second #define CL clear() #define RS resize #define INFTY 1000000001 #define EPS 10e-9 #define SIZE(x) ((int)(x).size()) typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<bool> VB; typedef pair<int,int> PII; typedef long long LL; typedef vector<string> VS; void show(PII p) {printf("(%d %d)\n",p.FI,p.SE);} void show(VI e) {REP(i,SIZE(e)) printf("%d ",e[i]); printf("\n");} void show(vector<PII> e) {REP(i,SIZE(e)) printf("(%d %d) ",e[i].FI,e[i].SE); printf("\n");} void show(VS e) {REP(i,SIZE(e)) printf("%s\n",e[i].c_str());} void show(VVI e) {REP(i,SIZE(e)) show(e[i]);} #define MAXN 100001 #define MAXN2 300000 const int X2_ADD = 500000; int x1s[MAXN2], x2s[MAXN2], y1s[MAXN2], y2s[MAXN2]; struct picompcls{ bool operator()(const int &l, const int &r) const{ int va, vb, vc, vd; if(l >= X2_ADD){ va = x2s[l % X2_ADD]; vc = x1s[l % X2_ADD]; } else{ va = x1s[l % X2_ADD]; vc = x2s[l % X2_ADD]; } if(r >= X2_ADD){ vb = x2s[r-X2_ADD]; vd = x1s[r % X2_ADD]; } else{ vb = x1s[r % X2_ADD]; vd = x2s[r % X2_ADD]; } if(va == vb){ if(vc == vd){ if(y1s[l % X2_ADD] == y1s[r % X2_ADD]){ return y2s[l%X2_ADD] < y2s[r % X2_ADD]; } return y1s[l % X2_ADD] < y1s[r % X2_ADD]; } return vc < vd; } return va < vb; } }; bool resultcomp(const int &a, const int &b){ if(x1s[a] == x1s[b]){ if(x2s[a] == x2s[b]){ if(y1s[a] == y1s[b]){ return y2s[a] < y2s[b]; } return y1s[a] < y1s[b]; } return x2s[a] < x2s[b]; } return x1s[a] < x1s[b]; } string printpl(int x){ char buf[100]; sprintf(buf, "(%d, %d, %d, %d)", x1s[x], x2s[x], y1s[x], y2s[x]); string result = buf; return result; } int main(){ int n; int x1, x2, y1, y2, counter, index1, index2; // pl pl1, pl2, pl3; VI to_add, to_remove; bool found; set<int, picompcls> pls_set; set<int, picompcls>::iterator it1, it2; pair<set<int>::iterator, bool> res; bool still_in_use[MAXN2]; scanf("%d", &n); REP(i, n){ scanf("%d %d %d %d", &x1, &x2, &y1, &y2); // pl1 = {.x1=x1, .x2=x2, .y1=y1, .y2=y2}; x1s[i] = x1; x2s[i] = x2; y1s[i] = y1; y2s[i] = y2; res = pls_set.insert(i); // if(!res.second){ // printf("%d not inserted\n", i); // } res = pls_set.insert(i+X2_ADD); // if(!res.second){ // printf("%d not inserted\n", i + X2_ADD); // } still_in_use[i] = true; } counter = n; // printf("pls set: "); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d ", *it1); // } // printf("\n\n"); do{ // printf("clearing to_add\n"); to_add.clear(); // printf("clearing to_remove\n"); to_remove.clear(); // printf("testing\n"); for(it1=pls_set.begin(); it1 != pls_set.end();){ found = false; index1 = (*it1) % X2_ADD; if(*it1 < X2_ADD && still_in_use[index1]){ // pl1 = pls[index1]; // printf("looking for intersection with %d (%s)\n", index1, printpl(index1).c_str()); for(it2=pls_set.lower_bound(index1); it2 != pls_set.end(); ++it2){ index2 = (*it2) % X2_ADD; // pl2 = pls[index2]; // if((*it2 >= X2_ADD && x2s[index2] >= x2s[index1]) || (*it2 < X2_ADD && x1s[index2] >= x2s[index1])) { // break; // } if(index1 != index2 && still_in_use[index2]){ // printf("testing %d (%s)\n", index2, printpl(index2).c_str()); // if((y1s[index2] > y1s[index1] && y1s[index2] <= y2s[index1]) || ((y2s[index2] >= y1s[index1] && y2s[index2] < y2s[index1])) || (y1s[index2] <= y1s[index1] && y2s[index2] >= y2s[index1])){ // if((y1s[index2] > y1s[index1] && y2s[index2] < y1s[index1]) || (y1s[index2] >= y1s[index1] && y2s[index2] < y1s[index1]) || (y1s[index2] > y1s[index1] && y2s[index2] <= y1s[index1]) || (y1s[index2] <= y1s[index1] && y2s[index2] >= y2s[index1])){ if(x1s[index1] < x2s[index2] && x2s[index1] > x1s[index2] && y1s[index1] < y2s[index2] && y2s[index1] > y1s[index2]){ // printf("found intersection with %d (%s)\n", index2, printpl(index2).c_str()); // pl3 = {.x1=min(pl1.x1, pl2.x1), .x2=max(pl1.x2, pl2.x2), .y1=min(pl1.y1, pl2.y1), .y2=max(pl1.y2, pl2.y2)}; // pls.push_back(pl3); x1s[counter] = min(x1s[index1], x1s[index2]); x2s[counter] = max(x2s[index1], x2s[index2]); y1s[counter] = min(y1s[index1], y1s[index2]); y2s[counter] = max(y2s[index1], y2s[index2]); // printf("adding %d (%s)\n", counter, printpl(counter).c_str()); to_add.push_back(counter++); still_in_use[index2] = false; to_remove.push_back(index2); found = true; break; } } } } if(found){ still_in_use[index1] = false; // pls_set.erase(it1++); to_remove.push_back(index1); } else{ ++it1; } } // printf("removing from pls_set\n"); // remove for(int i=0;i<to_remove.size();i++){ // printf("removing %d\n", to_remove[i] % X2_ADD); // if(to_remove[i] == 303){ // printf("pls_set: "); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d ", *it1); // } // } // printf("\n"); it1 = pls_set.find(to_remove[i] % X2_ADD); // if(to_remove[i] == 303){ // printf("it != pls_set.end(): %d\n", it1 != pls_set.end()); // } pls_set.erase(it1); // printf("removing %d\n", to_remove[i] % X2_ADD + X2_ADD); it1 = pls_set.find((to_remove[i] % X2_ADD) + X2_ADD); pls_set.erase(it1); } // printf("after remove; adding new to pls_set\n"); // add for(int i=0;i<to_add.size();i++){ // printf("adding %d (%s) to pls_set\n", to_add[i], printpl(to_add[i]).c_str()); pls_set.insert(to_add[i]); pls_set.insert(to_add[i] + X2_ADD); still_in_use[to_add[i]] = true; } // printf("after adding new to pls_set\n"); // printf("after intersecting\npls_set: "); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d ", *it1); // } // printf("\nstill in use:\n"); // for(int i=0;i<counter;i++){ // if(still_in_use[i]){ // printf("%d (%s) ", i, printpl(i).c_str()); // } // } // printf("\n\n"); }while(to_add.size() > 0); // printf("after final intersecting\n"); VI result; for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ index1 = (*it1) % X2_ADD; if(still_in_use[index1]){ result.push_back(index1); still_in_use[index1] = false; } } // printf("sorting\n"); // for(it1=pls_set.begin(); it1 != pls_set.end(); ++it1){ // printf("%d (%s)", *it1, printpl(*it1)); // } // printf("\n"); sort(result.begin(), result.end(), resultcomp); printf("%d\n", result.size()); for(int i=0;i<result.size();i++){ // pl1 = pls[result[i]]; printf("%d %d %d %d\n", x1s[result[i]], x2s[result[i]], y1s[result[i]], y2s[result[i]]); } // printf("end\n"); // to_add.clear(); // to_remove.clear(); // still_in_use.clear(); // pls_set.clear(); // pls.clear(); // result.clear(); return 0; } |