#include <stdio.h> #include <vector> #include <algorithm> #include <stdlib.h> #include <map> using namespace std; struct Car { int x; int height; int index; void print() { printf("%d: %d %d", index, x, height); } }; bool operator==(const Car &left, const Car &right) { return left.index==right.index; } bool operator>(const Car &left, const Car &right) { return left.index>right.index; } bool operator<(const Car &left, const Car &right) { return left.index<right.index; } bool sortCarByX(Car first, Car second) { return first.x < second.x; } struct Node { Node *left; Node *right; Node *parent; int leftMax; int rightMax; int value; int index; Node(int val, int idx) { left = NULL; right = NULL; parent = NULL; leftMax = 0; rightMax = 0; value = val; index = idx; } void add(Node *node) { if(index > node->index) { if(left == NULL) { node->parent = this; left = node; } else { left->add(node); } leftMax = max(leftMax, node->value); } else { if(right == NULL) { node->parent = this; right = node; } else { right->add(node); } rightMax = max(rightMax, node->value); } } void clean() { if(left != NULL) { left->clean(); delete left; } if(right != NULL) { right->clean(); delete right; } } void print() { printf("I: %d, L: %d\n", index, leftMax); if(left != NULL) { left->print(); } if(right != NULL) { right->print(); } } }; struct MagicTree { Node *root; map<Car, Node*> carsToNodes; MagicTree() { root = NULL; } void add(Car &car) { Node *nptr = new Node(car.height, car.index); if(root==NULL) { root = nptr; } else { root->add(nptr); } carsToNodes[car] = nptr; } int fetch(Car &car) { Node *node = carsToNodes[car]; int leftMax = node->leftMax; while(node->parent != NULL) { if(node->parent->right == node) { leftMax = max(leftMax, node->parent->leftMax); leftMax = max(leftMax, node->parent->value); } node = node->parent; } return leftMax; } void remove(Car &car) { Node *node = carsToNodes[car]; node->value = 0; while(node->parent != NULL) { node = node->parent; if(node->left != NULL) { node->leftMax = max(node->left->leftMax, node->left->rightMax); node->leftMax = max(node->leftMax, node->left->value); } if(node->right != NULL) { node->rightMax = max(node->right->leftMax, node->right->rightMax); node->rightMax = max(node->rightMax, node->right->value); } } } void clean() { if(root != NULL) { root->clean(); delete root; } } void print() { root->print(); } }; Car readCar(int idx) { int x1, y1, x2, y2; Car c; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); c.x = min(x1, x2); c.height = abs(y2-y1); c.index = idx; return c; } void balancedAdd(MagicTree &mt, vector<Car> &cars, int firstIdx, int lastIdx) { int middle = (firstIdx+lastIdx)/2; mt.add(cars[middle]); if(lastIdx > firstIdx) { balancedAdd(mt, cars, middle+1, lastIdx); } if(lastIdx > firstIdx+1) { balancedAdd(mt, cars, firstIdx, middle-1); } } MagicTree createMagicTree(vector<Car> &cars) { MagicTree mt = MagicTree(); balancedAdd(mt, cars, 0, cars.size()-1); return mt; } void test () { int n, w; vector<Car> cars; vector<Car> destinationCars; scanf("%d %d", &n, &w); for(int i = 0 ; i < n ; ++i) { Car c = readCar(i); cars.push_back(c); } for(int i = 0 ; i < n ; ++i) { Car c = readCar(i); destinationCars.push_back(c); } sort(destinationCars.begin(), destinationCars.end(), sortCarByX); MagicTree mt = createMagicTree(cars); for(int i = 0 ; i < destinationCars.size() ; ++i) { Car c = destinationCars[i]; int fetchedSize = mt.fetch(c); mt.remove(c); if(fetchedSize + c.height > w) { printf("NIE\n"); mt.clean(); return; } } mt.clean(); printf("TAK\n"); } int main() { int tests; scanf("%d", &tests); while(tests--) { test(); } 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 | #include <stdio.h> #include <vector> #include <algorithm> #include <stdlib.h> #include <map> using namespace std; struct Car { int x; int height; int index; void print() { printf("%d: %d %d", index, x, height); } }; bool operator==(const Car &left, const Car &right) { return left.index==right.index; } bool operator>(const Car &left, const Car &right) { return left.index>right.index; } bool operator<(const Car &left, const Car &right) { return left.index<right.index; } bool sortCarByX(Car first, Car second) { return first.x < second.x; } struct Node { Node *left; Node *right; Node *parent; int leftMax; int rightMax; int value; int index; Node(int val, int idx) { left = NULL; right = NULL; parent = NULL; leftMax = 0; rightMax = 0; value = val; index = idx; } void add(Node *node) { if(index > node->index) { if(left == NULL) { node->parent = this; left = node; } else { left->add(node); } leftMax = max(leftMax, node->value); } else { if(right == NULL) { node->parent = this; right = node; } else { right->add(node); } rightMax = max(rightMax, node->value); } } void clean() { if(left != NULL) { left->clean(); delete left; } if(right != NULL) { right->clean(); delete right; } } void print() { printf("I: %d, L: %d\n", index, leftMax); if(left != NULL) { left->print(); } if(right != NULL) { right->print(); } } }; struct MagicTree { Node *root; map<Car, Node*> carsToNodes; MagicTree() { root = NULL; } void add(Car &car) { Node *nptr = new Node(car.height, car.index); if(root==NULL) { root = nptr; } else { root->add(nptr); } carsToNodes[car] = nptr; } int fetch(Car &car) { Node *node = carsToNodes[car]; int leftMax = node->leftMax; while(node->parent != NULL) { if(node->parent->right == node) { leftMax = max(leftMax, node->parent->leftMax); leftMax = max(leftMax, node->parent->value); } node = node->parent; } return leftMax; } void remove(Car &car) { Node *node = carsToNodes[car]; node->value = 0; while(node->parent != NULL) { node = node->parent; if(node->left != NULL) { node->leftMax = max(node->left->leftMax, node->left->rightMax); node->leftMax = max(node->leftMax, node->left->value); } if(node->right != NULL) { node->rightMax = max(node->right->leftMax, node->right->rightMax); node->rightMax = max(node->rightMax, node->right->value); } } } void clean() { if(root != NULL) { root->clean(); delete root; } } void print() { root->print(); } }; Car readCar(int idx) { int x1, y1, x2, y2; Car c; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); c.x = min(x1, x2); c.height = abs(y2-y1); c.index = idx; return c; } void balancedAdd(MagicTree &mt, vector<Car> &cars, int firstIdx, int lastIdx) { int middle = (firstIdx+lastIdx)/2; mt.add(cars[middle]); if(lastIdx > firstIdx) { balancedAdd(mt, cars, middle+1, lastIdx); } if(lastIdx > firstIdx+1) { balancedAdd(mt, cars, firstIdx, middle-1); } } MagicTree createMagicTree(vector<Car> &cars) { MagicTree mt = MagicTree(); balancedAdd(mt, cars, 0, cars.size()-1); return mt; } void test () { int n, w; vector<Car> cars; vector<Car> destinationCars; scanf("%d %d", &n, &w); for(int i = 0 ; i < n ; ++i) { Car c = readCar(i); cars.push_back(c); } for(int i = 0 ; i < n ; ++i) { Car c = readCar(i); destinationCars.push_back(c); } sort(destinationCars.begin(), destinationCars.end(), sortCarByX); MagicTree mt = createMagicTree(cars); for(int i = 0 ; i < destinationCars.size() ; ++i) { Car c = destinationCars[i]; int fetchedSize = mt.fetch(c); mt.remove(c); if(fetchedSize + c.height > w) { printf("NIE\n"); mt.clean(); return; } } mt.clean(); printf("TAK\n"); } int main() { int tests; scanf("%d", &tests); while(tests--) { test(); } return 0; } |