#include <iostream> #include <algorithm> #include <stdio.h> #include <string> #include <vector> #include <map> #include <stack> #include <set> #include <math.h> #include <queue> #define INF 1000000007 #define MAXN 100002 using namespace std; int W; struct Point { pair < int, int > p; bool fromRight; }; bool merge(vector< pair <int, int> > & vec, int low, int pivot, int high, vector < Point > & temp) { int posToAdd = low; int leftPos = low; int rightPos = pivot + 1; while(leftPos <= pivot && rightPos <= high) { if(vec[leftPos].first <= vec[rightPos].first) { temp[posToAdd].p = vec[leftPos]; temp[posToAdd].fromRight = false; posToAdd++; leftPos++; } else { temp[posToAdd].p = vec[rightPos]; temp[posToAdd].fromRight = true; posToAdd++; rightPos++; } } while(leftPos <= pivot) { temp[posToAdd].p = vec[leftPos]; temp[posToAdd].fromRight = false; posToAdd++; leftPos++; } while(rightPos <= high) { temp[posToAdd].p = vec[rightPos]; temp[posToAdd].fromRight = true; posToAdd++; rightPos++; } int highestLeftVal = 0; for(int i = posToAdd - 1; i >= low; i--) { int currVal = temp[i].p.second; if(temp[i].fromRight && currVal + highestLeftVal > W) return false; if(temp[i].fromRight == false) highestLeftVal = max(highestLeftVal, currVal); vec[i] = temp[i].p; } return true; } bool mergesort(vector< pair < int, int > > & vec, int low, int high, vector < Point > & temp) { if(high == low) return true; int pivot = low + ( high - low) / 2; bool wasLeftOk = mergesort(vec, low, pivot, temp); if(!wasLeftOk) return false; bool wasRightOk = mergesort(vec, pivot + 1, high, temp); if(!wasRightOk) return false; bool wasMergeOk = merge(vec, low, pivot, high, temp); return wasMergeOk; } int main() { cin.tie(NULL); ios::sync_with_stdio(false); int T; cin >> T; for(int test = 0; test < T; test++) { int N; cin >> N >> W; vector< pair<int, int> > startPos(N); vector< pair<int, int> > endPos(N); vector< int > val(N); int x1, y1, x2, y2; for(int carId = 0; carId < N; carId++) { cin >> x1 >> y1 >> x2 >> y2; startPos[carId].first = min(x1, x2); startPos[carId].second = carId; val[carId] = abs(y1 - y2); } for(int carId = 0; carId < N; carId++) { cin >> x1 >> y1 >> x2 >> y2; endPos[carId].first = min(x1, x2); endPos[carId].second = carId; } sort(startPos.begin(), startPos.end()); sort(endPos.begin(), endPos.end()); vector< int > carEndPosition(N); for(int i = 0; i < endPos.size(); i++) { int carId = endPos[i].second; carEndPosition[carId] = i; } for(int i = 0; i < startPos.size(); i++) { int carId = startPos[i].second; startPos[i].first = carEndPosition[carId]; startPos[i].second = val[carId]; } vector< Point > temp(N); bool isOk = mergesort(startPos, 0, startPos.size() - 1, temp); if(isOk) cout << "TAK"; else cout << "NIE"; if(test != T - 1) cout << "\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 | #include <iostream> #include <algorithm> #include <stdio.h> #include <string> #include <vector> #include <map> #include <stack> #include <set> #include <math.h> #include <queue> #define INF 1000000007 #define MAXN 100002 using namespace std; int W; struct Point { pair < int, int > p; bool fromRight; }; bool merge(vector< pair <int, int> > & vec, int low, int pivot, int high, vector < Point > & temp) { int posToAdd = low; int leftPos = low; int rightPos = pivot + 1; while(leftPos <= pivot && rightPos <= high) { if(vec[leftPos].first <= vec[rightPos].first) { temp[posToAdd].p = vec[leftPos]; temp[posToAdd].fromRight = false; posToAdd++; leftPos++; } else { temp[posToAdd].p = vec[rightPos]; temp[posToAdd].fromRight = true; posToAdd++; rightPos++; } } while(leftPos <= pivot) { temp[posToAdd].p = vec[leftPos]; temp[posToAdd].fromRight = false; posToAdd++; leftPos++; } while(rightPos <= high) { temp[posToAdd].p = vec[rightPos]; temp[posToAdd].fromRight = true; posToAdd++; rightPos++; } int highestLeftVal = 0; for(int i = posToAdd - 1; i >= low; i--) { int currVal = temp[i].p.second; if(temp[i].fromRight && currVal + highestLeftVal > W) return false; if(temp[i].fromRight == false) highestLeftVal = max(highestLeftVal, currVal); vec[i] = temp[i].p; } return true; } bool mergesort(vector< pair < int, int > > & vec, int low, int high, vector < Point > & temp) { if(high == low) return true; int pivot = low + ( high - low) / 2; bool wasLeftOk = mergesort(vec, low, pivot, temp); if(!wasLeftOk) return false; bool wasRightOk = mergesort(vec, pivot + 1, high, temp); if(!wasRightOk) return false; bool wasMergeOk = merge(vec, low, pivot, high, temp); return wasMergeOk; } int main() { cin.tie(NULL); ios::sync_with_stdio(false); int T; cin >> T; for(int test = 0; test < T; test++) { int N; cin >> N >> W; vector< pair<int, int> > startPos(N); vector< pair<int, int> > endPos(N); vector< int > val(N); int x1, y1, x2, y2; for(int carId = 0; carId < N; carId++) { cin >> x1 >> y1 >> x2 >> y2; startPos[carId].first = min(x1, x2); startPos[carId].second = carId; val[carId] = abs(y1 - y2); } for(int carId = 0; carId < N; carId++) { cin >> x1 >> y1 >> x2 >> y2; endPos[carId].first = min(x1, x2); endPos[carId].second = carId; } sort(startPos.begin(), startPos.end()); sort(endPos.begin(), endPos.end()); vector< int > carEndPosition(N); for(int i = 0; i < endPos.size(); i++) { int carId = endPos[i].second; carEndPosition[carId] = i; } for(int i = 0; i < startPos.size(); i++) { int carId = startPos[i].second; startPos[i].first = carEndPosition[carId]; startPos[i].second = val[carId]; } vector< Point > temp(N); bool isOk = mergesort(startPos, 0, startPos.size() - 1, temp); if(isOk) cout << "TAK"; else cout << "NIE"; if(test != T - 1) cout << "\n"; } return 0; } |