//: Piotr Skotnicki // Potyczki Algorytmiczne 2014 // PAR #include <algorithm> #include <cstdio> #include <cstdlib> #include <vector> #include <iterator> using namespace std; const int INF = 2000000000; constexpr int N = 50001; struct max_tree { const int s; vector<int> el; explicit max_tree(int size) : s(size) , el(2 * size, 0) { } void set(int p, int v) { for (p += s, el[p] = v, p >>= 1; p > 0; p >>= 1) { el[p] = max(el[p << 1], el[(p << 1) + 1]); } } int query(int p, int k) const { int m = -INF; p += s; k += s; while (p < k) { if (p & 1) { m = max(m, el[p++]); } if (!(k & 1)) { m = max(m, el[k--]); } p >>= 1; k >>= 1; } if (p == k) { m = max(m, el[p]); } return m; } int top() const { return el[1]; } }; int m[N], wys[N], sx[N], dx[N], s[N], d[N]; int main() { int t, n, w, x1, y1, x2, y2; scanf("%d", &t); while (t--) { scanf("%d %d", &n, &w); max_tree tree(n + 1); for (int i = 0; i < n; ++i) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); sx[i] = min(x1, x2); wys[i] = abs(y2 - y1); s[i] = i; } for (int i = 0; i < n; ++i) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); dx[i] = min(x1, x2); d[i] = i; } stable_sort(begin(s), next(begin(s), n), [](int a, int b) -> bool { return sx[a] < sx[b]; }); stable_sort(begin(d), next(begin(d), n), [](int a, int b) -> bool { return dx[a] < dx[b]; }); for (int i = 0; i < n; ++i) { const int u = s[i]; m[u] = i; tree.set(i, wys[u]); } bool ok = true; for (int i = 0; i < n; ++i) { const int u = d[i]; const int pos = m[u]; int mx = 0; if (pos != 0) { mx = tree.query(0, pos - 1); } if (w - mx >= wys[u]) { tree.set(pos, 0); } else { ok = false; break; } } printf("%s\n", ok ? "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 | //: Piotr Skotnicki // Potyczki Algorytmiczne 2014 // PAR #include <algorithm> #include <cstdio> #include <cstdlib> #include <vector> #include <iterator> using namespace std; const int INF = 2000000000; constexpr int N = 50001; struct max_tree { const int s; vector<int> el; explicit max_tree(int size) : s(size) , el(2 * size, 0) { } void set(int p, int v) { for (p += s, el[p] = v, p >>= 1; p > 0; p >>= 1) { el[p] = max(el[p << 1], el[(p << 1) + 1]); } } int query(int p, int k) const { int m = -INF; p += s; k += s; while (p < k) { if (p & 1) { m = max(m, el[p++]); } if (!(k & 1)) { m = max(m, el[k--]); } p >>= 1; k >>= 1; } if (p == k) { m = max(m, el[p]); } return m; } int top() const { return el[1]; } }; int m[N], wys[N], sx[N], dx[N], s[N], d[N]; int main() { int t, n, w, x1, y1, x2, y2; scanf("%d", &t); while (t--) { scanf("%d %d", &n, &w); max_tree tree(n + 1); for (int i = 0; i < n; ++i) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); sx[i] = min(x1, x2); wys[i] = abs(y2 - y1); s[i] = i; } for (int i = 0; i < n; ++i) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); dx[i] = min(x1, x2); d[i] = i; } stable_sort(begin(s), next(begin(s), n), [](int a, int b) -> bool { return sx[a] < sx[b]; }); stable_sort(begin(d), next(begin(d), n), [](int a, int b) -> bool { return dx[a] < dx[b]; }); for (int i = 0; i < n; ++i) { const int u = s[i]; m[u] = i; tree.set(i, wys[u]); } bool ok = true; for (int i = 0; i < n; ++i) { const int u = d[i]; const int pos = m[u]; int mx = 0; if (pos != 0) { mx = tree.query(0, pos - 1); } if (w - mx >= wys[u]) { tree.set(pos, 0); } else { ok = false; break; } } printf("%s\n", ok ? "TAK" : "NIE"); } return 0; } |