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

using namespace std;
typedef long long I;

struct K {
    int t,l;
};

int main() {
    int T,n,l,a,b;
    scanf("%d", &T);
    vector<K> data;
    data.reserve(200000);
    while(T--) {
        scanf("%d", &n);
        data.clear();
        while(n--) {
            scanf("%d %d %d",&l, &a, &b);
            data.push_back({a,l});
            data.push_back({b,-l});
        }
        std::sort(data.begin(), data.end(), [](const K& a, const K &b) {return a.t==b.t? a.l>b.l : a.t < b.t;} );

        I Ep,Em,lp,lm;
        Ep=Em=lp=lm=0;
        for(const auto &k : data) {
            if(lm>lp && k.t*(lm-lp)>Em-Ep) {
                Ep = 1; Em = 0;
                break;
            }
            if(k.l > 0) {
                Ep+=k.l*I(k.t);
                lp+=k.l;
            } else {
                Em+=-k.l*I(k.t);
                lm+=-k.l;
            }
        }
        if(Ep != Em) printf("NIE\n");
        else printf("TAK\n");
    }
}