#include <bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> ostream& operator<<(ostream& os, pair<T, U> p) { return os << "(" << p.first << ", " << p.second << ")"; } template<template<class, class...> class C, class... A> typename enable_if<!is_same<C<A...>, string>::value, ostream&>::type operator<<(ostream& os, C<A...> c) { auto i = c.begin(); while (i != c.end()) { os << " {"[i == c.begin()] << *i; os << ",}"[++i == c.end()]; } return os; } #define debug(x) { cerr << #x << " = " << x << endl; } #else #define debug(...) {} #endif void make_unique(vector<int>& v) { sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); } int szukaj(vector<int>& v, int x) { for (size_t i = 0; i < v.size(); i ++) { if (v[i] == x) return i; } assert(false); return v.size(); } vector<uint32_t> solution; vector<pair<bool, bool>> drz; void wstaw(int v, int p, int k, int a, int b, bool co) { if (k <= a || b <= p) return; if (a <= p && k <= b) { drz[v] = make_pair(co, co); return; } int s = (p + k) / 2; if (drz[v].first) { drz[2 * v] = drz[v]; drz[2 * v + 1] = drz[v]; } wstaw(2 * v, p, s, a, b, co); wstaw(2 * v + 1, s, k, a, b, co); drz[v].first = drz[2 * v].first && drz[2 * v + 1].first; drz[v].second = drz[2 * v].second || drz[2 * v + 1].second; } int szukaj(int v, int p, int k, int x) { if (k <= x || drz[v].second == 0) return -1; if (drz[v].first) return p; int s = (p + k) / 2; int w = szukaj(2 * v, p, s, x); if (w != -1) return w; return szukaj(2 * v + 1, s, k, x); } const int inf = 1<<30; bool check(uint32_t d, vector<int> ys, vector<pair<int,int>> points) { debug("check"); debug(d); debug(points); solution = vector<uint32_t>(points.size(), 1); size_t n2 = 1; while (n2 <= (ys.size() + 1)) n2 *= 2; drz = vector<pair<bool, bool>>(2 * n2, {false, false}); priority_queue<pair<int,pair<int, int>>> zda; for (size_t i = 0; i < points.size(); i ++) { zda.push({-points[i].first, {-inf + points[i].second - 1, i}}); // debug(make_pair(-points[i].first, make_pair(-(int)i - 1, 0))); // debug(zda.top()); } wstaw(1, 0, n2, ys.size(), n2, true); int last = zda.top().first; while(!zda.empty()) { debug(zda.top()); debug(drz); if (zda.top().first != last) if (not drz[1].first) return false; last = zda.top().first; pair<int,int> z = zda.top().second; zda.pop(); // debug(zda.top()); debug(z); if (z.first < 0) { int id = z.second; if ((uint32_t)ys[points[id].second] >= d) return false; debug(points[id]); debug(id); int sz = szukaj(1, 0, n2, points[id].second); solution[id] = (sz == (int)ys.size() ? d : ys[sz]); if ((uint32_t)ys[points[id].second] >= solution[id]) return false; solution[id] -= ys[points[id].second]; int bok = sz - points[id].second; debug(sz); debug(solution[id]); debug(bok); if (bok <= 0) return false; wstaw(1, 0, n2, points[id].second, points[id].second + bok, true); zda.push({- points[id].first - solution[id], {points[id].second, points[id].second + bok}}); // debug(zda.top()); } else { debug("wstaw"); debug(z); wstaw(1, 0, n2, z.first, z.second, false); } } return true; } vector<uint32_t> test() { int n; scanf("%d", &n); vector<int> xs; vector<int> ys; vector<pair<int,int>> points; for (int i = 0; i < n; i ++) { int x, y; scanf("%d%d", &x, &y); xs.push_back(x); ys.push_back(y); points.emplace_back(x, y); } if (n == 1) return vector<uint32_t>(1, 1); make_unique(xs); make_unique(ys); for (auto &p : points) { p.first -= xs[0]; p.second = szukaj(ys, p.second); } int xx = xs[0]; for (auto& x : xs) { x -= xx; } //int n = xs.size(); //int m = ys.size(); //vector<vector<int>> moz = vector<vector<int>>(n + 1, vector<bool>(m + 1, 0)); /* sort(points.begin(), points.end(), [](pair<int,int> a, pair<int,int> b) { return make_pair(a.first, - a.second) < make_pair(b.first, -b.second);}); */ auto pierwszy = *min_element(points.begin(), points.end(), [](pair<int,int> a, pair<int,int> b) { return make_pair(a.first, - a.second) < make_pair(b.first, -b.second);}); for (size_t i = 1; i < xs.size(); i ++) { if (check(ys[pierwszy.second] + xs[i], ys, points)) { debug("hurra"); return solution; } } auto mem = *min_element(points.begin(), points.end(), [](pair<int,int> a, pair<int,int>b) { return make_pair(a.second, -a.first) < make_pair(b.second, -b.first);}); for (size_t i = 1; i < ys.size(); i ++) { if (check(ys[pierwszy.second] + (ys[i] - ys[0]) + (uint32_t)mem.first, ys, points)) { debug("hurra"); return solution; } } return vector<uint32_t>(); } int main() { int t; scanf("%d", &t); while(t--) { auto v = test(); if (v.empty()) { printf("NIE"); } else { printf("TAK "); for (int x : v) { printf("%d ", x); } } printf("\n"); } }
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | #include <bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> ostream& operator<<(ostream& os, pair<T, U> p) { return os << "(" << p.first << ", " << p.second << ")"; } template<template<class, class...> class C, class... A> typename enable_if<!is_same<C<A...>, string>::value, ostream&>::type operator<<(ostream& os, C<A...> c) { auto i = c.begin(); while (i != c.end()) { os << " {"[i == c.begin()] << *i; os << ",}"[++i == c.end()]; } return os; } #define debug(x) { cerr << #x << " = " << x << endl; } #else #define debug(...) {} #endif void make_unique(vector<int>& v) { sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); } int szukaj(vector<int>& v, int x) { for (size_t i = 0; i < v.size(); i ++) { if (v[i] == x) return i; } assert(false); return v.size(); } vector<uint32_t> solution; vector<pair<bool, bool>> drz; void wstaw(int v, int p, int k, int a, int b, bool co) { if (k <= a || b <= p) return; if (a <= p && k <= b) { drz[v] = make_pair(co, co); return; } int s = (p + k) / 2; if (drz[v].first) { drz[2 * v] = drz[v]; drz[2 * v + 1] = drz[v]; } wstaw(2 * v, p, s, a, b, co); wstaw(2 * v + 1, s, k, a, b, co); drz[v].first = drz[2 * v].first && drz[2 * v + 1].first; drz[v].second = drz[2 * v].second || drz[2 * v + 1].second; } int szukaj(int v, int p, int k, int x) { if (k <= x || drz[v].second == 0) return -1; if (drz[v].first) return p; int s = (p + k) / 2; int w = szukaj(2 * v, p, s, x); if (w != -1) return w; return szukaj(2 * v + 1, s, k, x); } const int inf = 1<<30; bool check(uint32_t d, vector<int> ys, vector<pair<int,int>> points) { debug("check"); debug(d); debug(points); solution = vector<uint32_t>(points.size(), 1); size_t n2 = 1; while (n2 <= (ys.size() + 1)) n2 *= 2; drz = vector<pair<bool, bool>>(2 * n2, {false, false}); priority_queue<pair<int,pair<int, int>>> zda; for (size_t i = 0; i < points.size(); i ++) { zda.push({-points[i].first, {-inf + points[i].second - 1, i}}); // debug(make_pair(-points[i].first, make_pair(-(int)i - 1, 0))); // debug(zda.top()); } wstaw(1, 0, n2, ys.size(), n2, true); int last = zda.top().first; while(!zda.empty()) { debug(zda.top()); debug(drz); if (zda.top().first != last) if (not drz[1].first) return false; last = zda.top().first; pair<int,int> z = zda.top().second; zda.pop(); // debug(zda.top()); debug(z); if (z.first < 0) { int id = z.second; if ((uint32_t)ys[points[id].second] >= d) return false; debug(points[id]); debug(id); int sz = szukaj(1, 0, n2, points[id].second); solution[id] = (sz == (int)ys.size() ? d : ys[sz]); if ((uint32_t)ys[points[id].second] >= solution[id]) return false; solution[id] -= ys[points[id].second]; int bok = sz - points[id].second; debug(sz); debug(solution[id]); debug(bok); if (bok <= 0) return false; wstaw(1, 0, n2, points[id].second, points[id].second + bok, true); zda.push({- points[id].first - solution[id], {points[id].second, points[id].second + bok}}); // debug(zda.top()); } else { debug("wstaw"); debug(z); wstaw(1, 0, n2, z.first, z.second, false); } } return true; } vector<uint32_t> test() { int n; scanf("%d", &n); vector<int> xs; vector<int> ys; vector<pair<int,int>> points; for (int i = 0; i < n; i ++) { int x, y; scanf("%d%d", &x, &y); xs.push_back(x); ys.push_back(y); points.emplace_back(x, y); } if (n == 1) return vector<uint32_t>(1, 1); make_unique(xs); make_unique(ys); for (auto &p : points) { p.first -= xs[0]; p.second = szukaj(ys, p.second); } int xx = xs[0]; for (auto& x : xs) { x -= xx; } //int n = xs.size(); //int m = ys.size(); //vector<vector<int>> moz = vector<vector<int>>(n + 1, vector<bool>(m + 1, 0)); /* sort(points.begin(), points.end(), [](pair<int,int> a, pair<int,int> b) { return make_pair(a.first, - a.second) < make_pair(b.first, -b.second);}); */ auto pierwszy = *min_element(points.begin(), points.end(), [](pair<int,int> a, pair<int,int> b) { return make_pair(a.first, - a.second) < make_pair(b.first, -b.second);}); for (size_t i = 1; i < xs.size(); i ++) { if (check(ys[pierwszy.second] + xs[i], ys, points)) { debug("hurra"); return solution; } } auto mem = *min_element(points.begin(), points.end(), [](pair<int,int> a, pair<int,int>b) { return make_pair(a.second, -a.first) < make_pair(b.second, -b.first);}); for (size_t i = 1; i < ys.size(); i ++) { if (check(ys[pierwszy.second] + (ys[i] - ys[0]) + (uint32_t)mem.first, ys, points)) { debug("hurra"); return solution; } } return vector<uint32_t>(); } int main() { int t; scanf("%d", &t); while(t--) { auto v = test(); if (v.empty()) { printf("NIE"); } else { printf("TAK "); for (int x : v) { printf("%d ", x); } } printf("\n"); } } |