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

using namespace std;

typedef long double ld;

const long double EPS=1e-9;

ld ABS(const long double& x){
    return (x<0)?-x:x;
}

void execute(){
    vector<pair<ld,ld>>tab;
    vector<pair<ld,ld>>goal;
    pair<ld,ld>cur=make_pair(0.d,0.d);
    int n,g=0;
    bool da_sie=true;
    ld a,b,c,e;
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a >> b >> c;
        tab.push_back(make_pair(b,a));
        goal.push_back(make_pair(c,a));
    }
    sort(tab.begin(),tab.end());
    sort(goal.begin(),goal.end());
    for(int i=0;(i<n-1||g<n-1)&&da_sie&&i<n;i++){
        if(goal[g].first+EPS<cur.first){
            da_sie=false;
            break;
        }
        while(g<n-1&&cur.first<goal[g].first+EPS&&tab[i].first+EPS>goal[g].first&&cur.second+tab[i].second+EPS>goal[g].second){
            if(ABS(goal[g].first-cur.first)<EPS){
                if(cur.second+EPS>goal[g].second){
                    cur.second-=goal[g].second;
                    g++;
                }
            }
            else if(ABS(goal[g].first-tab[i].first)<EPS){
                if(tab[i].first+EPS>goal[g].second){
                    tab[i].second-=goal[g].second;
                    g++;
                }
            }
            else{
                a=(tab[i].first-goal[g].first)/(goal[g].first-cur.first);
                c=(a*goal[g].second)/(a+1.d);
                if(c<cur.second+EPS&&tab[i].second+EPS>c/a){
                    cur.second-=c;
                    tab[i].second-=c/a;
                    g++;
                }
                else break;
            }
        }
        if(cur.second+tab[i].second!=0.d){
            cur.first=(tab[i].first*tab[i].second+cur.first*cur.second)/(tab[i].second+cur.second);
            cur.second+=tab[i].second;
        }
        else cur=make_pair(0.d,0.d);
        tab[i]=make_pair(0.d,0.d);
    }
    cur.first=(tab.back().first*tab.back().second+cur.first*cur.second)/(tab.back().second+cur.second);
    cur.second+=tab.back().second;
    if(da_sie&&ABS(cur.first-goal.back().first)<EPS&&ABS(cur.first-goal.back().first)<EPS)cout << "TAK" << endl;
    else cout << "NIE" << endl;
}

int main(){
    int t;
    cin >> t;
    while(t--)execute();
}