#include <stdio.h> #include <stdlib.h> #include <algorithm> using namespace std; const int INF = 2 * 1000 * 1000 * 1000; struct car_t { int startLeft, destLeft, height, posLeft; }; struct tree_t { int start, end; tree_t *left, *right; int value; }; tree_t *createTree(int size); void deleteTree(tree_t *tree); void changeValue(tree_t *tree, int pos, int value); int getValue(tree_t *tree, int start, int end); bool compareStartLeft(const car_t &lhs, const car_t &rhs); bool compareDestLeft(const car_t &lhs, const car_t &rhs); int main() { int t; scanf("%d", &t); for(int i = 0; i < t; ++i) { int n, w; scanf("%d %d", &n, &w); car_t *cars = new car_t[n]; for(int j = 0; j < n; ++j) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); cars[j].startLeft = min(x1, x2); cars[j].height = abs(y2 - y1); } for(int j = 0; j < n; ++j) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); cars[j].destLeft = min(x1, x2); } sort(cars, cars + n, compareStartLeft); tree_t *tree = createTree(n); for(int j = 0; j < n; ++j) { cars[j].posLeft = j; changeValue(tree, j, w - cars[j].height); } sort(cars, cars + n, compareDestLeft); bool possible = true; for(int j = 0; j < n && possible; ++j) { possible = cars[j].posLeft == 0 || cars[j].height <= getValue(tree, 0, cars[j].posLeft - 1); changeValue(tree, cars[j].posLeft, INF); } deleteTree(tree); delete[] cars; printf(possible? "TAK\n" : "NIE\n"); } return 0; } tree_t *createNode(int start, int end) { tree_t *node = new tree_t; node->start = start; node->end = end; node->value = INF; if(start == end) { node->left = NULL; node->right = NULL; } else { node->left = createNode(start, (start + end) / 2); node->right = createNode((start + end) / 2 + 1, end); } return node; } tree_t *createTree(int size) { int realSize = 1; while(realSize < size) realSize <<= 1; return createNode(0, realSize - 1); } void deleteTree(tree_t *tree) { if(tree->left != NULL) deleteTree(tree->left); if(tree->right != NULL) deleteTree(tree->right); delete tree; } void changeValue(tree_t *tree, int pos, int value) { if(tree->start == tree->end) tree->value = value; else { tree_t *next = pos <= tree->left->end? tree->left : tree->right; changeValue(next, pos, value); tree->value = min(tree->left->value, tree->right->value); } } int getValue(tree_t *tree, int start, int end) { if(tree->start == tree->end) return tree->value; int value = INF; if(start <= tree->left->end) value = min(value, getValue(tree->left, start, min(tree->left->end, end))); if(end >= tree->right->start) value = min(value, getValue(tree->right, max(tree->right->start, start), end)); return value; } bool compareStartLeft(const car_t &lhs, const car_t &rhs) { return lhs.startLeft < rhs.startLeft; } bool compareDestLeft(const car_t &lhs, const car_t &rhs) { return lhs.destLeft < rhs.destLeft; }
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 | #include <stdio.h> #include <stdlib.h> #include <algorithm> using namespace std; const int INF = 2 * 1000 * 1000 * 1000; struct car_t { int startLeft, destLeft, height, posLeft; }; struct tree_t { int start, end; tree_t *left, *right; int value; }; tree_t *createTree(int size); void deleteTree(tree_t *tree); void changeValue(tree_t *tree, int pos, int value); int getValue(tree_t *tree, int start, int end); bool compareStartLeft(const car_t &lhs, const car_t &rhs); bool compareDestLeft(const car_t &lhs, const car_t &rhs); int main() { int t; scanf("%d", &t); for(int i = 0; i < t; ++i) { int n, w; scanf("%d %d", &n, &w); car_t *cars = new car_t[n]; for(int j = 0; j < n; ++j) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); cars[j].startLeft = min(x1, x2); cars[j].height = abs(y2 - y1); } for(int j = 0; j < n; ++j) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); cars[j].destLeft = min(x1, x2); } sort(cars, cars + n, compareStartLeft); tree_t *tree = createTree(n); for(int j = 0; j < n; ++j) { cars[j].posLeft = j; changeValue(tree, j, w - cars[j].height); } sort(cars, cars + n, compareDestLeft); bool possible = true; for(int j = 0; j < n && possible; ++j) { possible = cars[j].posLeft == 0 || cars[j].height <= getValue(tree, 0, cars[j].posLeft - 1); changeValue(tree, cars[j].posLeft, INF); } deleteTree(tree); delete[] cars; printf(possible? "TAK\n" : "NIE\n"); } return 0; } tree_t *createNode(int start, int end) { tree_t *node = new tree_t; node->start = start; node->end = end; node->value = INF; if(start == end) { node->left = NULL; node->right = NULL; } else { node->left = createNode(start, (start + end) / 2); node->right = createNode((start + end) / 2 + 1, end); } return node; } tree_t *createTree(int size) { int realSize = 1; while(realSize < size) realSize <<= 1; return createNode(0, realSize - 1); } void deleteTree(tree_t *tree) { if(tree->left != NULL) deleteTree(tree->left); if(tree->right != NULL) deleteTree(tree->right); delete tree; } void changeValue(tree_t *tree, int pos, int value) { if(tree->start == tree->end) tree->value = value; else { tree_t *next = pos <= tree->left->end? tree->left : tree->right; changeValue(next, pos, value); tree->value = min(tree->left->value, tree->right->value); } } int getValue(tree_t *tree, int start, int end) { if(tree->start == tree->end) return tree->value; int value = INF; if(start <= tree->left->end) value = min(value, getValue(tree->left, start, min(tree->left->end, end))); if(end >= tree->right->start) value = min(value, getValue(tree->right, max(tree->right->start, start), end)); return value; } bool compareStartLeft(const car_t &lhs, const car_t &rhs) { return lhs.startLeft < rhs.startLeft; } bool compareDestLeft(const car_t &lhs, const car_t &rhs) { return lhs.destLeft < rhs.destLeft; } |