#include <stdio.h> typedef struct _rec { int x1, x2, y1, y2; struct _rec *prev, *next; } t_rec; int cmp (const void *a, const void *b) { if(((t_rec*)a)->x1 != ((t_rec*)b)->x1) return ((t_rec*)a)->x1 - ((t_rec*)b)->x1; // if(((t_rec*)a)->x2 != ((t_rec*)b)->x2) // return ((t_rec*)a)->x2 - ((t_rec*)b)->x2; return ((t_rec*)a)->y1 - ((t_rec*)b)->y1; } int _read(int *n) { register char c = 0; register int res; while (c != EOF && c < '0') c=getc_unlocked(stdin); (*n) = 0; res = 0; while (c >= '0' && c <= '9' ) { (*n)=(*n)*10 + (c-'0'); c=getc_unlocked(stdin); res |= 1; } return(res); } int Min(int a, int b) { return a<b ? a : b; } int Max(int a, int b) { return a<b ? b : a; } void del(t_rec *a) { if(a->prev) a->prev->next = a->next; if(a->next) a->next->prev = a->prev; } int main() { t_rec rec[100000]; t_rec *rectop, *recact, *recbis; int n, i; int found; // scanf("%d", &n); _read(&n); for(i=0; i<n; i++) { // scanf("%d %d %d %d", &(rec[i].x1), &(rec[i].x2), &(rec[i].y1), &(rec[i].y2)); _read(&(rec[i].x1)); _read(&(rec[i].x2)); _read(&(rec[i].y1)); _read(&(rec[i].y2)); } qsort(rec, n, sizeof(t_rec), cmp); // make list for(recact = rec, i=0; i<n; i++, recact++) { recact->prev = recact-1; recact->next = recact+1; } (rec+0)->prev = NULL; (rec+n-1)->next = NULL; rectop = rec; found=1; while(found) { found=0; recact = rectop; while(recact) { recbis = recact->next; while(recbis && recact->x2 > recbis->x1) { if(recbis->y1 < recact->y2 && recact->y1 < recbis->y2) { recact->x2 = Max(recact->x2, recbis->x2); recact->y1 = Min(recact->y1, recbis->y1); recact->y2 = Max(recact->y2, recbis->y2); del(recbis); found = 1; n--; recact = recbis; break; } else { recbis = recbis->next; } } recact=recact->next; } } printf("%d\n", n); for(recact = rectop; recact; recact=recact->next) { printf("%d %d %d %d\n", recact->x1, recact->x2, recact->y1, recact->y2); } 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 | #include <stdio.h> typedef struct _rec { int x1, x2, y1, y2; struct _rec *prev, *next; } t_rec; int cmp (const void *a, const void *b) { if(((t_rec*)a)->x1 != ((t_rec*)b)->x1) return ((t_rec*)a)->x1 - ((t_rec*)b)->x1; // if(((t_rec*)a)->x2 != ((t_rec*)b)->x2) // return ((t_rec*)a)->x2 - ((t_rec*)b)->x2; return ((t_rec*)a)->y1 - ((t_rec*)b)->y1; } int _read(int *n) { register char c = 0; register int res; while (c != EOF && c < '0') c=getc_unlocked(stdin); (*n) = 0; res = 0; while (c >= '0' && c <= '9' ) { (*n)=(*n)*10 + (c-'0'); c=getc_unlocked(stdin); res |= 1; } return(res); } int Min(int a, int b) { return a<b ? a : b; } int Max(int a, int b) { return a<b ? b : a; } void del(t_rec *a) { if(a->prev) a->prev->next = a->next; if(a->next) a->next->prev = a->prev; } int main() { t_rec rec[100000]; t_rec *rectop, *recact, *recbis; int n, i; int found; // scanf("%d", &n); _read(&n); for(i=0; i<n; i++) { // scanf("%d %d %d %d", &(rec[i].x1), &(rec[i].x2), &(rec[i].y1), &(rec[i].y2)); _read(&(rec[i].x1)); _read(&(rec[i].x2)); _read(&(rec[i].y1)); _read(&(rec[i].y2)); } qsort(rec, n, sizeof(t_rec), cmp); // make list for(recact = rec, i=0; i<n; i++, recact++) { recact->prev = recact-1; recact->next = recact+1; } (rec+0)->prev = NULL; (rec+n-1)->next = NULL; rectop = rec; found=1; while(found) { found=0; recact = rectop; while(recact) { recbis = recact->next; while(recbis && recact->x2 > recbis->x1) { if(recbis->y1 < recact->y2 && recact->y1 < recbis->y2) { recact->x2 = Max(recact->x2, recbis->x2); recact->y1 = Min(recact->y1, recbis->y1); recact->y2 = Max(recact->y2, recbis->y2); del(recbis); found = 1; n--; recact = recbis; break; } else { recbis = recbis->next; } } recact=recact->next; } } printf("%d\n", n); for(recact = rectop; recact; recact=recact->next) { printf("%d %d %d %d\n", recact->x1, recact->x2, recact->y1, recact->y2); } return 0; } |