#include <stdio.h> #include <vector> #include <algorithm> #include <set> typedef struct car_s { int w,xe,xs; int order; } car_t; typedef struct startpositionsorter_s { std::vector<car_t> &cars; bool operator()(const int c1,const int c2) { return (cars[c1].xs < cars[c2].xs); } } startpositionsorter_t; typedef struct endpositionsorter_s { std::vector<car_t> &cars; bool operator()(const int c1,const int c2) { // printf("comparing end positions of c1 %d %d c2 %d %d\n",c1,cars[c1].xe,c2,cars[c2].xe); return (cars[c1].xe < cars[c2].xe); } } endpositionsorter_t; std::vector<car_t> cars; typedef struct ordercomp_s { bool operator()(const int c1,const int c2) { return cars[c1].order < cars[c2].order; } } ordercomp_t; int main(int argc,char *argv[]) { int n,t; int w; scanf("%d",&t); while(t--) { bool result = true; std::vector<int> startpositions; std::vector<int> endpositions; std::set<int, ordercomp_t > ordering; cars.clear(); scanf("%d %d",&n,&w); cars.push_back((car_t){1000000001,-1,-1,0}); for (int i=1;i<=n;i++) { int x1,y1,x2,y2; scanf("%d %d %d %d",&x1,&y1,&x2,&y2); if (x1>x2) { int tmp = x1; x1 = x2; x2 = tmp; } if (y1>y2) { int tmp = y1; y1 = y2; y2 = tmp; } // printf(" read start pos car [%d] [%d %d %d %d]\n",i+1,x1,y1,x2,y2); cars.push_back((car_t){y2-y1,-1,x2}); startpositions.push_back(i); } cars.push_back((car_t){0,1000000001,1000000001,n+1}); startpositionsorter_t possorter = {cars}; std::sort(startpositions.begin(),startpositions.end(),possorter); for (int j=1;j<=n;j++) { int x1,y1,x2,y2; scanf("%d %d %d %d",&x1,&y1,&x2,&y2); if (x1>x2) { int tmp = x1; x1 = x2; x2 = tmp; } if (y1>y2) { int tmp = y1; y1 = y2; y2 = tmp; } if (y2-y1 != cars[j].w ) { printf("%d %d %d %d\n",x1,y1,x2,y2); } // printf(" read end pos car [%d] [%d %d %d %d]\n",j,x1,y1,x2,y2); cars[j].xe = x1; endpositions.push_back(j); } endpositionsorter_t endpossorter = {cars}; std::sort(endpositions.begin(),endpositions.end(),endpossorter); for (int i=0;i<endpositions.size();++i) { cars[endpositions[i]].order=i+1; } ordering.insert(0); ordering.insert(n+1); // printf(" cars size:%d %d %d\n",cars.size(),startpositions.size(),endpositions.size()); /* for (int i=0;i<cars.size();i++) { printf(" cars [%d] is [w:%d xs:%d xe:%d o:%d]\n",i,cars[i].w,cars[i].xs,cars[i].xe,cars[i].order); } for (int i=0;i<n;i++) { printf(" startposition :%d -> %d \n",i,startpositions[i]); } for (int i=0;i<n;i++) { // printf(" endposition :%d -> %d (xe:%d)\n",i,endpositions[i],cars[endpositions[i]].xe); } */ for (int i=0;i<n;i++) { bool dontadd = false; // printf(" new car analysing %d\n",startpositions[i]); std::set<int>::iterator it = ordering.upper_bound(startpositions[i]); // printf(" checking with max barrier, (%d->%d) next w is (%d ->%d) total w is %d\n",startpositions[i],cars[startpositions[i]].w,*it,cars[(*(it))].w, w); if ( (cars[(*(it))].w + cars[startpositions[i]].w > w ) ) { // printf(" breaking , my w is (%d->%d) previous w is (%d ->%d) total w is %d\n",startpositions[i],cars[startpositions[i]].w,*it,cars[(*(it))].w, w); result = false; break; } if ( cars[*(it)].w >= cars[startpositions[i]].w) { dontadd = true; } --it; // printf(" checking previous values my (%d->%d) previous : %d[%d] \n",i,startpositions[i],*it,cars[*it].w); while( cars[*it].w <= cars[startpositions[i]].w) { // printf(" erasing barrier %d %d\n", *it,cars[*it].w); ordering.erase(it--); // printf(" checking previous values my (%d->%d) previous : %d[%d] \n",i,startpositions[i],*it,cars[*it].w); } if (!dontadd) { ordering.insert(startpositions[i]); } } printf(result?"TAK\n":"NIE\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 | #include <stdio.h> #include <vector> #include <algorithm> #include <set> typedef struct car_s { int w,xe,xs; int order; } car_t; typedef struct startpositionsorter_s { std::vector<car_t> &cars; bool operator()(const int c1,const int c2) { return (cars[c1].xs < cars[c2].xs); } } startpositionsorter_t; typedef struct endpositionsorter_s { std::vector<car_t> &cars; bool operator()(const int c1,const int c2) { // printf("comparing end positions of c1 %d %d c2 %d %d\n",c1,cars[c1].xe,c2,cars[c2].xe); return (cars[c1].xe < cars[c2].xe); } } endpositionsorter_t; std::vector<car_t> cars; typedef struct ordercomp_s { bool operator()(const int c1,const int c2) { return cars[c1].order < cars[c2].order; } } ordercomp_t; int main(int argc,char *argv[]) { int n,t; int w; scanf("%d",&t); while(t--) { bool result = true; std::vector<int> startpositions; std::vector<int> endpositions; std::set<int, ordercomp_t > ordering; cars.clear(); scanf("%d %d",&n,&w); cars.push_back((car_t){1000000001,-1,-1,0}); for (int i=1;i<=n;i++) { int x1,y1,x2,y2; scanf("%d %d %d %d",&x1,&y1,&x2,&y2); if (x1>x2) { int tmp = x1; x1 = x2; x2 = tmp; } if (y1>y2) { int tmp = y1; y1 = y2; y2 = tmp; } // printf(" read start pos car [%d] [%d %d %d %d]\n",i+1,x1,y1,x2,y2); cars.push_back((car_t){y2-y1,-1,x2}); startpositions.push_back(i); } cars.push_back((car_t){0,1000000001,1000000001,n+1}); startpositionsorter_t possorter = {cars}; std::sort(startpositions.begin(),startpositions.end(),possorter); for (int j=1;j<=n;j++) { int x1,y1,x2,y2; scanf("%d %d %d %d",&x1,&y1,&x2,&y2); if (x1>x2) { int tmp = x1; x1 = x2; x2 = tmp; } if (y1>y2) { int tmp = y1; y1 = y2; y2 = tmp; } if (y2-y1 != cars[j].w ) { printf("%d %d %d %d\n",x1,y1,x2,y2); } // printf(" read end pos car [%d] [%d %d %d %d]\n",j,x1,y1,x2,y2); cars[j].xe = x1; endpositions.push_back(j); } endpositionsorter_t endpossorter = {cars}; std::sort(endpositions.begin(),endpositions.end(),endpossorter); for (int i=0;i<endpositions.size();++i) { cars[endpositions[i]].order=i+1; } ordering.insert(0); ordering.insert(n+1); // printf(" cars size:%d %d %d\n",cars.size(),startpositions.size(),endpositions.size()); /* for (int i=0;i<cars.size();i++) { printf(" cars [%d] is [w:%d xs:%d xe:%d o:%d]\n",i,cars[i].w,cars[i].xs,cars[i].xe,cars[i].order); } for (int i=0;i<n;i++) { printf(" startposition :%d -> %d \n",i,startpositions[i]); } for (int i=0;i<n;i++) { // printf(" endposition :%d -> %d (xe:%d)\n",i,endpositions[i],cars[endpositions[i]].xe); } */ for (int i=0;i<n;i++) { bool dontadd = false; // printf(" new car analysing %d\n",startpositions[i]); std::set<int>::iterator it = ordering.upper_bound(startpositions[i]); // printf(" checking with max barrier, (%d->%d) next w is (%d ->%d) total w is %d\n",startpositions[i],cars[startpositions[i]].w,*it,cars[(*(it))].w, w); if ( (cars[(*(it))].w + cars[startpositions[i]].w > w ) ) { // printf(" breaking , my w is (%d->%d) previous w is (%d ->%d) total w is %d\n",startpositions[i],cars[startpositions[i]].w,*it,cars[(*(it))].w, w); result = false; break; } if ( cars[*(it)].w >= cars[startpositions[i]].w) { dontadd = true; } --it; // printf(" checking previous values my (%d->%d) previous : %d[%d] \n",i,startpositions[i],*it,cars[*it].w); while( cars[*it].w <= cars[startpositions[i]].w) { // printf(" erasing barrier %d %d\n", *it,cars[*it].w); ordering.erase(it--); // printf(" checking previous values my (%d->%d) previous : %d[%d] \n",i,startpositions[i],*it,cars[*it].w); } if (!dontadd) { ordering.insert(startpositions[i]); } } printf(result?"TAK\n":"NIE\n"); } return 0; } |