#include<iostream> #include<vector> #include<algorithm> #include<cmath> #include<numeric> #include<string> #include<set> #include<queue> typedef long long ll; struct Car { ll position; ll height; int index; }; void readCars(const int N, std::vector<Car>& cars) { for(int i = 0; i < N; ++i) { Car c; ll x1, x2, y1, y2; std::cin>>x1>>y1>>x2>>y2; c.height = std::abs(y2 - y1); c.position = std::min(x1, x2); c.index = i; cars.push_back(c); } } struct CmpPos { bool operator()(const Car& lhs, const Car& rhs) { return lhs.position < rhs.position; } }; struct CmpHeight { bool operator()(const Car& lhs, const Car rhs) { return lhs.height > rhs.height; } }; struct Interval { int s, e, idx; }; struct CmpInterval { bool operator()(const Interval& lhs, const Interval& rhs) { return lhs.s < rhs.s; } }; int sign(const ll v) { if(v == 0) return 0; return (v < 0)?-1:1; } void solve() { ll parkingHeight; int carCount; std::cin>>carCount>>parkingHeight; std::vector<Car> startingDescription, endingDescription, original; startingDescription.reserve(carCount); endingDescription.reserve(carCount); readCars(carCount, original); readCars(carCount, endingDescription); startingDescription = original; std::vector<int> source(carCount), destination(carCount); std::sort(startingDescription.begin(), startingDescription.end(), CmpPos()); std::sort(endingDescription.begin(), endingDescription.end(), CmpPos()); for(int i = 0; i < carCount; ++i) { source[startingDescription[i].index] = i; destination[endingDescription[i].index] = i; } std::vector<Interval> intervals; intervals.reserve(carCount); for(int i = 0; i < carCount; ++i) { Interval in; in.s = std::min(source[i], destination[i]); in.e = std::max(source[i], destination[i]); in.idx = i; intervals.push_back(in); } std::sort(intervals.begin(), intervals.end(), CmpInterval()); for(auto i = intervals.begin(); i != intervals.end(); ++i) { for(auto j = i + 1; j != intervals.end(); ++j) { if(j->s > i->e) { break; } ll hSum = original[i->idx].height + original[j->idx].height; if(sign(source[i->idx] - source[j->idx]) != sign(destination[i->idx] - destination[j->idx]) && hSum > parkingHeight) { std::cout<<"NIE"<<std::endl; return; } } } std::cout<<"TAK"<<std::endl; } int main(int argc, char** argv) { std::ios_base::sync_with_stdio(false); int t; std::cin>>t; while(t--) { solve(); } 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 | #include<iostream> #include<vector> #include<algorithm> #include<cmath> #include<numeric> #include<string> #include<set> #include<queue> typedef long long ll; struct Car { ll position; ll height; int index; }; void readCars(const int N, std::vector<Car>& cars) { for(int i = 0; i < N; ++i) { Car c; ll x1, x2, y1, y2; std::cin>>x1>>y1>>x2>>y2; c.height = std::abs(y2 - y1); c.position = std::min(x1, x2); c.index = i; cars.push_back(c); } } struct CmpPos { bool operator()(const Car& lhs, const Car& rhs) { return lhs.position < rhs.position; } }; struct CmpHeight { bool operator()(const Car& lhs, const Car rhs) { return lhs.height > rhs.height; } }; struct Interval { int s, e, idx; }; struct CmpInterval { bool operator()(const Interval& lhs, const Interval& rhs) { return lhs.s < rhs.s; } }; int sign(const ll v) { if(v == 0) return 0; return (v < 0)?-1:1; } void solve() { ll parkingHeight; int carCount; std::cin>>carCount>>parkingHeight; std::vector<Car> startingDescription, endingDescription, original; startingDescription.reserve(carCount); endingDescription.reserve(carCount); readCars(carCount, original); readCars(carCount, endingDescription); startingDescription = original; std::vector<int> source(carCount), destination(carCount); std::sort(startingDescription.begin(), startingDescription.end(), CmpPos()); std::sort(endingDescription.begin(), endingDescription.end(), CmpPos()); for(int i = 0; i < carCount; ++i) { source[startingDescription[i].index] = i; destination[endingDescription[i].index] = i; } std::vector<Interval> intervals; intervals.reserve(carCount); for(int i = 0; i < carCount; ++i) { Interval in; in.s = std::min(source[i], destination[i]); in.e = std::max(source[i], destination[i]); in.idx = i; intervals.push_back(in); } std::sort(intervals.begin(), intervals.end(), CmpInterval()); for(auto i = intervals.begin(); i != intervals.end(); ++i) { for(auto j = i + 1; j != intervals.end(); ++j) { if(j->s > i->e) { break; } ll hSum = original[i->idx].height + original[j->idx].height; if(sign(source[i->idx] - source[j->idx]) != sign(destination[i->idx] - destination[j->idx]) && hSum > parkingHeight) { std::cout<<"NIE"<<std::endl; return; } } } std::cout<<"TAK"<<std::endl; } int main(int argc, char** argv) { std::ios_base::sync_with_stdio(false); int t; std::cin>>t; while(t--) { solve(); } return 0; } |