#include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < int(n); i++) #define ForD(i, n) for (int i = int(n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) template<typename T, typename U> pair<T, U>& operator+=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first += rhs.first; lhs.second += rhs.second; return lhs; } template<typename T, typename U> pair<T, U>& operator-=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first -= rhs.first; lhs.second -= rhs.second; return lhs; } template <class T> ostream &operator<<(ostream &os, const vector<T> &container) { for (auto &u : container) os << u << " "; return os; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update // #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; template<typename T> using pb_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif template<typename T, typename U> pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) { return {l.first - r.first, l.second - r.second}; } template<typename T, typename U> pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) { return {l.first + r.first, l.second + r.second}; } #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; bool solve() { map<int, ll> present; map<int, ll> expected; int n; cin >> n; For (i, n) { ll l; int a, b; cin >> l >> a >> b; if (a == b) continue; present[a] += l; expected[b] += l; } vector<int> rems; for (auto& [x, l] : present) { ll rem = min(l, expected[x]); l -= rem; expected[x] -= rem; rems.push_back(x); } for (int x : rems) { if (expected[x] == 0) expected.erase(x); if (present[x] == 0) present.erase(x); } if (expected.empty() && present.empty()) return true; if (expected.empty() ^ present.empty()) return false; if (expected.begin()->first < present.begin()->first || expected.rbegin()->first > present.rbegin()->first) return false; ll sum_exp = 0; for (auto [x, l] : expected) sum_exp += (ll)x * l; ll sum_pre = 0; for (auto [x, l] : present) sum_pre += (ll)x * l; ll curr_pre = 0; ll curr_exp = 0; ll pre_l = 0; ll exp_l = 0; auto pre_it = present.rbegin(); for (auto exp_it = expected.rbegin(); exp_it != expected.rend(); exp_it++) { curr_exp += (ll)exp_it->first * exp_it->second; exp_l += exp_it->second; while (pre_it != present.rend() && exp_l > pre_l) { ll add = min(pre_it->second, exp_l - pre_l); pre_it->second -= add; pre_l += add; curr_pre += add * (ll)pre_it->first; if (pre_it->second == 0) pre_it++; } if (curr_exp > curr_pre) return false; } return sum_exp == sum_pre; } int main() { _upgrade; int t; cin >> t; while (t--) { if (solve()) cout << "TAK\n"; else cout << "NIE\n"; } }
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 144 145 146 147 148 149 | #include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < int(n); i++) #define ForD(i, n) for (int i = int(n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) template<typename T, typename U> pair<T, U>& operator+=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first += rhs.first; lhs.second += rhs.second; return lhs; } template<typename T, typename U> pair<T, U>& operator-=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first -= rhs.first; lhs.second -= rhs.second; return lhs; } template <class T> ostream &operator<<(ostream &os, const vector<T> &container) { for (auto &u : container) os << u << " "; return os; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update // #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; template<typename T> using pb_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif template<typename T, typename U> pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) { return {l.first - r.first, l.second - r.second}; } template<typename T, typename U> pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) { return {l.first + r.first, l.second + r.second}; } #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; bool solve() { map<int, ll> present; map<int, ll> expected; int n; cin >> n; For (i, n) { ll l; int a, b; cin >> l >> a >> b; if (a == b) continue; present[a] += l; expected[b] += l; } vector<int> rems; for (auto& [x, l] : present) { ll rem = min(l, expected[x]); l -= rem; expected[x] -= rem; rems.push_back(x); } for (int x : rems) { if (expected[x] == 0) expected.erase(x); if (present[x] == 0) present.erase(x); } if (expected.empty() && present.empty()) return true; if (expected.empty() ^ present.empty()) return false; if (expected.begin()->first < present.begin()->first || expected.rbegin()->first > present.rbegin()->first) return false; ll sum_exp = 0; for (auto [x, l] : expected) sum_exp += (ll)x * l; ll sum_pre = 0; for (auto [x, l] : present) sum_pre += (ll)x * l; ll curr_pre = 0; ll curr_exp = 0; ll pre_l = 0; ll exp_l = 0; auto pre_it = present.rbegin(); for (auto exp_it = expected.rbegin(); exp_it != expected.rend(); exp_it++) { curr_exp += (ll)exp_it->first * exp_it->second; exp_l += exp_it->second; while (pre_it != present.rend() && exp_l > pre_l) { ll add = min(pre_it->second, exp_l - pre_l); pre_it->second -= add; pre_l += add; curr_pre += add * (ll)pre_it->first; if (pre_it->second == 0) pre_it++; } if (curr_exp > curr_pre) return false; } return sum_exp == sum_pre; } int main() { _upgrade; int t; cin >> t; while (t--) { if (solve()) cout << "TAK\n"; else cout << "NIE\n"; } } |