#include <cstdlib> #include <climits> #include <cstdarg> #include <cstdio> #include <algorithm> #include <vector> using namespace std; #ifdef DEBUG int debugf(char const *format, ...) { int ret; va_list args; va_start(args, format); ret = vfprintf(stderr, format, args); fflush(stderr); va_end(args); return ret; } #else int debugf(char const */*format*/, ...) { return 0; } #endif struct Car { int id, x1, y1, x2, y2; Car() : id(-1), x1(0), y1(0), x2(0), y2(0) { fix(); } Car(Car const &o) : id(o.id), x1(o.x1), y1(o.y1), x2(o.x2), y2(o.y2) { fix(); } Car(int const &id, int const &x1, int const &y1, int const &x2, int const &y2) : id(id), x1(x1), y1(y1), x2(x2), y2(y2) { fix(); } ~Car() { } void fix() { if(x2 < x1) swap(x1, x2); if(y2 < y1) swap(y1, y2); } Car& operator=(Car const &o) { id = o.id; x1 = o.x1; y1 = o.y1; x2 = o.x2; y2 = o.y2; return *this; } int getHeight(void) const { return y2 - y1; } bool operator<(Car const &o) const { return x1 < o.x1; } operator int() const { return id; } }; struct MaximumsTree { int v; int i0, ih, i1; MaximumsTree *l, *r; void init(vector<Car> const &arr, int const &_i0, int const &_i1) { i0 = _i0; i1 = _i1; ih = (i0 + i1) >> 1; if(i0 == i1) { v = arr[i0].getHeight(); l = NULL; r = NULL; } else { v = INT_MIN; if(i0 <= ih) { l = new MaximumsTree(arr, i0, ih); v = max(v, l->v); } else { l = NULL; } if(ih+1 <= i1) { r = new MaximumsTree(arr, ih+1, i1); v = max(v, r->v); } else { r = NULL; } } } MaximumsTree(vector<Car> const &arr) { init(arr, 0, arr.size()-1); } MaximumsTree(vector<Car> const &arr, int const &i0, int const &i1) { init(arr, i0, i1); } ~MaximumsTree() { if(l) { delete l; l = NULL; } if(r) { delete r; r = NULL; } } int getFromRange(int const &q0, int const &q1) { if(q1 < q0) return getFromRange(q1, q0); if(i0 > q1 or i1 < q0) return INT_MIN; if(q0 <= i0 and i1 <= q1) return v; int ret = INT_MIN; if(l) ret = max(ret, l->getFromRange(q0, q1)); if(r) ret = max(ret, r->getFromRange(q0, q1)); return ret; } void update(int const &i, int const &v) { if(i < i0 or i1 < i) return; if(i0 == i1 and i0 == i) this->v = v; if(l) l->update(i, v); if(r) r->update(i, v); this->v = INT_MIN; if(l) this->v = max(this->v, l->v); if(r) this->v = max(this->v, r->v); } void debug(int const &d=0) { for(int i = 0; i < d; ++i) debugf(" "); debugf("|--"); debugf(">[%d;%d](%d)\n", i0, i1, v); if(l) l->debug(d+1); if(r) r->debug(d+1); } void debug2(int const &d=0) { if(d == 0) { debugf("\n"); for(int q0 = i0; q0 <= i1; ++q0) { for(int q1 = q0; q1 <= i1; ++q1) { debugf("#[%d;%d] -> (%d)\n", q0, q1, getFromRange(q0, q1)); } } } } }; void test() { int n, w; scanf("%d %d", &n, &w); vector<Car> A(n); vector<Car> B(n); for(int i = 0; i < n; ++i) { A[i].id = i; scanf("%d %d %d %d", &(A[i].x1), &(A[i].y1), &(A[i].x2), &(A[i].y2)); } sort(A.begin(), A.end()); for(int i = 0; i < n; ++i) { B[i].id = i; scanf("%d %d %d %d", &(B[i].x1), &(B[i].y1), &(B[i].x2), &(B[i].y2)); } sort(B.begin(), B.end()); debugf("A: "); for(int i = 0; i < n; ++i) debugf("%d ", A[i]+1); debugf("\n"); debugf("B: "); for(int i = 0; i < n; ++i) debugf("%d ", B[i]+1); debugf("\n"); vector<int> rB(n); for(int i = 0; i < n; ++i) rB[B[i].id] = i; vector<int> AB(n); for(int i = 0; i < n; ++i) AB[i] = rB[A[i].id]; MaximumsTree MT(A); MT.debug(); // MT.debug2(); debugf("AB: "); for(int i = 0; i < n; ++i) debugf("%d ", AB[i]+1); debugf("\n"); for(int i = 0; i < n; ++i) { if(i != AB[i]) { int h1 = A[i].getHeight(); int h2 = A[AB[i]].getHeight(); int hp = 0; if(i < AB[i]) { if(AB[i] - i >= 2) { hp = MT.getFromRange(i+1, AB[i]-1); } } else { if(i - AB[i] >= 2) { hp = MT.getFromRange(AB[i]+1, i-1); } } debugf("h1=%d, h2=%d, hp=%d\n", h1, h2, hp); if(h1 + hp > w or h2 + hp > w or h1 + h2 > w) { printf("NIE\n"); return; } swap(A[i], A[AB[i]]); swap(AB[i], AB[AB[i]]); MT.update(i, A[i].getHeight()); MT.update(AB[i], A[AB[i]].getHeight()); } } printf("TAK\n"); } int main(int const /*argc*/, char const * const * /*argv*/) { int t; scanf("%d", &t); while(t --> 0) test(); return EXIT_SUCCESS; }
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 | #include <cstdlib> #include <climits> #include <cstdarg> #include <cstdio> #include <algorithm> #include <vector> using namespace std; #ifdef DEBUG int debugf(char const *format, ...) { int ret; va_list args; va_start(args, format); ret = vfprintf(stderr, format, args); fflush(stderr); va_end(args); return ret; } #else int debugf(char const */*format*/, ...) { return 0; } #endif struct Car { int id, x1, y1, x2, y2; Car() : id(-1), x1(0), y1(0), x2(0), y2(0) { fix(); } Car(Car const &o) : id(o.id), x1(o.x1), y1(o.y1), x2(o.x2), y2(o.y2) { fix(); } Car(int const &id, int const &x1, int const &y1, int const &x2, int const &y2) : id(id), x1(x1), y1(y1), x2(x2), y2(y2) { fix(); } ~Car() { } void fix() { if(x2 < x1) swap(x1, x2); if(y2 < y1) swap(y1, y2); } Car& operator=(Car const &o) { id = o.id; x1 = o.x1; y1 = o.y1; x2 = o.x2; y2 = o.y2; return *this; } int getHeight(void) const { return y2 - y1; } bool operator<(Car const &o) const { return x1 < o.x1; } operator int() const { return id; } }; struct MaximumsTree { int v; int i0, ih, i1; MaximumsTree *l, *r; void init(vector<Car> const &arr, int const &_i0, int const &_i1) { i0 = _i0; i1 = _i1; ih = (i0 + i1) >> 1; if(i0 == i1) { v = arr[i0].getHeight(); l = NULL; r = NULL; } else { v = INT_MIN; if(i0 <= ih) { l = new MaximumsTree(arr, i0, ih); v = max(v, l->v); } else { l = NULL; } if(ih+1 <= i1) { r = new MaximumsTree(arr, ih+1, i1); v = max(v, r->v); } else { r = NULL; } } } MaximumsTree(vector<Car> const &arr) { init(arr, 0, arr.size()-1); } MaximumsTree(vector<Car> const &arr, int const &i0, int const &i1) { init(arr, i0, i1); } ~MaximumsTree() { if(l) { delete l; l = NULL; } if(r) { delete r; r = NULL; } } int getFromRange(int const &q0, int const &q1) { if(q1 < q0) return getFromRange(q1, q0); if(i0 > q1 or i1 < q0) return INT_MIN; if(q0 <= i0 and i1 <= q1) return v; int ret = INT_MIN; if(l) ret = max(ret, l->getFromRange(q0, q1)); if(r) ret = max(ret, r->getFromRange(q0, q1)); return ret; } void update(int const &i, int const &v) { if(i < i0 or i1 < i) return; if(i0 == i1 and i0 == i) this->v = v; if(l) l->update(i, v); if(r) r->update(i, v); this->v = INT_MIN; if(l) this->v = max(this->v, l->v); if(r) this->v = max(this->v, r->v); } void debug(int const &d=0) { for(int i = 0; i < d; ++i) debugf(" "); debugf("|--"); debugf(">[%d;%d](%d)\n", i0, i1, v); if(l) l->debug(d+1); if(r) r->debug(d+1); } void debug2(int const &d=0) { if(d == 0) { debugf("\n"); for(int q0 = i0; q0 <= i1; ++q0) { for(int q1 = q0; q1 <= i1; ++q1) { debugf("#[%d;%d] -> (%d)\n", q0, q1, getFromRange(q0, q1)); } } } } }; void test() { int n, w; scanf("%d %d", &n, &w); vector<Car> A(n); vector<Car> B(n); for(int i = 0; i < n; ++i) { A[i].id = i; scanf("%d %d %d %d", &(A[i].x1), &(A[i].y1), &(A[i].x2), &(A[i].y2)); } sort(A.begin(), A.end()); for(int i = 0; i < n; ++i) { B[i].id = i; scanf("%d %d %d %d", &(B[i].x1), &(B[i].y1), &(B[i].x2), &(B[i].y2)); } sort(B.begin(), B.end()); debugf("A: "); for(int i = 0; i < n; ++i) debugf("%d ", A[i]+1); debugf("\n"); debugf("B: "); for(int i = 0; i < n; ++i) debugf("%d ", B[i]+1); debugf("\n"); vector<int> rB(n); for(int i = 0; i < n; ++i) rB[B[i].id] = i; vector<int> AB(n); for(int i = 0; i < n; ++i) AB[i] = rB[A[i].id]; MaximumsTree MT(A); MT.debug(); // MT.debug2(); debugf("AB: "); for(int i = 0; i < n; ++i) debugf("%d ", AB[i]+1); debugf("\n"); for(int i = 0; i < n; ++i) { if(i != AB[i]) { int h1 = A[i].getHeight(); int h2 = A[AB[i]].getHeight(); int hp = 0; if(i < AB[i]) { if(AB[i] - i >= 2) { hp = MT.getFromRange(i+1, AB[i]-1); } } else { if(i - AB[i] >= 2) { hp = MT.getFromRange(AB[i]+1, i-1); } } debugf("h1=%d, h2=%d, hp=%d\n", h1, h2, hp); if(h1 + hp > w or h2 + hp > w or h1 + h2 > w) { printf("NIE\n"); return; } swap(A[i], A[AB[i]]); swap(AB[i], AB[AB[i]]); MT.update(i, A[i].getHeight()); MT.update(AB[i], A[AB[i]].getHeight()); } } printf("TAK\n"); } int main(int const /*argc*/, char const * const * /*argv*/) { int t; scanf("%d", &t); while(t --> 0) test(); return EXIT_SUCCESS; } |