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

using namespace std;
using LL = long long;
using PII = pair<int, int>;

int main()
{
	int t, n, l, a, b;

	cin >> t;

	for (int tt = 0; tt < t; ++tt)
	{
		cin >> n;

		vector<PII> start(n);
		vector<PII> end(n);

		LL sum1 = 0;
		LL sum2 = 0;

		for (int i = 0; i < n; ++i)
		{
			cin >> l >> a >> b;

			sum1 += 1LL * l * a;
			sum2 += 1LL * l * b;

			start[i] = make_pair(a, l);
			end[i] = make_pair(b, l);
		}

		sort(start.begin(), start.end());
		sort(end.begin(), end.end());

		int cnt1 = 1;
		int cnt2 = 1;

		for (int i = 1; i < n; ++i)
		{
			if (start[i].first == start[cnt1 - 1].first)
				start[cnt1 - 1].second += start[i].second;
			else
				start[cnt1++] = start[i];

			if (end[i].first == end[cnt2 - 1].first)
				end[cnt2 - 1].second += end[i].second;
			else
				end[cnt2++] = end[i];
		}

		int ind1 = 0;
		int ind2 = 0;

		while (ind1 < cnt1 && ind2 < cnt2 && start[ind1].first == end[ind2].first && start[ind1].second == end[ind2].second)
		{
			++ind1;
			++ind2;
		}

		if (ind1 < cnt1 && ind2 < cnt2 && (end[ind2].first < start[ind1].first || (end[ind2].first == start[ind1].first && end[ind2].second > start[ind1].second)))
		{
			cout << "NIE" << endl;
			continue;
		}

		ind1 = cnt1 - 1;
		ind2 = cnt2 - 1;

		while (ind1 >= 0 && ind2 >= 0 && start[ind1].first == end[ind2].first && start[ind1].second == end[ind2].second)
		{
			--ind1;
			--ind2;
		}

		if (ind1 >= 0 && ind2 >= 0 && (end[ind2].first > start[ind1].first || (end[ind2].first == start[ind1].first && end[ind2].second > start[ind1].second)))
		{
			cout << "NIE" << endl;
			continue;
		}

		if (sum1 == sum2)
			cout << "TAK" << endl;
		else
			cout << "NIE" << endl;
	}

	return 0;
}