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
#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long t, n, l, a, b;
    long long minl1, mina, minl2, minb;
    long long maxl1, maxa, maxl2, maxb;
    long long minta, maxta, mintb, maxtb;
    
    cin >> t;
    
    while (t--)
    {
        long long energyBefore = 0ll, energyAfter = 0ll;
        cin >> n;
        bool isGood = true;

        maxl1 = maxl2 = 1ll;
        maxa = maxb = 1000001ll;
        
        minl1 = minl2 = 1000001ll;
        mina = minb = 1ll;

        minta = mintb = 1000001ll;
        maxta = maxtb = 1ll;
        
        while (n--)
        {
            cin >> l >> a >> b;
            minta = min(minta, a);
            maxta = max(maxta, a);
            mintb = min(mintb, b);
            maxtb = max(maxtb, b);
            
            energyBefore += l * a;
            energyAfter += l * b;

            if (l * mina < minl1 * a)
            {
                mina = a;
                minl1 = l;
            }

            if (l * maxa > maxl1 * a)
            {
                maxa = a;
                maxl1 = l;
            }

            if (l * minb < minl2 * b)
            {
                minb = b;
                minl2 = l;
            }

            if (l * maxb > maxl2 * b)
            {
                maxb = b;
                maxl2 = l;
            }
        }
        
        isGood &= (minl1 * minb <= minl2 * mina);
        isGood &= (maxl1 * maxb >= maxl2 * maxa);
        isGood &= (energyBefore == energyAfter);
        isGood &= (maxtb <= maxta);
        isGood &= (minta <= mintb);

        if (isGood)
        {
            cout << "TAK\n";
        }
        else
        {
            cout << "NIE\n";
        }
    }

    return 0;
}