#include<cstdio> #include<algorithm> #include<utility> #include<cmath> using namespace std; int t; pair<int, int> events[200005]; bool comp(pair<int, int> x, pair<int, int> y){ if(abs(x.first) != abs(y.first)) return abs(x.first) < abs(y.first); return x.first > y.first; } int main(){ scanf("%d", &t); while(t--){ int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ int a, b, c; scanf("%d %d %d", &a, &b, &c); events[2*i - 1] = make_pair(b, a); events[2*i] = make_pair((-1)*c, a); } sort(events + 1, events + (2*n) + 1, comp); long long stored_energy = 0LL; long long energy_delta = 0LL; bool expired = false; for(int i = 1; i <= (2*n); i++){ stored_energy += energy_delta * (long long)(abs(events[i].first) - abs(events[i-1].first)); if(stored_energy < 0LL){ expired = true; printf("NIE\n"); break; } if(events[i].first < 0) energy_delta -= (long long)events[i].second; else energy_delta += (long long)events[i].second; } if(expired) continue; if(stored_energy == 0LL) printf("TAK\n"); else printf("NIE\n"); } 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 | #include<cstdio> #include<algorithm> #include<utility> #include<cmath> using namespace std; int t; pair<int, int> events[200005]; bool comp(pair<int, int> x, pair<int, int> y){ if(abs(x.first) != abs(y.first)) return abs(x.first) < abs(y.first); return x.first > y.first; } int main(){ scanf("%d", &t); while(t--){ int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ int a, b, c; scanf("%d %d %d", &a, &b, &c); events[2*i - 1] = make_pair(b, a); events[2*i] = make_pair((-1)*c, a); } sort(events + 1, events + (2*n) + 1, comp); long long stored_energy = 0LL; long long energy_delta = 0LL; bool expired = false; for(int i = 1; i <= (2*n); i++){ stored_energy += energy_delta * (long long)(abs(events[i].first) - abs(events[i-1].first)); if(stored_energy < 0LL){ expired = true; printf("NIE\n"); break; } if(events[i].first < 0) energy_delta -= (long long)events[i].second; else energy_delta += (long long)events[i].second; } if(expired) continue; if(stored_energy == 0LL) printf("TAK\n"); else printf("NIE\n"); } return 0; } |