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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <vector>

using namespace std;

//const int size = 100002;

class tea {
public:
        unsigned long long vol;
        unsigned long long temp;
        tea(unsigned long long v, unsigned long long t);
        tea();
        static bool teaCompare (tea i, tea j) { return (i.temp < j.temp); }
        //void print() const;
};

tea::tea( unsigned long long v, unsigned long long t) : vol(v), temp(t) { }

tea::tea() { vol = 0; temp = 0; }

/*
void tea::print() const
{
        printf("vol %llu, temp  %llu\n", this->vol, this->temp);
}

void print_vect_tea(const vector<tea> &v)
{
        for(vector<const tea>::iterator it = v.begin(); it != v.end(); ++it) {
                it->print();
        }

}
*/

vector<tea> tab_act;
vector<tea> tab_req;

int n;

int main()
{
        int t;
        unsigned long long act_temp, req_temp;
        unsigned long long act_vol, req_vol, ori_vol;
        int act_min_temp, act_max_temp;
        int req_min_temp, req_max_temp;
        int answered_no;

        scanf("%d\n", &t);
        for(int it = 0; it < t; ++it) {
                //printf("test %d\n", it+1);
                scanf("%d\n", &n);
                act_temp = 0;
                req_temp = 0;
                act_max_temp = req_max_temp = -1;
                act_min_temp = req_min_temp = 1000005;
                tab_act.clear();
                tab_req.clear();
                for(int i = 0; i < n; ++i) {
                        int l, a, b;
                        scanf("%d %d %d\n", &l, &a, &b);
                        tab_act.push_back( tea(l, a) );
                        tab_req.push_back( tea(l, b) );
                        act_temp += l*a;
                        req_temp += l*b;
                        if (a < act_min_temp) {
                                act_min_temp = a;
                        }
                        if (a > act_max_temp) {
                                act_max_temp = a;
                        }
                        if (b < req_min_temp) {
                                req_min_temp = b;
                        }
                        if (b > req_max_temp) {
                                req_max_temp = b;
                        }
                }
                if (act_temp != req_temp) {
                        printf("NIE\n");
                        continue;
                }
                if (act_min_temp > req_min_temp) {
                        printf("NIE\n");
                        continue;
                }
                if (act_max_temp < req_max_temp) {
                        printf("NIE\n");
                        continue;
                }
                sort( tab_act.begin(), tab_act.end(), tea::teaCompare );
                sort( tab_req.begin(), tab_req.end(), tea::teaCompare );

                act_temp = req_temp = 0;
                act_vol = req_vol = 0;
                answered_no = 0;
                vector<tea>::iterator itact = tab_act.begin();
                ori_vol = itact->vol;

                for(vector<tea>::iterator it = tab_req.begin(); it != tab_req.end(); ++it) {
                        req_temp += it->temp * it->vol;
                        req_vol += it->vol;
                        
                        while(act_vol < req_vol) {
                                if (act_vol + itact->vol <= req_vol) {
                                        act_vol += itact->vol;
                                        act_temp += itact->vol*itact->temp;
                                        itact->vol = ori_vol;
                                        ++itact;
                                        if (itact != tab_act.end()) {
                                                ori_vol = itact->vol;
                                        }
                                } else {
                                        unsigned long long part = req_vol-act_vol;

                                        act_vol += part;
                                        act_temp += part*itact->temp;
                                        itact->vol -= part;
                                }
                        }
                        //printf("act_vol %lld, req_vol %lld, act_temp %lld, req_temp %lld\n", act_vol, req_vol, act_temp, req_temp);
                        if (act_temp > req_temp) {
                                printf("NIE\n");
                                answered_no = 1;
                                break;
                        }
                }
                if (answered_no) {
                        continue;
                }

                act_temp = req_temp = 0;
                act_vol = req_vol = 0;
                answered_no = 0;
                vector<tea>::reverse_iterator ritact = tab_act.rbegin();
                //print_vect_tea(tab_act);
                for(vector<tea>::reverse_iterator it = tab_req.rbegin(); it != tab_req.rend(); ++it) {
                        req_temp += it->temp * it->vol;
                        req_vol += it->vol;
                        
                        while(act_vol < req_vol) {
                                if (act_vol + ritact->vol <= req_vol) {
                                        act_vol += ritact->vol;
                                        act_temp += ritact->vol*ritact->temp;
                                        ++ritact;
                                } else {
                                        unsigned long long part = req_vol-act_vol;

                                        act_vol += part;
                                        act_temp += part*ritact->temp;
                                        ritact->vol -= part;
                                }
                        }
                        //printf("act_vol %lld, req_vol %lld, act_temp %lld, req_temp %lld\n", act_vol, req_vol, act_temp, req_temp);
                        if (act_temp < req_temp) {
                                printf("NIE\n");
                                answered_no = 1;
                                break;
                        }
                }
                if (answered_no) {
                        continue;
                }
                printf("TAK\n");
        }
        return 0;
}