#include <bits/stdc++.h> using namespace std; const int N = 100005; int t, n; int ind[N]; int length[N]; pair<int, int> points[N]; void printYes() { printf("TAK "); for (int i = 1; i <= n; i++) { printf("%d ", length[i]); } printf("\n"); } pair<long long, long long> trySetup(int height) { set<pair<int, int> > openSquares; priority_queue<pair<long long, pair<int, int> > > toRemove; long long area = 0; long long width = 0; for (int i = 1; i <= n; i++) { int x = points[i].first; int y = points[i].second; if (y >= height) { return {-1, -1}; } while (!toRemove.empty() && -toRemove.top().first <= x) { auto it = openSquares.find(toRemove.top().second); openSquares.erase(it); toRemove.pop(); } auto firstUpper = openSquares.lower_bound({y + 1, 0}); if (firstUpper != openSquares.begin()) { auto firstLower = firstUpper; firstLower--; if (firstLower->second > y) { return {-1, -1}; } } int to = height; if (firstUpper != openSquares.end()) { to = firstUpper->first; } area += (long long)(to - y) * (to - y); length[ind[i]] = to - y; width = max(width, (long long)x + to - y); toRemove.push({-((long long)x + to - y), {y, to}}); openSquares.insert({y, to}); } return {area, width}; } bool goodNormal(pair<long long, long long> result, int height, int minY) { if (result.first == -1) { return false; } long long x = result.second - points[1].first; long long y = height - minY; return x * y == result.first; } bool goodLast(pair<long long, long long> result, long long height, int minY) { if (result.first == -1) { return false; } int maxY = points[1].second; result.first -= (long long)(height - maxY) * (height - maxY); height += result.second - points[1].first - (height - maxY); result.first += (long long)(height - maxY) * (height - maxY); long long x = result.second - points[1].first; long long y = height - minY; length[ind[1]] = height - points[1].second; return x * y == result.first; } bool cmp(int a, int b) { return points[a] < points[b]; } void test() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &points[i].first, &points[i].second); points[i].second *= -1; ind[i] = i; } sort(ind + 1, ind + 1 + n, cmp); sort(points + 1, points + 1 + n); for (int i = 1; i <= n; i++) { points[i].second *= -1; } int minY = points[1].second; for (int i = 1; i <= n; i++) { minY = min(minY, points[i].second); } int maxY = points[1].second; for (int i = 2; i <= n; i++) { if (points[i].first == points[i - 1].first) { continue; } int height = maxY + points[i].first - points[1].first; pair<long long, long long> setup = trySetup(height); if (goodNormal(setup, height, minY)) { printYes(); return; } if (points[i].second > maxY) { break; } } int height = maxY + points[n].first - points[1].first + 1; pair<long long, long long> setup = trySetup(height); if (goodLast(setup, height, minY)) { printYes(); } else { printf("NIE\n"); } } int main() { scanf("%d", &t); while (t--) { test(); } 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 | #include <bits/stdc++.h> using namespace std; const int N = 100005; int t, n; int ind[N]; int length[N]; pair<int, int> points[N]; void printYes() { printf("TAK "); for (int i = 1; i <= n; i++) { printf("%d ", length[i]); } printf("\n"); } pair<long long, long long> trySetup(int height) { set<pair<int, int> > openSquares; priority_queue<pair<long long, pair<int, int> > > toRemove; long long area = 0; long long width = 0; for (int i = 1; i <= n; i++) { int x = points[i].first; int y = points[i].second; if (y >= height) { return {-1, -1}; } while (!toRemove.empty() && -toRemove.top().first <= x) { auto it = openSquares.find(toRemove.top().second); openSquares.erase(it); toRemove.pop(); } auto firstUpper = openSquares.lower_bound({y + 1, 0}); if (firstUpper != openSquares.begin()) { auto firstLower = firstUpper; firstLower--; if (firstLower->second > y) { return {-1, -1}; } } int to = height; if (firstUpper != openSquares.end()) { to = firstUpper->first; } area += (long long)(to - y) * (to - y); length[ind[i]] = to - y; width = max(width, (long long)x + to - y); toRemove.push({-((long long)x + to - y), {y, to}}); openSquares.insert({y, to}); } return {area, width}; } bool goodNormal(pair<long long, long long> result, int height, int minY) { if (result.first == -1) { return false; } long long x = result.second - points[1].first; long long y = height - minY; return x * y == result.first; } bool goodLast(pair<long long, long long> result, long long height, int minY) { if (result.first == -1) { return false; } int maxY = points[1].second; result.first -= (long long)(height - maxY) * (height - maxY); height += result.second - points[1].first - (height - maxY); result.first += (long long)(height - maxY) * (height - maxY); long long x = result.second - points[1].first; long long y = height - minY; length[ind[1]] = height - points[1].second; return x * y == result.first; } bool cmp(int a, int b) { return points[a] < points[b]; } void test() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &points[i].first, &points[i].second); points[i].second *= -1; ind[i] = i; } sort(ind + 1, ind + 1 + n, cmp); sort(points + 1, points + 1 + n); for (int i = 1; i <= n; i++) { points[i].second *= -1; } int minY = points[1].second; for (int i = 1; i <= n; i++) { minY = min(minY, points[i].second); } int maxY = points[1].second; for (int i = 2; i <= n; i++) { if (points[i].first == points[i - 1].first) { continue; } int height = maxY + points[i].first - points[1].first; pair<long long, long long> setup = trySetup(height); if (goodNormal(setup, height, minY)) { printYes(); return; } if (points[i].second > maxY) { break; } } int height = maxY + points[n].first - points[1].first + 1; pair<long long, long long> setup = trySetup(height); if (goodLast(setup, height, minY)) { printYes(); } else { printf("NIE\n"); } } int main() { scanf("%d", &t); while (t--) { test(); } return 0; } |