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
#include <bits/stdc++.h>
using namespace std;

using u64 = uint64_t;
using i64 = int64_t;

// print tuples {{{
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& t) {
    return os << '[' << t.first << ',' << t.second << ']';
}
// }}}
// print containers {{{
template <typename It>
void print(ostream& os, It begin, It end, u64 len, u64 limit = 30) {
    u64 count = 0;
    os << "{";
    while (begin != end && count < limit) {
        os << "(" << *begin << ")";
        count++;
        begin++;
    }
    if (begin != end)
        os << "... " << len << " total";
    os << "}";
}
#define MAKE_PRINTER_1(container) \
template <typename T> ostream& operator<<(ostream& os, const container<T>& t) { print(os, t.begin(), t.end(), t.size()); return os; }
#define MAKE_PRINTER_2(container) \
template <typename T1, typename T2> \
ostream& operator<<(ostream& os, const container<T1, T2>& t) { \
    print(os, t.begin(), t.end(), t.size()); \
    return os; \
}
MAKE_PRINTER_1(vector)
MAKE_PRINTER_2(map)
MAKE_PRINTER_1(set)
MAKE_PRINTER_2(unordered_map)
MAKE_PRINTER_1(unordered_set)
#undef MAKE_PRINTER_1
#undef MAKE_PRINTER_2
// }}}
// read/write {{{
template <typename T> T read() { T e; cin >> e; return e; }
void read() {}
template <typename T, typename ...Ts> void read(T& v, Ts& ...ts) { v = read<T>(); read(ts...); }
template <typename T> vector<T> readv(u64 n) { vector<T> v; for (u64 i = 0; i < n; i++) v.push_back(read<T>()); return v; }
template <typename T> struct identity { const T& operator()(const T& t) const { return t; } };
#define PRINTERS(FNAME, OUTP) \
    template <typename T> void FNAME(const T& t) { OUTP << t << ' '; } \
    void FNAME##ln() { OUTP << '\n'; } \
    template <typename T> void FNAME##ln(const T& t) { OUTP << t << '\n'; } \
    template <typename T, typename F = identity<typename T::value_type>> \
    void FNAME##v(const T& t, F f = F()) { for (const auto& e : t) FNAME(f(e)); FNAME##ln(); }
PRINTERS(print, cout)
#ifdef DEBUG_PRINTS
    PRINTERS(dprint, cerr)
#else
# define dprint(...)
# define dprintv(...)
# define dprintln(...)
#endif
/// }}}

struct P {
    u64 x, y;
    friend ostream& operator<<(ostream& os, const P& p) { return os << "(" << p.x << "," << p.y << ")"; }
};

class Segments {
public:
    Segments(u64 maxy) : maxy_(maxy) {
        segms_[maxy] = maxy + 1; // in this case the value is not important
    }
    bool is_in_segment(u64 y) const {
        assert(y < maxy_);
        auto it = segms_.upper_bound(y);
        assert(it != segms_.end());
        assert(it->first > y);
//        dprint("is in segment y ="); dprintln(y);
//        dprint("segms size: "); dprintln(segms_.size());
//        dprint("iterator's value is "); dprintln(*it);
        return it != segms_.begin() && y < (--it)->second;
    }
    u64 find_upper_segment(u64 y) const {
        assert(y < maxy_);
        const auto it = segms_.upper_bound(y);
        assert(it != segms_.end());
        assert(it->first > y);
        dprint("segms: "); dprintln(segms_);
        return it->first;
    }
    void add(u64 y1, u64 y2) {
        assert(segms_.find(y1) == segms_.end());
        assert(!is_in_segment(y1));
        segms_[y1] = y2;
    }
    void remove(u64 y) {
        assert(segms_.find(y) != segms_.end());
        segms_.erase(y);
    }
private:
    u64 maxy_;
    map<u64, u64> segms_;
};

class Squares {
public:
    Squares(u64 maxy) : maxy_(maxy), segments_(maxy) {
    }
    u64 add_square(const P& p) {
        flush_queue_(p.x);
        auto next_y = segments_.find_upper_segment(p.y);
        segments_.add(p.y, next_y);
        auto x = p.x + (next_y - p.y);
        dprint("adding to queue: "); dprintln(make_pair(x, p.y));
        remove_lists_[x].push_back(p.y);
        return next_y - p.y; // returns size of a square
    }
    bool is_in_square(const P& p) {
        flush_queue_(p.x);
        return p.y >= maxy_ || segments_.is_in_segment(p.y);
    }
private:
    void flush_queue_(u64 x) {
        auto it = remove_lists_.find(x);
        if (it != remove_lists_.end()) {
            for (auto y : it->second) {
                dprint("removing from queue: "); dprintln(make_pair(x, y));
                segments_.remove(y);
            }
            remove_lists_.erase(it);
        }
    }
    u64 maxy_;
    // element of the queue = right-down point of square
    unordered_map<u64, vector<u64>> remove_lists_;
    Segments segments_;
};

