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
#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAINS(A,X) (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); }
typedef signed long long slong;
typedef long double ldouble;
typedef pair<int,int> pii;
const slong INF = 1000000100;
const ldouble EPS = 1e-9;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}

#ifdef LOCALs
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

typedef pair<slong,slong> pll;
/// na unsigned int moze dzialac w sumie?
/// a moze unsigned long long? tylko punkty przesunac na dodatnie....

int n;
vector<slong> x, y, V;
const int MAXN = 10042;
slong ans[MAXN];

bool cmp(int i, int j) {
    // z gory na dol od lewej do prawej
    if(y[i] == y[j]) {
        return (x[i] < x[j]);
    }
    return (y[i] > y[j]);
}

slong calc_height(pll A, pll B) {
    assert(B.X >= A.X);
    slong side = B.X - A.X;
    slong h = A.Y + side;
    if(B.Y >= h) return 0;
    return h;
}

map<slong, pll> S; /// <x, <y, dlugosc> >, odcinki obrysu
typedef pair<slong,pll> segment;

void add_segment(int i, slong l, slong st) {
    /// nowy odcinek przykrywa czesciowo / w calosci odcinek st
    //cerr << "dodaje odcinek ";
    //debug(i, x[i], y[i], l, st);
    auto it = S.find(st);
    vector<segment> s;

    if(it != S.begin()) {
        it--;
        s.PB(*it);
        it = S.erase(it);
    }

    segment old = (*it);
    slong h = old.Y.X, w = old.Y.Y;

    if(st < x[i]) {
        s.PB({st, {h, x[i]-st}});
    }
    s.PB({x[i], {y[i], l}});
    if(x[i] + l < st + w) {
        s.PB({x[i]+l, {h,st+w-(x[i]+l)}});
    }

    it = S.erase(it);
    if(it != S.end()) {
        s.PB(*it);
        S.erase(it);
    }

    vector<segment> ns;
    for(segment t : s) {
        if(SIZE(ns) && ns.back().Y.X == t.Y.X) {
            ns.back().Y.Y += t.Y.Y;
        }
        else {
            ns.PB(t);
        }
    }

    for(segment t : ns) {
        S[t.X] = t.Y;
    }
    //debug(S);
}

slong add_square(int i) {
    /// TODO: napisac

    auto it = S.upper_bound(x[i]);
    assert(it != S.begin());
    it--;

    slong st = (it->X), h = (it->Y).X, l = (it->Y).Y;
    if(y[i] >= h) return 0;
    slong side = h - y[i];

    if(x[i] + side > st + l) return 0; /// nie wkleja sie ladnie

    add_segment(i, side, st);

    return side;
}

int check_height(slong h, int ontop=-1) {
    /// jak ontop zdefiniowany to na koncu trzeba go na gorze polozyc
    /// TODO: napisac

    //cerr << "check height ";
    //debug(h, ontop);

    slong w = 0;
    slong minx = INF, miny = INF;
    FORW(i,0,n) {
        minx = min(minx, x[i]);
        miny = min(miny, y[i]);
    }

    S.clear();
    S[0] = {h,INF*3};
    slong area_sum = 0; /// !!!!! nie przekreci sie? da sie wiecej niz (2*10^9) * (3*10^9)? !!!!!!

    for(int i : V) {
        if(i == ontop) continue;
        slong side = add_square(i);
        if(side == 0) return 0;
        ans[i] = side;
        area_sum += side * side;
        w = max(w, x[i] + side);
    }

    slong expected = (w - minx) * (h - miny);
    if(area_sum != expected) return 0;

    /// TODO: dodaj ontopa jesli zdefiniowany
    if(ontop != -1) {
        ans[ontop] = w - minx;
    }

    /// TODO: jakies sprawdzenie jeszczo jedno z assertami?

    cout << "TAK";
    FORW(i,0,n) cout << " " << ans[i];
    cout << "\n";

    return 1;
}

slong sqsum = 0, duze = 0;

void solve()
{
    cin >> n;
    x.resize(n);
    y.resize(n);
    sqsum += slong(n) * n;
    if(n >= 200) duze++;
    pll lefttop = {INF,-1};
    int lt = 0;
    FORW(i,0,n) {
        cin >> x[i] >> y[i];
        if(x[i] < lefttop.X || (x[i] == lefttop.X && y[i] > lefttop.Y)) {
            lefttop = {x[i], y[i]};
            lt = i;
        }
    }

    if(n == 1) {
        cout << "TAK 1\n";
        return;
    }

    V.resize(n);
    FORW(i,0,n) V[i] = i;
    sort(ALL(V), cmp);
    //debug(lefttop, lt, V);

    /// cala gora to jeden kwadrat
    if(check_height(lefttop.Y, lt)) return;

    FORW(i,0,n) {
        pll p = {x[i], y[i]}; /// kandydat na prawego gornego sasiada
        if(i == lt) continue;
        slong h = calc_height(lefttop, p);
        if(h == 0) continue;
        if(check_height(h)) return;
    }

    //cerr << "FAIL!!!" << endl;
    cout << "NIE\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    int tt = t;
    while(t--) solve();
    //debug(sqsum, duze, tt);
}