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
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

bool solve_case(map<int, long double> &available, map<int, long double> &required) {
  long double cur_a = 0, cur_t = 0;
  map<int,long double>::iterator available_it=available.begin();

  for (map<int,long double>::iterator it=required.begin(); it!=required.end() && available_it!=available.end();) {
    while (available_it->first < it->first && available_it != available.end()) {
      // printf("%d %Le - %Le %Le %d %Le\n", it->first, it->second, cur_t, cur_a, available_it->first, available_it->second);
      cur_t = (cur_a*cur_t + available_it->second*available_it->first)/(cur_a+available_it->second);
      cur_a += available_it->second;
      available_it->second = 0.0;

      available_it++;
    }

    // printf("%Le %Le\n", cur_a, cur_t);
    
    while (available_it->first >= it->first && it != required.end()) {
      long double x = it->second*(it->first - cur_t)/(available_it->first - cur_t);
      long double y = it->second - x;

      // printf("%d %Le - %Le %Le %d %Le\n", it->first, it->second, cur_t, cur_a, available_it->first, available_it->second);
      // printf("%Le %Le\n", x, y);

      if (cur_a - y < -0.0000000001) {
        // printf("%Le %Le\n", cur_a, y);
        return false;
      }

      if (available_it->second - x < -0.0000000001) {
        cur_t = (cur_a*cur_t + available_it->second*available_it->first)/(cur_a+available_it->second);
        cur_a += available_it->second;
        available_it->second = 0.0;

        available_it++;
      } else {
        cur_a -= y;
        available_it->second -= x;
        it->second = 0.0;
        it++;
      }
    }
  }

  return true;
}

int main() {
  int t;

  scanf("%d", &t);

  for(int i=0; i<t; i++) {
    int n;
    scanf("%d", &n);

    map<int, long double> available;
    map<int, long double> required;

    for(int j=0; j<n; j++) {
      int l, a, b;

      scanf("%d %d %d", &l, &a, &b);

      if (available.find(a) != available.end()) {
        available[a] += l;
      } else {
        available[a] = l;
      }

      if (required.find(b) != required.end()) {
        required[b] += l;
      } else {
        required[b] = l;
      }
    }
  
    puts(solve_case(available, required) ? "TAK" : "NIE");
  }
}