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
//Adrian Oklecinski 2019
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int l[100009], a[100009], b[100009], tabb[1000009];
long long asum, bsum;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        bool prawd=false;
        bool test=false;
        int maks1=-9, mini1=2000000000, maks2=-9, mini2=2000000000;
        bool spr=false;
        asum=0;
        bsum=0;
        int n;
        cin>>n;
        for(int i=0; i<=1000008; ++i){
            tabb[i]=0;
        }
        for(int i=0; i<n; ++i){
            cin>>l[i]>>a[i]>>b[i];
            tabb[a[i]]+=l[i];
            tabb[b[i]]-=l[i];
            maks1=max(maks1, a[i]);
            maks2=max(maks2, b[i]);
            mini1=min(mini1, a[i]);
            mini2=min(mini2, b[i]);
            asum+=l[i]*a[i];
            bsum+=l[i]*b[i];
        }
        if(maks1>=maks2 && mini1<=mini2){
            spr=true;
        }
        else{
            prawd=true;
        }

        if(spr){

            if(asum==bsum){
                test=true;
            }
            else{
                prawd=true;
            }

            if(test){
                for(int i=0; i<max(maks1, maks2)-1; ++i){
                    if(tabb[i]==0){
                        continue;
                    }
                    else if(tabb[i]>0){
                        tabb[i+2]-=tabb[i];
                        tabb[i+1]+=2*tabb[i];
                        tabb[i]=0;
                    }
                    else{
                        prawd=true;
                        break;
                    }
                }
                if(tabb[max(maks1, maks2)-1]!=0 || tabb[max(maks1, maks2)]!=0){
                    prawd=true;
                }
            }
        }
        if(prawd){
            cout<<"NIE"<<endl;
        }
        else{
            cout<<"TAK"<<endl;
        }
    }
    return 0;
}