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
#include <iostream>
using namespace std;

#define MIN 1000005
#define MAX 0

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i)
	{
		long long suma = 0, am[MIN] = {0};
		int n, max1 = MAX, max2 = MAX, min1 = MIN, min2 = MIN;
		cin >> n;
		int tab[n][3];
		for (int j = 0; j < n; ++j)
		{
			cin >> tab[j][0] >> tab[j][1] >> tab[j][2];
			suma += tab[j][0] * (tab[j][1] - tab[j][2]);
			if (tab[j][1] > max1) max1 = tab[j][1];
			if (tab[j][1] < min1) min1 = tab[j][1];
			if (tab[j][2] > max2) max2 = tab[j][2];
			if (tab[j][2] < min2) min2 = tab[j][2];
		}
		if (suma != 0 || max2 > max1 || min2 < min1) cout << "NIE\n";
		else
		{
			for (int j = 0; j < n; ++j)
			{
				am[tab[j][1]] += tab[j][0];
				am[tab[j][2]] -= tab[j][0];
			}
			for (int j = min1; j < max1 - 1 && am[MIN-1] == 0; ++j)
			{
				if (am[j] >= 0)
				{
					am[j+2] -= am[j];
					am[j+1] += 2*am[j];
					am[j] = 0;
				}
				else
				{
					am[MIN-1] = 1;
					cout << "NIE\n";
				}
			}
			if (am[MIN-1] == 0) cout << "TAK\n";
		}
	}
	return 0;
}