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
#include<bits/stdc++.h>
#define st first
#define nd second
using namespace std;
const int N=100005;
const long double eps=1e-7;
pair<int, int> tab[N], tab2[N];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int q;
    cin>>q;
    while(q--){
        int n, wsk1, wsk2;
        cin>>n;
        long long s1=0, s2=0;
        for(int i=0; i<n; i++){
            cin>>tab[i].nd>>tab[i].st>>tab2[i].st;
            tab2[i].nd=tab[i].nd;
            s1+=tab[i].st*(long long)tab[i].nd;
            s2+=tab2[i].st*(long long)tab2[i].nd;
        }
        if(s1!=s2){
            cout<<"NIE\n";
            continue;
        }
        sort(tab, tab+n);
        sort(tab2, tab2+n);
        bool b=1;
        wsk1=n-2;
        wsk2=n-1;
        long double v1=tab[n-1].nd;
        long double q1=tab[n-1].st*v1;
        while(wsk1>=0 or wsk2>=0){
            //cout<<q1<<" "<<v1<<"\n";
            if(wsk1<0){
                //cout<<"\n\n\n";
                //cout<<t;
                while(wsk2+1){
                    if(q1!=v1*tab2[wsk2--].st){
                        b=0;
                    }
                }
                break;
            }
            if(tab2[wsk2].st*v1>q1 and tab2[wsk2].st>tab[wsk1].st){
                b=0;
                break;
            }
            //cout<<"a";
            long long v2=tab[wsk1].nd, v=tab2[wsk2].nd, t2=tab[wsk1].st;
            long long  q2=tab[wsk1].st*tab[wsk1].nd, q=tab2[wsk2].st*tab2[wsk2].nd;
            wsk1--;
            while(tab[wsk1].st==t2 and wsk1+1){
                q2+=tab[wsk1].st*tab[wsk1].nd;
                wsk1--;
            }
            if((q1+q2)*v>(v1+v2)*q){
                q1=q1+q2;
                v1=v1+v2;
                continue;
            }
            //cout<<"b";
            if(v1+v2<v){
                b=0;
                break;
            }
           // cout<<"c";
            if(q1+q2<q){
                b=0;
                break;
            }
            wsk2--;
            q1=q1+q2-q;
            v1=v1+v2-v;
            //cout<<"d";
        }
        if(b){
            cout<<"TAK\n";
        }
        else{
            cout<<"NIE\n";
        }
    }
}