#include <cstdio> #include <algorithm> using std::sort; using std::min; #define MAX_CARS 50000 int n, w, halfW; struct Car { int height; int preX; int postX; }; Car cars[MAX_CARS]; int indexes[MAX_CARS]; int tempIndexes[MAX_CARS]; int maxis[MAX_CARS]; bool beforeLessOperator(int i1, int i2) { return cars[i1].preX < cars[i2].preX; } //begin, middle, end bool merge(int b, int m, int e) { for (int i = 0; i < e; i++) tempIndexes[i] = indexes[i]; int max = -1; for (int i = m - 1; i >= b; i--) { int height = cars[indexes[i]].height; if (height > max) max = height; maxis[i] = max; } int i1 = b; int i2 = m; int iProgress = b; while (i1 < m && i2 < e) { Car & c1 = cars[tempIndexes[i1]]; Car & c2 = cars[tempIndexes[i2]]; if (c1.postX <= c2.postX) indexes[iProgress++] = tempIndexes[i1++]; else { if (c2.height + maxis[i1] > w) return false; indexes[iProgress++] = tempIndexes[i2++]; } } while (i1 < m) indexes[iProgress++] = tempIndexes[i1++]; return true; } //begin, end bool mergesort(int b, int e) { if (e - b <= 1) return true; bool success; int m = (b + e) / 2; success = mergesort(b, m); if (!success) return false; success = mergesort(m, e); if (!success) return false; return merge(b, m, e); } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &w); halfW = w / 2; int minx, miny, maxx, maxy; for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &minx, &miny, &maxx, &maxy); cars[i].height = abs(maxy - miny); cars[i].preX = min(minx, maxx); indexes[i] = i; } for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &minx, &miny, &maxx, &maxy); cars[i].postX = min(minx, maxx); } sort(indexes, indexes + n, beforeLessOperator); bool success = mergesort(0, n); if (success) printf("TAK\n"); else printf("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 | #include <cstdio> #include <algorithm> using std::sort; using std::min; #define MAX_CARS 50000 int n, w, halfW; struct Car { int height; int preX; int postX; }; Car cars[MAX_CARS]; int indexes[MAX_CARS]; int tempIndexes[MAX_CARS]; int maxis[MAX_CARS]; bool beforeLessOperator(int i1, int i2) { return cars[i1].preX < cars[i2].preX; } //begin, middle, end bool merge(int b, int m, int e) { for (int i = 0; i < e; i++) tempIndexes[i] = indexes[i]; int max = -1; for (int i = m - 1; i >= b; i--) { int height = cars[indexes[i]].height; if (height > max) max = height; maxis[i] = max; } int i1 = b; int i2 = m; int iProgress = b; while (i1 < m && i2 < e) { Car & c1 = cars[tempIndexes[i1]]; Car & c2 = cars[tempIndexes[i2]]; if (c1.postX <= c2.postX) indexes[iProgress++] = tempIndexes[i1++]; else { if (c2.height + maxis[i1] > w) return false; indexes[iProgress++] = tempIndexes[i2++]; } } while (i1 < m) indexes[iProgress++] = tempIndexes[i1++]; return true; } //begin, end bool mergesort(int b, int e) { if (e - b <= 1) return true; bool success; int m = (b + e) / 2; success = mergesort(b, m); if (!success) return false; success = mergesort(m, e); if (!success) return false; return merge(b, m, e); } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &w); halfW = w / 2; int minx, miny, maxx, maxy; for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &minx, &miny, &maxx, &maxy); cars[i].height = abs(maxy - miny); cars[i].preX = min(minx, maxx); indexes[i] = i; } for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &minx, &miny, &maxx, &maxy); cars[i].postX = min(minx, maxx); } sort(indexes, indexes + n, beforeLessOperator); bool success = mergesort(0, n); if (success) printf("TAK\n"); else printf("NIE\n"); } return 0; } |