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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
pair<pair<int, int>,int> px[2000],py[2000];
pair<int, int> p[2000];
int w[2000],d[2000];
bool wypisz;
const int niesk = 2000000001;
int binarnieY(int l, int p,int szuk);
int binarnieX(int l, int p,int szuk);
int main()
{
int n,i,j,t,minx,miny,poprzx =0,poprzy=0,punkt,maksy=0,maksx=0;

scanf("%d",&t);
while(t--){
	scanf("%d",&n);
	for(i = 0; i < n; ++i){
	scanf("%d%d",&p[i].first,&p[i].second);
	px[i].first.first = p[i].first;
	px[i].first.second = p[i].second;
	px[i].second = i;
	py[i].first.first = p[i].second;
	py[i].first.second = p[i].first;
	py[i].second = i;
	w[i] = niesk;
	}
	wypisz = true;
	sort(px,px+n);
	sort(py,py+n);
	minx = px[0].first.first;
	miny = py[0].first.first;
	if(px[0].second != py[0].second){
		printf("NIE\n");
	}
	else {
		vector<int> wspx,wspy;
		wspx.push_back(0);
		wspy.push_back(0);
		for(i=1;i<n;++i){
			if(px[i].first.first == px[i-1].first.first) {w[px[i-1].second] = min(w[px[i-1].second],px[i].first.second - px[i-1].first.second);d[px[i-1].second] = 1;}
			else {
				if(px[i].first.second < miny){
					printf("NIE\n"); wypisz= false;break;
				}
				wspx.push_back(i);
			}
			if(py[i].first.first == py[i-1].first.first) {w[py[i-1].second] = min(w[py[i-1].second],py[i].first.second - py[i-1].first.second);d[px[i-1].second] = 1;}
			else {
				if(py[i].first.second < minx){
					printf("NIE\n");wypisz= false;break;
				}
				wspy.push_back(i);
			}
		}
	if(wypisz == false) continue;	
	wspx.push_back(n);	
	wspy.push_back(n);
	//cout << "wspx ";for(i=0;i < wspx.size();++i) cout<<wspx[i] <<" ";cout << endl;
	//cout << "wspy ";for(i=0;i < wspy.size();++i) cout<<wspy[i] <<" ";cout << endl;

	for(i = 0; i < n; ++i) //if( w[px[i].second]==niesk) 
	{
		j = *lower_bound(wspx.begin(),wspx.end(),px[i].first.first);//chodze po przedzialach x
		while(j < wspx.size()){
			//punkt = binarnieY(wspx[j],wspx[j+1]-1, px[i].first.second);
			punkt = -1;
			for(int k = wspx[j];k<wspx[j+1];++k) 
			if(px[k].first.second>px[i].first.second) 
			{
				punkt = k;
				break;
			}
			if(punkt == -1) ++j;
			else {
				w[px[i].second]=min(w[px[i].second],max(px[punkt].first.first-px[i].first.first,px[punkt].first.second-px[i].first.second));
				//break;
				++j;
			}
		}	
	}
	
	
	
	maksy=0;maksx=0;
	for(i=1;i < wspx.size();++i) {
		j = wspx[i]-1;
		if(w[px[j].second]<niesk) {maksy=max(maksy,px[j].first.second+w[px[j].second]);break;}
		else if(j-1 > wspx[i-1] && w[px[j-1].second]<niesk) maksy=max(maksy,px[j-1].first.second+w[px[j-1].second]);
	}
	for(i=1;i < wspy.size();++i) {
		j = wspy[i]-1;
		if(w[py[j].second]<niesk) {maksx=max(maksx,py[j].first.second+w[py[j].second]);break;}
		else if(j-1 > wspy[i-1] && w[py[j-1].second]<niesk) maksx=max(maksx,py[j-1].first.second+w[py[j-1].second]);
	}
	for(i=1;i < wspx.size();++i) {
		j = wspx[i]-1;
		if(w[px[j].second]<niesk &&  maksy > px[j].first.second+w[px[j].second]){wypisz = false;break;}
	}
	for(i=1;i < wspy.size();++i) {
		j = wspy[i]-1;
		if(w[py[j].second]<niesk &&  maksy > py[j].first.second+w[py[j].second]){wypisz = false;break;}
	}
	for(i=1;i < wspx.size();++i) {
		j = wspx[i]-1;
		if(w[px[j].second]==niesk ) 
			if(px[j].first.second < maksy)
				{
				w[px[j].second]=maksy-px[j].first.second;	
				}
			else 
					{
						w[px[j].second]=maksx-px[j].first.first;
						maksy=	px[j].first.second + w[px[j].second];			
					}

	}
	for(i=1;i < wspy.size() ;++i) {
		j = wspy[i]-1;
		if(w[py[j].second]==niesk) 
		if(py[j].first.second < maksx)
				{
				w[py[j].second]=maksx-py[j].first.second;	
				}
		else		
					{
						w[py[j].second]=maksy-py[j].first.first;
						maksx= 	py[j].first.second + w[py[j].second];					
					}
	}
	//cout <<maksx <<" "<<maksy<<endl;
	if(w[px[n-1].second]==niesk ) 
					{
						w[px[n-1].second]=maksx-px[n-1].first.first;
						maksy=	px[n-1].first.second + w[px[n-1].second];				
					}
	if(w[py[n-1].second]==niesk) 
					{
						w[py[n-1].second]=maksy-py[n-1].first.first;
						maksx=	py[n-1].first.second + w[py[n-1].second];					
					}
	//cout <<maksx <<" "<<maksy<<endl;
	for(i=1;i < wspx.size()-1 ;++i) {
		j = wspx[i]-1;
		//cout <<"ktory " <<px[j].second<<endl; 
		if(px[j].first.second < maksy && px[j].first.second + w[px[j].second] >= maksy ) 
					{
						
						w[px[j].second]=maksy-px[j].first.second;	
						maksx=	max(maksx, px[j].first.first +w[px[j].second] );
					}
	}
	//cout <<maksx <<" "<<maksy<<endl;	
	
	 for(i=1;i < wspy.size()-1 ;++i) {
		j = wspy[i]-1;
		if(py[j].first.second < maksx && py[j].first.second + w[py[j].second] > maksx ) 
					{
						w[py[j].second]=maksx-py[j].first.second;			
					}
	}	
	for(i = n-1; i >=0; --i) {
		j = i + 1;
		while(j < n && px[i].first.first == px[j].first.first) ++j;
		while(j< n && d[px[j].second] == 1 && px[i].first.first + w[px[i].second] > px[j].first.first){
			if(px[j].first.second < px[i].first.second && px[j].first.second + w[px[j].second] > px[i].first.second)
				w[px[i].second] = min(w[px[i].second], px[j].first.first - px[i].first.first);
			++j;	
		}
	}	
	for(i = n-1; i >=0; --i) {
		j = i + 1;
		while(j < n  && py[i].first.first == py[j].first.first) ++j;
		while(j< n && d[py[j].second] == 1 && py[i].first.first + w[py[i].second] > py[j].first.first){
			if(py[j].first.second < py[i].first.second && py[j].first.second + w[py[j].second] > py[i].first.second)
				w[py[i].second] = min(w[py[i].second], py[j].first.first - py[i].first.first);
			++j;	
		}
	}
	//cout <<maksx <<" "<<maksy<<endl;			
	long long pole1 = 0,pole2;				
	for(i = 0; i < n;++i) 	pole1 += (long long)(w[i])*w[i];
	pole2 = (long long)(maksx - minx);
	pole2 *= (long long)(maksy - miny);
	//cout << pole1 <<" "<<pole2<<endl;
	//if(pole1 != pole2) wypisz = false;						
	if(wypisz == true) {printf("TAK ");for(i=0;i<n;++i) printf("%d ",w[i]);printf("\n");}
	else printf("NIE\n");
	}
		
}


return 0;
}
int binarnieY(int l, int p,int szuk){
	return -1;
}
int binarnieX(int l, int p,int szuk){
	return -1;
}