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

using namespace std;

void map_add_helper(map<int, int>& map, int x, int l)
{
    if (map.find(x) != map.end())
    {
        map[x] += l;
    }
    else
    {
        map[x] = l;
    }
}

void solve_case()
{
    int n;
    unsigned long long sumI, sumO;
    map<int, int> input = {};
    map<int, int> output = {};

    cin >> n;
    sumI = sumO = 0;

    for (int i = 0; i < n; ++i)
    {
        int l, x, y;
        cin >> l >> x >> y;
        sumI += l*x;
        sumO += l*y;

        map_add_helper(input, x, l);
        map_add_helper(output, y, l);
    }

    if (sumI != sumO)
    {
        cout << "NIE" << endl;
    }
    else
    {
        while (!output.empty() && !input.empty())
        {
            auto[minO, volO] = *output.begin();
            auto piv = input.lower_bound(minO);

            //cout << "Question: (" << minO << ", " << volO << ")" << endl;

            if (piv == input.end())
            {
                //cout << "no pivot - ";
                break;
            }

            auto[pivVal, pivVol] = *piv;
            //cout << "More/Eq: (" << pivVal << ", " << pivVol << ")" << endl;
            if (pivVal == minO)
            {
                //cout << "Pivot match! volumeny: " << pivVol << " | " << volO << endl;
                output[minO] -= pivVol;
                input[pivVal] -= volO;

                if (input[pivVal] <= 0)
                    input.erase(pivVal);

                if (output[minO] <= 0)
                    output.erase(minO);
            }
            else if(piv == input.begin())
            {
                //cout << "No smaller - ";
                break;
            }
            else
            {
                auto[minI, volI] = *(--piv);
                //auto[minI, volI] = *input.begin();
                
                //cout << "Less: (" << minI << ", " << volI << ")" << endl;

                const int maxVol = min(min(volI, volO), pivVol);
                const int tmpSum = minI + pivVal;
                const int newVal = tmpSum - minO;

                //cout << "Wymagany split! (" << newVal << ", " << maxVol << ") - zrobione z: (" << minI << ", " << volI << ") oraz (" << pivVal << ", " << pivVol << ")" << endl;

                input[minI] -= maxVol;
                input[pivVal] -= maxVol;
                output[minO] -= maxVol;

                if (input.find(newVal) != input.end())
                    input[newVal] += maxVol;
                else
                    input.insert({newVal, maxVol});

                if (input[minI] <= 0)
                    input.erase(minI);

                if (input[pivVal] <= 0)
                    input.erase(pivVal);

                if (output[minO] <= 0)
                    output.erase(minO);
            }

            //cout << endl;
        }

        if (!output.empty())
        {
            cout << "NIE" << endl;
        }
        else
        {
            cout << "TAK" << endl;
        }
    }
}

int main()
{
    int t;
    cin >> t;

    for (int i = 0; i < t; ++i)
    {
        solve_case();
    }

    return 0;
}