// Sebastian Jaszczur #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; class Car { public: int h; int pos; int target; int max_h; }; bool cmp(Car a, Car b) { return a.pos < b.pos; } int GLOBALH; bool merge(vector<Car> &vec, int beg, int end) { if(end - beg <= 1) return true; if(merge(vec, beg, (beg+end)/2) == false) return false; if(merge(vec, (beg+end)/2, end) == false) return false; vec[(beg+end)/2-1].max_h = vec[(beg+end)/2-1].h; for(int iv = (beg+end)/2-2; iv >= beg; iv--) { vec[iv].max_h = max(vec[iv].h, vec[iv+1].max_h); } int ivb = beg; int ive = (beg+end)/2; vector<Car> result; while(ivb < (beg+end)/2 and ive<end) { if(vec[ivb].target <= vec[ive].target) { result.push_back(vec[ivb]); ivb += 1; continue; }else { if(vec[ivb].max_h + vec[ive].h > GLOBALH) return false; result.push_back(vec[ive]); ive += 1; continue; } } while(ivb < (beg+end)/2) { result.push_back(vec[ivb]); ivb += 1; } while(ive<end) { result.push_back(vec[ive]); ive += 1; } for(int i=0; i<result.size(); i++) { vec[beg+i] = result[i]; } return true; } int main() { int t; cin>>t; for(int it=0; it<t; it++) { int n, w; cin>>n>>w; GLOBALH = w; vector<Car> parking; parking.resize(n); for(int in=0; in<n; in++) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; parking[in].h = abs(y1-y2); parking[in].pos = min(x1, x2); } for(int in=0; in<n; in++) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; //parking[in].h = abs(y1-y2); //not necessary, of course parking[in].target = min(x1, x2); } sort(parking.begin(), parking.end(), cmp); if(merge(parking, 0, parking.size())) cout<<"TAK\n"; else cout<<"NIE\n"; } } /* 2 3 3 0 0 2 2 2 1 4 3 4 0 6 1 0 0 2 2 2 1 4 3 0 2 2 3 3 3 0 0 2 2 2 1 4 3 4 0 6 1 2 1 4 3 0 0 2 2 4 0 6 1 */
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 | // Sebastian Jaszczur #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; class Car { public: int h; int pos; int target; int max_h; }; bool cmp(Car a, Car b) { return a.pos < b.pos; } int GLOBALH; bool merge(vector<Car> &vec, int beg, int end) { if(end - beg <= 1) return true; if(merge(vec, beg, (beg+end)/2) == false) return false; if(merge(vec, (beg+end)/2, end) == false) return false; vec[(beg+end)/2-1].max_h = vec[(beg+end)/2-1].h; for(int iv = (beg+end)/2-2; iv >= beg; iv--) { vec[iv].max_h = max(vec[iv].h, vec[iv+1].max_h); } int ivb = beg; int ive = (beg+end)/2; vector<Car> result; while(ivb < (beg+end)/2 and ive<end) { if(vec[ivb].target <= vec[ive].target) { result.push_back(vec[ivb]); ivb += 1; continue; }else { if(vec[ivb].max_h + vec[ive].h > GLOBALH) return false; result.push_back(vec[ive]); ive += 1; continue; } } while(ivb < (beg+end)/2) { result.push_back(vec[ivb]); ivb += 1; } while(ive<end) { result.push_back(vec[ive]); ive += 1; } for(int i=0; i<result.size(); i++) { vec[beg+i] = result[i]; } return true; } int main() { int t; cin>>t; for(int it=0; it<t; it++) { int n, w; cin>>n>>w; GLOBALH = w; vector<Car> parking; parking.resize(n); for(int in=0; in<n; in++) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; parking[in].h = abs(y1-y2); parking[in].pos = min(x1, x2); } for(int in=0; in<n; in++) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; //parking[in].h = abs(y1-y2); //not necessary, of course parking[in].target = min(x1, x2); } sort(parking.begin(), parking.end(), cmp); if(merge(parking, 0, parking.size())) cout<<"TAK\n"; else cout<<"NIE\n"; } } /* 2 3 3 0 0 2 2 2 1 4 3 4 0 6 1 0 0 2 2 2 1 4 3 0 2 2 3 3 3 0 0 2 2 2 1 4 3 4 0 6 1 2 1 4 3 0 0 2 2 4 0 6 1 */ |