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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <set>

using namespace std;

#if 0
#define ASSERT(x) assert(x)
#define DEBUG(fmt,...) printf("[%04d] " fmt, __LINE__, ##__VA_ARGS__);
#else
#define ASSERT(x) {}
#define DEBUG(fmt,...) {}
#endif

using ULL = unsigned long long;
constexpr int INTMAX = 2e9;

struct SquareData
{
	int x, y, a, max_a;
};


struct Square {

	Square(SquareData *ptr):data(ptr)
	{
		data->max_a = INTMAX;
	}

	bool operator <(const Square& rhs) const
	{
		//printf("Square compare {%d,%d} < {%d,%d} : %d\n", data->x, data->y, rhs.data->x, rhs.data->y,
		//		int((data->y != rhs.data->y) ? (data->y < rhs.data->y ) : (data->x < rhs.data->x)));
		return (data->y != rhs.data->y) ? (data->y < rhs.data->y ) : (data->x < rhs.data->x);
	}

	bool operator ==(const Square& rhs) const
	{
		return (data == rhs.data);
	}

	bool operator !=(const Square& rhs) const
	{
		return (data != rhs.data);
	}

	SquareData *data;
};

SquareData squareData[2000];

int t, n, x_min, x_max, y_min, y_max;
std::set<Square> squares;

inline bool checkFirst()
{
	const auto &f = squares.begin();
	return ((f->data->x == x_min) and (f->data->y == y_min));
}

void findBoundaries()
{
	auto it_rend = squares.rend();
	--it_rend;
	auto it2_rend = squares.rend();
	for(auto it=squares.rbegin(); it!=it_rend; ++it)
	{
		const auto x = it->data->x;
		const auto y = it->data->y;
		DEBUG("  punkt: x = %d y = %d\n", x, y);
		auto it2 = it;
		for(++it2; it2!=it2_rend; ++it2)
		{
			if((it2->data->x <= x) and (it2->data->y <= y))
			{
				it2->data->max_a = min(it2->data->max_a, max(x-it2->data->x, y-it2->data->y));
				DEBUG("  ogranicza: x = %d y = %d (max_a = %d)\n", it2->data->x, it2->data->y, it2->data->max_a);
			}
		}
	}
}

set<Square>::iterator newBoundary(Square* s)
{
	set<Square>::iterator first = squares.end();
	bool search = true;

	if(not ((s->data->a > 0) and (s->data->a < INTMAX)))
	{
		DEBUG("  a = %d\n", s->data->a);
		ASSERT(false);
	}
	const auto x1 = s->data->x;
	const auto y1 = s->data->y + s->data->a;
	const auto x2 = s->data->x + s->data->a;
	const auto y2 = s->data->y;
	x_max = max(x_max, x2);
	y_max = max(y_max, y1);
	auto it_end = squares.end();
	for(auto it=squares.begin(); it!=it_end; ++it)
	{
		//if(s == &(*it)) { continue; /* skip self*/ }
		if((it->data->y < y2) and (it->data->x < x2) and (it->data->x > x1))
		{
			const auto prev = it->data->max_a;
			it->data->max_a = min(it->data->max_a, y2-it->data->y);
			if(search and (prev != it->data->max_a))
			{
				search = false;
				first = it;
			}
			DEBUG("    newBund ogranicza: x = %d y = %d (max_a = %d)\n", it->data->x, it->data->y, it->data->max_a);
		}
		if((it->data->x < x1) and (it->data->y < y1) and (it->data->y > y2))
		{
			const auto prev = it->data->max_a;
			it->data->max_a = min(it->data->max_a, x1-it->data->x);
			if(search and (prev != it->data->max_a))
			{
				search = false;
				first = it;
			}
			DEBUG("    newBund ogranicza: x = %d y = %d (max_a = %d)\n", it->data->x, it->data->y, it->data->max_a);
		}
		if(it->data->y >= y1)
		{
			break;
		}
	}
	return first;
}

bool assertBoundary(const Square* s)
{
	if(not ((s->data->a > 0) and (s->data->a < INTMAX)))
	{
		DEBUG("  a = %d\n", s->data->a);
		ASSERT(false);
	}
	const auto x1 = s->data->x;
	const auto y1 = s->data->y + s->data->a;
	const auto x2 = s->data->x + s->data->a;
	const auto y2 = s->data->y;
	auto it_end = squares.end();
	for(auto it=squares.begin(); it!=it_end; ++it)
	{
		if((it->data->y < y2) and (it->data->x < x2) and (it->data->x > x1))
		{
			if(it->data->max_a > (y2-it->data->y))
			{
				DEBUG("assertBoundary = false\n");
				return false;
			}
		}
		if((it->data->x < x1) and (it->data->y < y1) and (it->data->y > y2))
		{
			if(it->data->max_a > (x1-it->data->x))
			{
				DEBUG("assertBoundary = false\n");
				return false;
			}
		}
	}
	return true;
}

