#include <cstdlib> #include <cstring> #include <algorithm> #include <vector> static const unsigned int MAX_T = 20; static const unsigned int MAX_N = 50 * 1000; static const unsigned int MAX_W = 1e9; static const unsigned int TREE_N = 131072; int sttree[2 * TREE_N]; struct rect { int id; int x, y, width, height; inline rect(int id, int x1, int y1, int x2, int y2) { this->id = id; if (x1 > x2) std::swap(x1, x2); if (y1 > y2) std::swap(y1, y2); x = x1; y = y1; width = x2 - x1; height = y2 - y1; } inline bool operator<(const rect & other) { if (x == other.x) return y < other.y; return x < other.y; } }; struct CompareRects { std::vector<rect> & v; CompareRects(std::vector<rect> & v) : v(v) {} bool operator()(int a, int b) { return v[a] < v[b]; } }; inline void sttree_clear() { memset(sttree, 0, sizeof(sttree)); } inline void sttree_set(int pos, int val) { int id = TREE_N + pos; sttree[id] = val; id /= 2; while (id >= 1) { sttree[id] = std::max(sttree[2*id+0], sttree[2*id+1]); id /= 2; } } inline int sttree_get_maximum_inner(int a, int b, int id, int begin, int end) { //printf("QUERY_INNER %d %d %d %d %d\n", a, b, id, begin, end); if (a + 1 >= b) return sttree[id]; if (begin <= a && b <= end) return sttree[id]; int midpt = (a + b) / 2; int lmax = (begin < midpt) ? sttree_get_maximum_inner(a, midpt, 2*id+0, begin, std::min(midpt, end)) : 0; int rmax = (midpt < end) ? sttree_get_maximum_inner(midpt, b, 2*id+1, std::max(begin, midpt), end) : 0; return std::max(lmax, rmax); } inline int sttree_get_maximum(int begin, int end) { //printf("QUERY %d %d\n", begin, end); if (begin >= end) return 0; return sttree_get_maximum_inner(0, TREE_N, 1, begin, end); } void populateWithRects(std::vector<rect> & v, int n) { v.reserve(n); int x1, y1, x2, y2; for (int i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); v.push_back(rect(i, x1, y1, x2, y2)); } } void populateWithInts(std::vector<int> & v, int n) { v.reserve(n); for (int i = 0; i < n; i++) v.push_back(i); } void setupTree(const std::vector<rect> & rects, const std::vector<int> & ordering, int n) { sttree_clear(); for (int i = 0; i < n; i++) sttree_set(i, rects[ordering[i]].height); } bool sortRects(const std::vector<rect> & rects, const std::vector<int> & ordering, int n, int w) { for (int i : ordering) { const rect & r = rects[i]; if (sttree_get_maximum(0, i) > w - r.height) return false; sttree_set(i, 0); } return true; } bool solve() { int n, w; scanf("%d %d", &n, &w); std::vector<rect> baseRects, destRects; populateWithRects(baseRects, n); populateWithRects(destRects, n); std::vector<int> baseOrdering, destOrdering; populateWithInts(baseOrdering, n); populateWithInts(destOrdering, n); std::sort(baseOrdering.begin(), baseOrdering.end(), CompareRects(baseRects)); std::sort(destOrdering.begin(), destOrdering.end(), CompareRects(destRects)); setupTree(baseRects, baseOrdering, n); return sortRects(destRects, destOrdering, n, w); } int main() { int t; scanf("%d", &t); while (t --> 0) puts(solve() ? "TAK" : "NIE"); 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 175 | #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> static const unsigned int MAX_T = 20; static const unsigned int MAX_N = 50 * 1000; static const unsigned int MAX_W = 1e9; static const unsigned int TREE_N = 131072; int sttree[2 * TREE_N]; struct rect { int id; int x, y, width, height; inline rect(int id, int x1, int y1, int x2, int y2) { this->id = id; if (x1 > x2) std::swap(x1, x2); if (y1 > y2) std::swap(y1, y2); x = x1; y = y1; width = x2 - x1; height = y2 - y1; } inline bool operator<(const rect & other) { if (x == other.x) return y < other.y; return x < other.y; } }; struct CompareRects { std::vector<rect> & v; CompareRects(std::vector<rect> & v) : v(v) {} bool operator()(int a, int b) { return v[a] < v[b]; } }; inline void sttree_clear() { memset(sttree, 0, sizeof(sttree)); } inline void sttree_set(int pos, int val) { int id = TREE_N + pos; sttree[id] = val; id /= 2; while (id >= 1) { sttree[id] = std::max(sttree[2*id+0], sttree[2*id+1]); id /= 2; } } inline int sttree_get_maximum_inner(int a, int b, int id, int begin, int end) { //printf("QUERY_INNER %d %d %d %d %d\n", a, b, id, begin, end); if (a + 1 >= b) return sttree[id]; if (begin <= a && b <= end) return sttree[id]; int midpt = (a + b) / 2; int lmax = (begin < midpt) ? sttree_get_maximum_inner(a, midpt, 2*id+0, begin, std::min(midpt, end)) : 0; int rmax = (midpt < end) ? sttree_get_maximum_inner(midpt, b, 2*id+1, std::max(begin, midpt), end) : 0; return std::max(lmax, rmax); } inline int sttree_get_maximum(int begin, int end) { //printf("QUERY %d %d\n", begin, end); if (begin >= end) return 0; return sttree_get_maximum_inner(0, TREE_N, 1, begin, end); } void populateWithRects(std::vector<rect> & v, int n) { v.reserve(n); int x1, y1, x2, y2; for (int i = 0; i < n; i++) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); v.push_back(rect(i, x1, y1, x2, y2)); } } void populateWithInts(std::vector<int> & v, int n) { v.reserve(n); for (int i = 0; i < n; i++) v.push_back(i); } void setupTree(const std::vector<rect> & rects, const std::vector<int> & ordering, int n) { sttree_clear(); for (int i = 0; i < n; i++) sttree_set(i, rects[ordering[i]].height); } bool sortRects(const std::vector<rect> & rects, const std::vector<int> & ordering, int n, int w) { for (int i : ordering) { const rect & r = rects[i]; if (sttree_get_maximum(0, i) > w - r.height) return false; sttree_set(i, 0); } return true; } bool solve() { int n, w; scanf("%d %d", &n, &w); std::vector<rect> baseRects, destRects; populateWithRects(baseRects, n); populateWithRects(destRects, n); std::vector<int> baseOrdering, destOrdering; populateWithInts(baseOrdering, n); populateWithInts(destOrdering, n); std::sort(baseOrdering.begin(), baseOrdering.end(), CompareRects(baseRects)); std::sort(destOrdering.begin(), destOrdering.end(), CompareRects(destRects)); setupTree(baseRects, baseOrdering, n); return sortRects(destRects, destOrdering, n, w); } int main() { int t; scanf("%d", &t); while (t --> 0) puts(solve() ? "TAK" : "NIE"); return 0; } |