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
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <vector>

typedef std::pair<unsigned long long int, unsigned long long int> Element;

bool lessCompareTemp(const Element &lhs, const Element &rhs) {
    return lhs.first/lhs.second*rhs.second < rhs.first;
}

bool equalCompareTemp(const Element &lhs, const Element &rhs) {
    return lhs.first/lhs.second*rhs.second == rhs.first;
}

int main() {
    int T;
    scanf("%d", &T);

    while (T--) {
        int N;
        std::vector<Element> needed, avail;

        scanf("%d", &N);
        for (int i=0; i<N; ++i) {
            unsigned long long int cnt, begin, end;
            scanf("%llu %llu %llu", &cnt, &begin, &end);
            avail.emplace_back(begin*cnt, cnt);
            needed.emplace_back(end*cnt, cnt);
        }

        std::sort(avail.begin(), avail.end(), lessCompareTemp);
        std::sort(needed.begin(), needed.end(), lessCompareTemp);

        auto ait = avail.begin();
        auto nit = needed.begin();
        bool ok = true;

        while (ok && nit != needed.end()) {
            if (lessCompareTemp(*nit,*ait)) {
                ok = false;
                break;
            } else if (equalCompareTemp(*nit, *ait)) {
                unsigned long long int cnt = std::min(nit->second, ait->second);
                nit->first -= cnt * nit->first/nit->second;
                nit->second -= cnt;
                ait->first -= cnt * ait->first/ait->second;
                ait->second -= cnt;
            } else { //needed.tmp > avail.tmp
                auto aitn = ait;
                aitn++;

                if (aitn == avail.end()) {
                    ok = false;
                    break;
                } else {
                    Element joined(ait->first + aitn->first, ait->second + aitn->second);
                    if (!lessCompareTemp(*nit, joined)) {
                        aitn->first += ait->first;
                        aitn->second += ait->second;
                        ait->first = 0;
                        ait->second =0;
                    } else {
                        while (nit != needed.end() && lessCompareTemp(*nit, joined)) {
                            if (joined.second < nit->second) {
                                ok = false;
                                break;
                            }

                            joined.first -= nit->first;
                            joined.second -= nit->second;

                            if (lessCompareTemp(*aitn, joined)) {
                                ok = false;
                                break;
                            }
                            nit++;
                        }

                        *aitn = joined;
                        ait++;
                    }
                }
            }

            if (nit!=needed.end() && nit->second == 0) nit++;
            if (ait!=avail.end() && ait->second == 0) ait++;
        }
        
        if (ok) printf("TAK\n");
        else printf("NIE\n");
    }

    return 0;
}