#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> PII; typedef pair<ll, int> PILL; typedef pair<ll, ll> PLL; const int MAX_N = 1e5+5; const int M = 1e6; const ll INF = (ll)(1e18); const int inf = 1e9; const ll MOD = 1000000007LL; void solve() { int n; map<ll, ll> before, after; vector<ll> befT, aftT; cin >> n; ll sumBefore = 0LL; ll sumAfter = 0LL; for (int i = 0; i < n; i++) { ll l, a, b; cin >> l >> a >> b; sumBefore += l*a; sumAfter += l*b; before[a] += l; after[b] += l; befT.push_back(a); aftT.push_back(b); } if (sumBefore != sumAfter) { cout << "NIE\n"; return; } sort(befT.begin(), befT.end()); sort(aftT.begin(), aftT.end()); auto it1 = unique(befT.begin(), befT.end()); auto it2 = unique(aftT.begin(), aftT.end()); befT.resize(distance(befT.begin(), it1)); aftT.resize(distance(aftT.begin(), it2)); int s = min(befT.size(), aftT.size()); for (int i = 0; i < s; i++) { ll t1 = befT[i]; ll t2 = aftT[i]; if (t2 < t1) { cout << "NIE\n"; return; } if (t1 == t2 && before[t1] == after[t2]) continue; if (t1 == t2 && before[t1] < after[t2]) { cout << "NIE\n"; return; } break; } for (int i = s-1; i >= 0; i--) { ll t1 = befT[i]; ll t2 = aftT[i]; if (t2 > t1) { cout << "NIE\n"; return; } if (t1 == t2 && before[t1] == after[t2]) continue; if (t1 == t2 && before[t1] < after[t2]) { cout << "NIE\n"; return; } break; } cout << "TAK\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; for (int i = 0; i < T; i++) { solve(); } return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int, int> PII; typedef pair<ll, int> PILL; typedef pair<ll, ll> PLL; const int MAX_N = 1e5+5; const int M = 1e6; const ll INF = (ll)(1e18); const int inf = 1e9; const ll MOD = 1000000007LL; void solve() { int n; map<ll, ll> before, after; vector<ll> befT, aftT; cin >> n; ll sumBefore = 0LL; ll sumAfter = 0LL; for (int i = 0; i < n; i++) { ll l, a, b; cin >> l >> a >> b; sumBefore += l*a; sumAfter += l*b; before[a] += l; after[b] += l; befT.push_back(a); aftT.push_back(b); } if (sumBefore != sumAfter) { cout << "NIE\n"; return; } sort(befT.begin(), befT.end()); sort(aftT.begin(), aftT.end()); auto it1 = unique(befT.begin(), befT.end()); auto it2 = unique(aftT.begin(), aftT.end()); befT.resize(distance(befT.begin(), it1)); aftT.resize(distance(aftT.begin(), it2)); int s = min(befT.size(), aftT.size()); for (int i = 0; i < s; i++) { ll t1 = befT[i]; ll t2 = aftT[i]; if (t2 < t1) { cout << "NIE\n"; return; } if (t1 == t2 && before[t1] == after[t2]) continue; if (t1 == t2 && before[t1] < after[t2]) { cout << "NIE\n"; return; } break; } for (int i = s-1; i >= 0; i--) { ll t1 = befT[i]; ll t2 = aftT[i]; if (t2 > t1) { cout << "NIE\n"; return; } if (t1 == t2 && before[t1] == after[t2]) continue; if (t1 == t2 && before[t1] < after[t2]) { cout << "NIE\n"; return; } break; } cout << "TAK\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; for (int i = 0; i < T; i++) { solve(); } return 0; } |