#include <iostream> #include <stack> #include <string> struct Car { int n, x1, x2, w; Car *l, *r, *u; bool proc; }; bool operator<(const Car& a, const Car& b) { return a.x1 < b.x1; } void add(Car ** root, Car * c) { if( c == nullptr ) return; c->u = nullptr; if( *root == nullptr ) { *root = c; return; } Car * cur = *root; while(1) { Car * leaf = (*c < *cur)? cur->l : cur->r; Car ** pleaf = (*c < *cur)? &(cur->l) : &(cur->r); if( leaf == nullptr) { c->u = cur; *pleaf = c; break; } else { cur = leaf; } } } void take(Car **root, Car *c) { if( c->r != nullptr ) c->r->u = nullptr; if( c->l != nullptr ) c->l->u = nullptr; if( *root == c ) { *root = ( c->r != nullptr )? c->r : c->l; } else { if( *c < *c->u ) { c->u->l = nullptr; } else { c->u->r = nullptr; } } if( *root != c->l ) add(root, c->l); if( *root != c->r ) add(root, c->r); c->l = nullptr; c->r = nullptr; c->u = nullptr; } void build(Car ** root, int n) { for(int i = 0; i < n; ++i ) { int x1, x2, y1, y2; Car * c = new Car(); std::cin >> x1 >> y1 >> x2 >> y2; c->x1 = std::min(x1, x2); c->x2 = std::max(x1, x2); c->w = std::abs(y2 - y1); c->n = i; c->proc = false; c->l = nullptr; c->r = nullptr; c->u = nullptr; add(root, c); } } int find_maxw(Car * cars, int begx, int endx, int n) { int maxw = 0; std::stack<Car*> st; st.push(cars); while( !st.empty() ) { Car * c = st.top(); st.pop(); //std::cerr << n << " " << begx << "-" << endx << " " << c->n << " " << c->x1 << "-" << c->x2 << " " << c->w << std::endl; if( c->n != n && begx <= c->x1 && c->x2 <= endx ) { maxw = std::max(maxw, c->w); } if( c->x1 <= begx ) { if( c->r != nullptr) st.push(c->r); } else if( c->x1 >= endx ) { if( c->l != nullptr) st.push(c->l); } else { if( c->r != nullptr) st.push(c->r); if( c->l != nullptr) st.push(c->l); } } return maxw; } Car * find(Car * root, int n) { if(root == nullptr || root->n == n ) return root; Car * c = find(root->l, n); if( c == nullptr ) c = find(root->r, n); return c; } std::string solve() { int n, w; std::cin >> n >> w; Car * beg = nullptr; build(&beg, n); Car * end = nullptr; build(&end, n); std::stack<Car *> st; st.push(end); while( !st.empty() ) { Car * endc = st.top(); if( endc->l != nullptr && ! endc->l->proc ) { st.push(endc->l); continue; } st.pop(); if( endc->r != nullptr ) st.push(endc->r); endc->proc = true; Car * begc = find(beg, endc->n); int x1 = std::min(begc->x1, endc->x1); int x2 = std::max(begc->x1, endc->x2); if( x1 == x2 ) continue; int maxw = find_maxw(beg, x1, x2, begc->n ); // std::cerr << "maxw " << maxw << std::endl; if( w-maxw < endc->w ) { return "NIE"; } take(&beg, begc); begc->x1 = endc->x1; begc->x2 = endc->x2; add(&beg, begc); } return "TAK"; } int main() { std::cin.sync_with_stdio(false); std::cout.sync_with_stdio(false); int t; std::cin >> t; while(t--) std::cout << solve() << std::endl; 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 | #include <iostream> #include <stack> #include <string> struct Car { int n, x1, x2, w; Car *l, *r, *u; bool proc; }; bool operator<(const Car& a, const Car& b) { return a.x1 < b.x1; } void add(Car ** root, Car * c) { if( c == nullptr ) return; c->u = nullptr; if( *root == nullptr ) { *root = c; return; } Car * cur = *root; while(1) { Car * leaf = (*c < *cur)? cur->l : cur->r; Car ** pleaf = (*c < *cur)? &(cur->l) : &(cur->r); if( leaf == nullptr) { c->u = cur; *pleaf = c; break; } else { cur = leaf; } } } void take(Car **root, Car *c) { if( c->r != nullptr ) c->r->u = nullptr; if( c->l != nullptr ) c->l->u = nullptr; if( *root == c ) { *root = ( c->r != nullptr )? c->r : c->l; } else { if( *c < *c->u ) { c->u->l = nullptr; } else { c->u->r = nullptr; } } if( *root != c->l ) add(root, c->l); if( *root != c->r ) add(root, c->r); c->l = nullptr; c->r = nullptr; c->u = nullptr; } void build(Car ** root, int n) { for(int i = 0; i < n; ++i ) { int x1, x2, y1, y2; Car * c = new Car(); std::cin >> x1 >> y1 >> x2 >> y2; c->x1 = std::min(x1, x2); c->x2 = std::max(x1, x2); c->w = std::abs(y2 - y1); c->n = i; c->proc = false; c->l = nullptr; c->r = nullptr; c->u = nullptr; add(root, c); } } int find_maxw(Car * cars, int begx, int endx, int n) { int maxw = 0; std::stack<Car*> st; st.push(cars); while( !st.empty() ) { Car * c = st.top(); st.pop(); //std::cerr << n << " " << begx << "-" << endx << " " << c->n << " " << c->x1 << "-" << c->x2 << " " << c->w << std::endl; if( c->n != n && begx <= c->x1 && c->x2 <= endx ) { maxw = std::max(maxw, c->w); } if( c->x1 <= begx ) { if( c->r != nullptr) st.push(c->r); } else if( c->x1 >= endx ) { if( c->l != nullptr) st.push(c->l); } else { if( c->r != nullptr) st.push(c->r); if( c->l != nullptr) st.push(c->l); } } return maxw; } Car * find(Car * root, int n) { if(root == nullptr || root->n == n ) return root; Car * c = find(root->l, n); if( c == nullptr ) c = find(root->r, n); return c; } std::string solve() { int n, w; std::cin >> n >> w; Car * beg = nullptr; build(&beg, n); Car * end = nullptr; build(&end, n); std::stack<Car *> st; st.push(end); while( !st.empty() ) { Car * endc = st.top(); if( endc->l != nullptr && ! endc->l->proc ) { st.push(endc->l); continue; } st.pop(); if( endc->r != nullptr ) st.push(endc->r); endc->proc = true; Car * begc = find(beg, endc->n); int x1 = std::min(begc->x1, endc->x1); int x2 = std::max(begc->x1, endc->x2); if( x1 == x2 ) continue; int maxw = find_maxw(beg, x1, x2, begc->n ); // std::cerr << "maxw " << maxw << std::endl; if( w-maxw < endc->w ) { return "NIE"; } take(&beg, begc); begc->x1 = endc->x1; begc->x2 = endc->x2; add(&beg, begc); } return "TAK"; } int main() { std::cin.sync_with_stdio(false); std::cout.sync_with_stdio(false); int t; std::cin >> t; while(t--) std::cout << solve() << std::endl; return 0; } |