vector<pair<u64, u64>> solve(u64 maxy, const vector<pair<u64, P>>& ps,
        const unordered_set<u64>& xs) {
    const auto last_x = ps.back().second.x;
    dprint("solve with maxy: "); dprintln(maxy);
    Squares sq(maxy);
    vector<pair<u64, u64>> res;
    for (const auto& p : ps) {
        dprint("punkt "); dprintln(p);
        if (sq.is_in_square(p.second)) {
            dprint(p); dprintln("found in square");
            return {};
        }
        res.push_back(make_pair(p.first, sq.add_square(p.second)));
        dprint(res.back()); dprintln("is result");

        if (xs.find(res.back().second + p.second.x) == xs.end())
            if (res.back().second + p.second.x < last_x) {
                dprintln("...but this result doesn't place square on the left of any square");
                return {};
            }
    }
    return res;
}

void normalize(vector<pair<u64, P>>& ps) {
    u64 minx = ps[0].second.x, miny = ps[0].second.y;
    for (const auto& p : ps) {
        minx = min(p.second.x, minx);
        miny = min(p.second.y, miny);
    }
    for (auto& p : ps) {
        assert(p.second.x >= minx);
        assert(p.second.y >= miny); 
        p.second.x -= minx;
        p.second.y -= miny;
    }
}

struct Res {
    u64 maxx;
    bool is_good;
};

Res verify(
        const vector<pair<u64, P>>& ps,
        vector<pair<u64, u64>> sizes, u64 maxy) {
    Res res{0, false};
    if (sizes.size() == 0)
        return res;
    assert(ps.size() == sizes.size());
    u64 size_sum = 0;
    for (u64 i = 0; i < ps.size(); i++) {
        assert(sizes[i].first == ps[i].first);
        res.maxx = max(res.maxx, ps[i].second.x + sizes[i].second);
        size_sum += sizes[i].second * sizes[i].second;
    }
    assert(size_sum <= res.maxx * maxy);
    res.is_good = (size_sum == res.maxx * maxy);
    if (!res.is_good) {
        dprint("is no good, because there are empty spaces: "); dprintln(res.maxx * maxy - size_sum);
    }
    return res;
}

vector<pair<u64, u64>> handle_cap_case(vector<pair<u64, P>> v, const unordered_set<u64>& xs) {
    auto highest_square_index = v[0].first;
    auto maxy = v[0].second.y;
    v.erase(v.begin());
    auto sizes = solve(maxy, v, xs);
    auto res = verify(v, sizes, maxy);
    if (!res.is_good)
        return {};
    sizes.push_back(make_pair(highest_square_index, res.maxx));
    return sizes;
}

vector<pair<u64, u64>> go_t() {
    vector<pair<u64, P>> v;
    const u64 n = read<u64>();
    for (u64 i = 0; i < n; i++) {
        P p;
        read(p.x, p.y);
        v.push_back(make_pair(i, p));
    }
    assert(v.size());
    if (v.size() == 1)
        return {make_pair(0, 1)};
    normalize(v);
    sort(v.begin(), v.end(), [](const pair<u64, P>& a, const pair<u64, P>& b) -> bool {
            return a.second.x != b.second.x ? a.second.x < b.second.x : a.second.y > b.second.y;
    });
    dprint("sorted and normalized: ");
    dprintln(v);
    assert(v[0].second.x == 0);
    vector<u64> xs;
    unordered_set<u64> xs2;
    for (const auto& p : v) {
        if (p.second.x > 0)
            xs.push_back(p.second.x);
        xs2.insert(p.second.x);
    }
    sort(xs.begin(), xs.end());
    {
        auto sizes = handle_cap_case(v, xs2);
        if (sizes.size())
            return sizes;
    }
    for (u64 i = 0; i < xs.size(); i++) {
        auto x = (i % 2 == 0 ? xs[i / 2] : xs[xs.size() - 1u - i / 2]);
        auto maxy = v[0].second.y + x;
        auto sizes = solve(maxy, v, xs2);
        auto res = verify(v, sizes, maxy);
        if (res.is_good) {
            return sizes;
        }
    }
    return {};
}

void go() { // {{{
    u64 t = read<u64>();
    while (t--) {
        auto res = go_t();
        if (res.empty())
            cout << "NIE\n";
        else {
            cout << "TAK";
            sort(res.begin(), res.end());
            for (u64 i = 0; i < res.size(); i++)
                cout << " " << res[i].second;
            cout << "\n";
        }
    }
} // }}}

int main () { // {{{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    go();
} //