#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define F(i,k,n) for(int i = k ; i < n ; ++i)
#define sz(x) (int)x.size()
#define var(x) #x << " " << x
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int,int> PII;
typedef pair<int,LL> PIL;
typedef pair<LL,int> PLI;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<LL> VLL;
const int INF=1e9+7,N=1e6+7;
LL a,b,c,d,l,n,m,t,k,wyn=0;
int main() {
boost;
cin >> t;
while(t--){
cin >> n;
LL sum_in=0,sum_out=0;
map<LL,LL> map_in,map_out;
vector<PLL> in,out;
for(int i=0;i<n;i++){
cin >> l >> a >> b;
sum_in+=l*a;
sum_out+=l*b;
map_in[a]+=l;
map_out[b]+=l;
}
if(sum_out!=sum_in){
cout << "NIE\n";
continue;
}
for(auto it=map_in.begin();it!=map_in.end();++it){
a=min(it->nd,map_out[it->st]);
map_out[it->st]-=a;
if(it->nd-a)
in.pb({it->st,it->nd-a});
}
for(auto it=map_out.begin();it!=map_out.end();++it)
if(it->nd)
out.pb({it->st,it->nd});
/* for(int i=0;i<sz(in);i++)
cout << "\tin[ " << i << " ] = { " << in[i].st << " , " << in[i].nd << " }\n";
for(int i=0;i<sz(out);i++)
cout << "\tout[ " << i << " ] = { " << out[i].st << " , " << out[i].nd << " }\n";*/
if((sz(in)==0)||(in[0].st<out[0].st && in.back().st>out.back().st)){
cout << "TAK\n";
continue;
}
cout << "NIE\n";
/*
int l=0,r=0,s=0;
while(r<sz(in) && out[0].st>=in[r].st){
r++;
}
cout << "\tpoczatkowe r:\t" r << "\n";
while(r<sz(in)){
a=min(in[l].nd,in[r].nd);
in[l].nd-=a;
in[r].nd-=a;
}*/
/*if()
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 | #include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false);cin.tie();cout.tie(); #define F(i,k,n) for(int i = k ; i < n ; ++i) #define sz(x) (int)x.size() #define var(x) #x << " " << x #define all(v) (v).begin(), (v).end() using namespace std; typedef long long LL; typedef long double LD; typedef pair<int,int> PII; typedef pair<int,LL> PIL; typedef pair<LL,int> PLI; typedef pair<LL,LL> PLL; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<LL> VLL; const int INF=1e9+7,N=1e6+7; LL a,b,c,d,l,n,m,t,k,wyn=0; int main() { boost; cin >> t; while(t--){ cin >> n; LL sum_in=0,sum_out=0; map<LL,LL> map_in,map_out; vector<PLL> in,out; for(int i=0;i<n;i++){ cin >> l >> a >> b; sum_in+=l*a; sum_out+=l*b; map_in[a]+=l; map_out[b]+=l; } if(sum_out!=sum_in){ cout << "NIE\n"; continue; } for(auto it=map_in.begin();it!=map_in.end();++it){ a=min(it->nd,map_out[it->st]); map_out[it->st]-=a; if(it->nd-a) in.pb({it->st,it->nd-a}); } for(auto it=map_out.begin();it!=map_out.end();++it) if(it->nd) out.pb({it->st,it->nd}); /* for(int i=0;i<sz(in);i++) cout << "\tin[ " << i << " ] = { " << in[i].st << " , " << in[i].nd << " }\n"; for(int i=0;i<sz(out);i++) cout << "\tout[ " << i << " ] = { " << out[i].st << " , " << out[i].nd << " }\n";*/ if((sz(in)==0)||(in[0].st<out[0].st && in.back().st>out.back().st)){ cout << "TAK\n"; continue; } cout << "NIE\n"; /* int l=0,r=0,s=0; while(r<sz(in) && out[0].st>=in[r].st){ r++; } cout << "\tpoczatkowe r:\t" r << "\n"; while(r<sz(in)){ a=min(in[l].nd,in[r].nd); in[l].nd-=a; in[r].nd-=a; }*/ /*if() cout << "TAK\n"; else cout << "NIE\n";*/ } } |
English