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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cassert>

typedef unsigned long long ull;
using namespace std;
const int MAX_N = 2000;

struct point
{
	int id;
	ull x, y;
	mutable ull edge = 0;
	point(int _id, ull _x, ull _y) : id(_id), x(_x), y(_y) {}
	inline ull distance_to(const point& p) const
	{
		return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y);
	}
};

struct line
{
	point start, end;
};

struct horizontal_compare 
{
	bool operator() (const line& lhs, const line& rhs) const
	{
		return lhs.start.y == rhs.start.y ? lhs.start.x < rhs.start.x : lhs.start.y < rhs.start.y;
	}
};

struct vertical_compare
{
	bool operator() (const line& lhs, const line& rhs) const
	{
		return lhs.start.x == rhs.start.x ? lhs.start.y < rhs.start.y : lhs.start.x < rhs.start.x;
	}
};

std::vector<point> points;
std::vector<point> skipped;
ull result_place[MAX_N];
int n;

bool solve()
{
	if (points.size() == 1)
	{
		cout << "TAK 1" << endl;
		return true;
	}

	// start with most bottom-left point
	std::set<line, horizontal_compare> horizontal;
	std::set<line, vertical_compare> vertical;
	ull total_area = 0, top = 0, right = 0;
	ull bottom = points.front().y, left = points.front().x;

	size_t k = 0;
	for (auto& p : points)
	{
		ull closest_dist = -1;
		point closest_point(-1, -1, -1);
		for(size_t l = k + 1; l < points.size(); ++l)
		{
			const auto& w = points[l];
			if (w.x >= p.x && w.y >= p.y)
			{
				ull d = p.distance_to(w);
				if (closest_dist > d)
				{
					closest_point = w;
					closest_dist = d;

				}
			}
		}

		auto it = horizontal.upper_bound(line{point(-1,p.x, p.y),point(-1,p.x,p.y)});
		for(; it != horizontal.end(); ++it)
		{
			if (it->start.y > p.y && it->start.x < p.x && it->end.x > p.x)
			{
				ull d = it->start.y - p.y;
				d *= d;
				if (d < closest_dist)
				{
					closest_dist = d;
					closest_point = point(0, p.x, it->start.y);
					break;
				}
			}
		}

		it = vertical.upper_bound(line{ point(-1,p.x, p.y),point(-1,p.x,p.y) });
		for (; it != vertical.end(); ++it)
		{
			if (it->start.x > p.x && it->start.y < p.y && it->end.y > p.y)
			{
				ull d = it->start.x - p.x;
				d *= d;
				if (d < closest_dist)
				{
					closest_dist = d;
					closest_point = point(0, it->start.x, p.y);
					break;
				}
			}
		}

		if (closest_point.id != -1)
		{
			p.edge = std::max(closest_point.x - p.x, closest_point.y - p.y);
			total_area += p.edge*p.edge;
			right = std::max(right, p.x + p.edge);
			top = std::max(top, p.y + p.edge);
			result_place[p.id] = p.edge;

			auto l = line{ point{ 0, p.x, p.y }, point{ 0, p.x, p.y + p.edge } };
			auto it = vertical.find(l);
			if(it != vertical.end())
			{
				if (l.end.y - l.start.y > it->end.y - it->start.y)
				{
					vertical.erase(it);
					vertical.insert(l);
				}
			}
			else
			{
				vertical.insert(l);
			}

			l = line{ point{ 0, p.x + p.edge, p.y }, point{ 0, p.x + p.edge, p.y + p.edge } };
			it = vertical.find(l);
			if (it != vertical.end())
			{
				if (l.end.y - l.start.y > it->end.y - it->start.y)
				{
					vertical.erase(it);
					vertical.insert(l);
				}
			}
			else
			{
				vertical.insert(l);
			}

			l = line{ point{ 0, p.x, p.y }, point{ 0, p.x + p.edge, p.y } };
			it = horizontal.find(l);
			if (it != horizontal.end())
			{
				if (l.end.x - l.start.x > it->end.x - it->start.x)
				{
					horizontal.erase(it);
					horizontal.insert(l);
				}
			}
			else
			{
				horizontal.insert(l);
			}

			l = line{ point{ 0, p.x, p.y + p.edge }, point{ 0, p.x + p.edge, p.y + p.edge } };
			it = horizontal.find(l);
			if (it != horizontal.end())
			{
				if (l.end.x - l.start.x > it->end.x - it->start.x)
				{
					horizontal.erase(it);
					horizontal.insert(l);
				}
			}
			else
			{
				horizontal.insert(l);
			}
		}
		else
		{
			skipped.push_back(p);
		}

		++k;
	}

	if (skipped.size() == points.size())
	{
		return false;
	}

	ull old_right = right;
	ull old_top = top;

	for (auto& p : skipped)
	{
		// fill top-right
		if (p.y < top && p.x < right)
		{
			p.edge = std::min(old_top - p.y, old_right - p.x);
		}

		// fill bottom-right
		else if (p.y == bottom)
		{
			p.edge = right - p.x;
			// fill, not fit
			if (p.edge == 0)
			{
				p.edge = top - bottom;
			}
			right += p.edge;
		}

		// fill top-left
		else if (p.x == left)
		{
			p.edge = top - p.y;
			// fill, not fit
			if (p.edge == 0)
			{
				p.edge = right - left;
			}
			top += p.edge;
		}

		if (p.edge == 0)
		{
			return false;
		}

		total_area += p.edge*p.edge;
		result_place[p.id] = p.edge;
	}

	if (total_area != (top - bottom) * (right - left))
	{
		return false;
	}

	std::cout << "TAK";
	for (int i = 0; i < n; ++i)
	{
		std::cout << ' ' << result_place[i];
	}
	std::cout << std::endl;
	return true;
}

void clear_state()
{
	skipped.clear();
	for(auto& x : points)
	{
		x.edge = 0;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	int t;
	ull p1, p2;
	points.reserve(MAX_N);
	skipped.reserve(MAX_N);

	cin >> t;
	while (t--)
	{
		points.clear();
		skipped.clear();
		cin >> n;
		for (int i = 0; i < n; ++i)
		{
			cin >> p1 >> p2;
			points.push_back(point(i, p1, p2));
		}

		std::sort(points.begin(), points.end(), [](const point& a, const point& b) { return a.x*a.x + a.y*a.y < b.x*b.x + b.y*b.y; });
		bool ok = solve();
		if (ok) continue;

		std::cout << "NIE" << std::endl;
	}
	return 0;
}