int solveAny()
{
	int solved = 0;

	auto it_end = squares.end();
	for(set<Square>::iterator it=squares.begin(); it!=it_end; )
	{
		if(INTMAX != it->data->max_a)
		{
			DEBUG(" SOLVE {%d,%d} a = %d\n", it->data->x, it->data->y, it->data->max_a);
			it->data->a = it->data->max_a;
			auto startIt = newBoundary(&const_cast<Square&>(*it));
			bool wasFirst = (it == squares.begin());
			//decltype(it) it_prev;
			//if (not wasFirst)
			//{
			//	it_prev = it;
			//	--it_prev;
			//}
			squares.erase(it);
			//it = (wasFirst) ? squares.begin() : it_prev;
			it = (wasFirst) ? squares.begin() : startIt;
			++solved;
		} else
		{
			++it;
		}
	}

	DEBUG("solved = %d\n", solved);
	return solved;
}

bool checkSolution()
{
	ULL total = (x_max - x_min) * (y_max - y_min);
	DEBUG("CALCULATE total = %llu   n = %d\n", total, n);
	for(int i=0; i<n; ++i)
	{
		const ULL a = squareData[i].a;
		const ULL field = a*a;
		if(total >= field)
		{
			DEBUG("total = %llu   field %llu\n", total, field);
			total -= field;
		}
		else
		{
			return false;
		}
	}

	return (0 == total);
}

bool solve()
{
	if(checkFirst())
	{
		findBoundaries();
		while(solveAny())
		{
			if(squares.size() <= 2)
			{
				break;
			}
		}
		const auto left = squares.size();
		if(left == 0)
		{
			DEBUG("left == 0\n");
		}
		else if(left == 1)
		{
			DEBUG("left == 1\n");
			squares.begin()->data->a = min(squares.begin()->data->max_a,
					max(x_max-squares.begin()->data->x, y_max-squares.begin()->data->y));

			if(not assertBoundary(&(*squares.begin())))
			{
				return false;
			}
		}
		else if(left == 2)
		{
			DEBUG("left == 2  (x_max %d y_max %d)\n", x_max, y_max);

			set<Square>::iterator first = squares.begin();
			set<Square>::iterator second = first;
			++second;

			DEBUG("\n\n---------\n");
			DEBUG("     first {%d,%d} {%d,%d}\n", first->data->x, first->data->y, second->data->x, second->data->y);

			first->data->a = (second->data->y - first->data->y);
			DEBUG("  first->data->a %d\n", first->data->a);
			if((first->data->x + first->data->a) >= x_max)
			{
				second->data->a = (first->data->x + first->data->a) - second->data->x;
				DEBUG("  second->data->a %d\n", second->data->a);
				if((second->data->y + second->data->a) >= y_max)
				{
					auto x_max_old = x_max;
					auto y_max_old = y_max;
					x_max = max(x_max, max(first->data->x+first->data->a, second->data->x+second->data->a));
					y_max = max(y_max, max(first->data->y+first->data->a, second->data->y+second->data->a));
					if(checkSolution())
					{
						return true;
					}
					x_max = x_max_old;
					y_max = y_max_old;
				}
			}

			second->data->a = (first->data->x - second->data->x);
			if((second->data->y + second->data->a) >= y_max)
			{
				first->data->a = (second->data->y + second->data->a) - first->data->y;
				if((first->data->x + second->data->a) >= x_max)
				{
					x_max = max(x_max, max(first->data->x+first->data->a, second->data->x+second->data->a));
					y_max = max(y_max, max(first->data->y+first->data->a, second->data->y+second->data->a));
					return checkSolution();
				}
			}
		}
		else
		{
			return false; // more than 2 - cannot solve probably
		}

		return checkSolution();
	}
	else
	{
		DEBUG("checkFirst() says NO\n");
	}
	return false;
}

int main()
{
	scanf("%d", &t);
	for(int i=0; i<t; ++i)
	{
		memset(&squareData, 0, sizeof(squareData));
		squares.clear();
		x_min = INTMAX;
		x_max = 0;
		y_min = INTMAX;
		y_max = 0;

		int x, y;

		scanf("%d", &n);
		for(int j=0; j<n; ++j)
		{
			scanf("%d %d", &x, &y);
			squareData[j].x = x;
			squareData[j].y = y;

			x_min = min(x_min, x);
			x_max = max(x_max, x);
			y_min = min(y_min, y);
			y_max = max(y_max, y);

			squares.emplace(&(squareData[j]));
			DEBUG("j = %d square.size() = %lu\n", j, squares.size());
		}

		++x_max;
		++y_max;

		if(solve())
		{
			printf("TAK");
			for(int j=0; j<n; ++j)
			{
				ASSERT((squareData[j].a > 0) and (squareData[j].a < INTMAX));
				printf(" %d", squareData[j].a);
			}
			printf("\n");
		}
		else
		{
			printf("NIE\n");
		}
	}
	return 0;
}