#include <cstdio> #include <vector> #include <algorithm> #include <map> using namespace std; struct Car{ int x1, x2; int ex1, ex2; int h; Car(){} bool isMoveRight() { return (x2 - x1 >= 0); } bool operator<(const Car& other) const { return x1 < other.x1; } }; vector<Car> cars; void printCars(){ for(int i=0; i<(int)cars.size(); i++){ Car &c = cars[i]; printf("x1=%d ex1=%d x2=%d ex2=%d h=%d\n", c.x1, c.ex1, c.x2, c.ex2, c.h); } printf("----------\n"); } int rescale(){ map<int, int> myMap; map<int, int>::iterator it; for(int i=0; i<(int)cars.size(); i++){ Car &car = cars[i]; myMap[car.x1] = 0; myMap[car.ex1] = 0; myMap[car.x2] = 0; myMap[car.ex2] = 0; } int num = 1; for(it = myMap.begin(); it != myMap.end(); it++) it->second = num++; for(int i=0; i<(int)cars.size(); i++){ Car &car = cars[i]; car.x1 = myMap[car.x1]; car.ex1 = myMap[car.ex1]; car.x2 = myMap[car.x2]; car.ex2 = myMap[car.ex2]; } return num - 1; } int add_range(int *tree, int a, int b, int v, int p, int k, int pos){ int *t = tree+pos; if(a <= p && b >= k){ *t = max(*t, v); return *t; } if(p > b || a > k) return 0; int p1 = p, k1 = (p+k)/2; int p2 = k1 + 1, k2 = k; int l = add_range(tree,a,b,v,p1,k1,2*pos); int r = add_range(tree,a,b,v,p2,k2,2*pos+1); *t = max(max(l, r), *t); return *t; } int range_max(int *tree, int a, int b, int p, int k, int pos){ int *t = tree+pos; if(p > b || a > k) return 0; if(a <= p && b >= k) return *t; int p1 = p, k1 = (p+k)/2; int p2 = k1 + 1, k2 = k; int l = range_max(tree,a,b,p1,k1,2*pos); int r = range_max(tree,a,b,p2,k2,2*pos+1); return max(l, r); } void printTree(int *tree, int N){ for(int i=1; i<2*N; i++) printf("%d: %d | ", i, tree[i]); printf("\n--------------\n"); } bool solve(int maxX, int hLimit){ int N = 1; while(N < maxX) N *= 2; int *treeLeft = new int[2 * N]; int *treeRight = new int[2 * N]; vector<Car> leftMoves; vector<Car> rightMoves; for(int i=0; i<2*N; i++){ treeLeft[i] = 0; treeRight[i] = 0; } for(int i=0; i<(int)cars.size(); i++){ if(cars[i].isMoveRight()) rightMoves.push_back(cars[i]); else leftMoves.push_back(cars[i]); } sort(rightMoves.begin(), rightMoves.end()); for(int i=(int)rightMoves.size()-1; i>=0; i--){ Car &c = rightMoves[i]; int mx = range_max(treeRight, c.x1, c.ex2 - 1, 1, N, 1); if(mx + c.h > hLimit) return false; add_range(treeRight, c.x2, c.ex2 - 1, c.h, 1, N, 1); //printTree(treeRight, N); } sort(leftMoves.begin(), leftMoves.end()); for(int i=0; i<(int)leftMoves.size(); i++){ Car &c = leftMoves[i]; int mx = range_max(treeLeft, c.x2, c.ex1 - 1, 1, N, 1); if(mx + c.h > hLimit) return false; add_range(treeLeft, c.x2, c.ex2 - 1, c.h, 1, N, 1); // printTree(treeLeft, N); } for(int i=0; i<2*N; i++) treeRight[i] = 0; for(int i=0; i<(int)rightMoves.size(); i++){ Car &c = rightMoves[i]; add_range(treeRight, c.x1, c.ex2 - 1, c.h, 1, N, 1); } for(int i=0; i<(int)leftMoves.size(); i++){ Car &c = leftMoves[i]; int mx = range_max(treeRight, c.x2, c.ex1 - 1, 1, N, 1); if(mx + c.h > hLimit) return false; } delete [] treeLeft; delete [] treeRight; return true; } int main(){ int t, n, w; scanf("%d", &t); for(int i=0; i<t; i++){ scanf("%d%d", &n, &w); cars.clear(); cars.reserve(n); for(int j=0; j<n; j++){ Car car; int y1, y2, x1, x2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); car.x1 = min(x1, x2); car.ex1 = max(x1, x2); car.h = abs(y1 - y2); cars.push_back(car); } for(int j=0; j<n; j++){ int y1, y2, x1, x2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); cars[j].x2 = min(x1, x2); cars[j].ex2 = max(x1, x2); } int maxX = rescale(); bool res = solve(maxX, w); if(res) 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 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 | #include <cstdio> #include <vector> #include <algorithm> #include <map> using namespace std; struct Car{ int x1, x2; int ex1, ex2; int h; Car(){} bool isMoveRight() { return (x2 - x1 >= 0); } bool operator<(const Car& other) const { return x1 < other.x1; } }; vector<Car> cars; void printCars(){ for(int i=0; i<(int)cars.size(); i++){ Car &c = cars[i]; printf("x1=%d ex1=%d x2=%d ex2=%d h=%d\n", c.x1, c.ex1, c.x2, c.ex2, c.h); } printf("----------\n"); } int rescale(){ map<int, int> myMap; map<int, int>::iterator it; for(int i=0; i<(int)cars.size(); i++){ Car &car = cars[i]; myMap[car.x1] = 0; myMap[car.ex1] = 0; myMap[car.x2] = 0; myMap[car.ex2] = 0; } int num = 1; for(it = myMap.begin(); it != myMap.end(); it++) it->second = num++; for(int i=0; i<(int)cars.size(); i++){ Car &car = cars[i]; car.x1 = myMap[car.x1]; car.ex1 = myMap[car.ex1]; car.x2 = myMap[car.x2]; car.ex2 = myMap[car.ex2]; } return num - 1; } int add_range(int *tree, int a, int b, int v, int p, int k, int pos){ int *t = tree+pos; if(a <= p && b >= k){ *t = max(*t, v); return *t; } if(p > b || a > k) return 0; int p1 = p, k1 = (p+k)/2; int p2 = k1 + 1, k2 = k; int l = add_range(tree,a,b,v,p1,k1,2*pos); int r = add_range(tree,a,b,v,p2,k2,2*pos+1); *t = max(max(l, r), *t); return *t; } int range_max(int *tree, int a, int b, int p, int k, int pos){ int *t = tree+pos; if(p > b || a > k) return 0; if(a <= p && b >= k) return *t; int p1 = p, k1 = (p+k)/2; int p2 = k1 + 1, k2 = k; int l = range_max(tree,a,b,p1,k1,2*pos); int r = range_max(tree,a,b,p2,k2,2*pos+1); return max(l, r); } void printTree(int *tree, int N){ for(int i=1; i<2*N; i++) printf("%d: %d | ", i, tree[i]); printf("\n--------------\n"); } bool solve(int maxX, int hLimit){ int N = 1; while(N < maxX) N *= 2; int *treeLeft = new int[2 * N]; int *treeRight = new int[2 * N]; vector<Car> leftMoves; vector<Car> rightMoves; for(int i=0; i<2*N; i++){ treeLeft[i] = 0; treeRight[i] = 0; } for(int i=0; i<(int)cars.size(); i++){ if(cars[i].isMoveRight()) rightMoves.push_back(cars[i]); else leftMoves.push_back(cars[i]); } sort(rightMoves.begin(), rightMoves.end()); for(int i=(int)rightMoves.size()-1; i>=0; i--){ Car &c = rightMoves[i]; int mx = range_max(treeRight, c.x1, c.ex2 - 1, 1, N, 1); if(mx + c.h > hLimit) return false; add_range(treeRight, c.x2, c.ex2 - 1, c.h, 1, N, 1); //printTree(treeRight, N); } sort(leftMoves.begin(), leftMoves.end()); for(int i=0; i<(int)leftMoves.size(); i++){ Car &c = leftMoves[i]; int mx = range_max(treeLeft, c.x2, c.ex1 - 1, 1, N, 1); if(mx + c.h > hLimit) return false; add_range(treeLeft, c.x2, c.ex2 - 1, c.h, 1, N, 1); // printTree(treeLeft, N); } for(int i=0; i<2*N; i++) treeRight[i] = 0; for(int i=0; i<(int)rightMoves.size(); i++){ Car &c = rightMoves[i]; add_range(treeRight, c.x1, c.ex2 - 1, c.h, 1, N, 1); } for(int i=0; i<(int)leftMoves.size(); i++){ Car &c = leftMoves[i]; int mx = range_max(treeRight, c.x2, c.ex1 - 1, 1, N, 1); if(mx + c.h > hLimit) return false; } delete [] treeLeft; delete [] treeRight; return true; } int main(){ int t, n, w; scanf("%d", &t); for(int i=0; i<t; i++){ scanf("%d%d", &n, &w); cars.clear(); cars.reserve(n); for(int j=0; j<n; j++){ Car car; int y1, y2, x1, x2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); car.x1 = min(x1, x2); car.ex1 = max(x1, x2); car.h = abs(y1 - y2); cars.push_back(car); } for(int j=0; j<n; j++){ int y1, y2, x1, x2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); cars[j].x2 = min(x1, x2); cars[j].ex2 = max(x1, x2); } int maxX = rescale(); bool res = solve(maxX, w); if(res) printf("TAK\n"); else printf("NIE\n"); } return 0; } |