#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define dbl(k, x) fixed << setprecision(k) << (x) template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e5 + 7; int n; LL L[N]; PLL A[N], B[N], pA[N], sA[N]; void solve() { cin >> n; LL sa = 0, sb = 0; for(int i = 1; i <= n; i++) { cin >> L[i] >> A[i].first >> B[i].first; sa += A[i].first * L[i]; sb += B[i].first * L[i]; A[i].second = B[i].second = L[i]; } if(sa != sb) { cout << "NIE\n"; return; } sort(A + 1, A + n + 1); sort(B + 1, B + n + 1); vector < PLL > vec; for(int i = 1; i <= n + 1; i++) { if(i <= n) vec.push_back(B[i]); /* if(i == n + 1 || B[i].first != B[i - 1].first) vec.push_back({B[i - 1].first, B[i - 1].second}); else B[i].second += B[i - 1].second; */ } for(int i = 1; i <= n; i++) { pA[i] = sA[i] = A[i]; } sort(all(vec)); int i1 = 1, i2 = n; LL cur1 = 0, cur2 = 0, l1 = 0, l2 = 0, k1 = 0, b1 = 0, k2 = 0, b2 = 0; for(int i = 0; i < sz(vec); i++) { k1 += vec[i].second; b1 += vec[i].second * vec[i].first; LL l = k1; while(i1 <= n && l1 < l) { if(l1 + pA[i1].second <= l) { l1 += pA[i1].second; cur1 += pA[i1].second * pA[i1].first; i1++; } else { pA[i1].second -= (l - l1); cur1 += (l - l1) * pA[i1].first; l1 = l; } } k2 += vec[sz(vec) - 1 - i].second; b2 += vec[sz(vec) - 1 - i].second * vec[sz(vec) - 1 - i].first; l = k2; while(i2 >= 1 && l2 < l) { if(l2 + sA[i2].second <= l) { l2 += sA[i2].second; cur2 += sA[i2].second * sA[i2].first; i2--; } else { sA[i2].second -= (l - l2); cur2 += (l - l2) * sA[i2].first; l2 = l; } } if(l1 != k1 || l2 != k2) { cout << "NIE\n"; return; } if(cur1 > b1 || b2 > cur2) { cout << "NIE\n"; return; } } cout << "TAK\n"; } int main() { _upgrade int t; cin >> t; while(t--) { 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 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 136 137 138 139 140 141 142 143 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define dbl(k, x) fixed << setprecision(k) << (x) template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e5 + 7; int n; LL L[N]; PLL A[N], B[N], pA[N], sA[N]; void solve() { cin >> n; LL sa = 0, sb = 0; for(int i = 1; i <= n; i++) { cin >> L[i] >> A[i].first >> B[i].first; sa += A[i].first * L[i]; sb += B[i].first * L[i]; A[i].second = B[i].second = L[i]; } if(sa != sb) { cout << "NIE\n"; return; } sort(A + 1, A + n + 1); sort(B + 1, B + n + 1); vector < PLL > vec; for(int i = 1; i <= n + 1; i++) { if(i <= n) vec.push_back(B[i]); /* if(i == n + 1 || B[i].first != B[i - 1].first) vec.push_back({B[i - 1].first, B[i - 1].second}); else B[i].second += B[i - 1].second; */ } for(int i = 1; i <= n; i++) { pA[i] = sA[i] = A[i]; } sort(all(vec)); int i1 = 1, i2 = n; LL cur1 = 0, cur2 = 0, l1 = 0, l2 = 0, k1 = 0, b1 = 0, k2 = 0, b2 = 0; for(int i = 0; i < sz(vec); i++) { k1 += vec[i].second; b1 += vec[i].second * vec[i].first; LL l = k1; while(i1 <= n && l1 < l) { if(l1 + pA[i1].second <= l) { l1 += pA[i1].second; cur1 += pA[i1].second * pA[i1].first; i1++; } else { pA[i1].second -= (l - l1); cur1 += (l - l1) * pA[i1].first; l1 = l; } } k2 += vec[sz(vec) - 1 - i].second; b2 += vec[sz(vec) - 1 - i].second * vec[sz(vec) - 1 - i].first; l = k2; while(i2 >= 1 && l2 < l) { if(l2 + sA[i2].second <= l) { l2 += sA[i2].second; cur2 += sA[i2].second * sA[i2].first; i2--; } else { sA[i2].second -= (l - l2); cur2 += (l - l2) * sA[i2].first; l2 = l; } } if(l1 != k1 || l2 != k2) { cout << "NIE\n"; return; } if(cur1 > b1 || b2 > cur2) { cout << "NIE\n"; return; } } cout << "TAK\n"; } int main() { _upgrade int t; cin >> t; while(t--) { solve(); } return 0; } |