#include <bits/stdc++.h> using namespace std; const int N = 100001; int t; int n; long long a[N]; long long b[N]; long long l[N]; map<int,long long>tplus; map<int,long long>tminus; void insert(long long l,long long a,long long b){ tplus[a] += l; tminus[b] -= l; /*long long aval = tplus[a] + tminus[a]; long long bval = tplus[b] + tminus[b]; tplus.erase(a); tplus.erase(b); tminus.erase(a); tminus.erase(b); if(aval > 0) tplus[a] = aval; else if(aval < 0) tminus[a] = aval; if(bval > 0) tplus[b] = bval; else if(bval < 0) tminus[b] = bval;*/ } void normalize(){ // potrzebne? } int find_next(int p,bool allow_minus){ auto nxtIt = tplus.upper_bound(p); int nxt = (*nxtIt).first; if(nxtIt == tplus.end()) nxt = p; if(allow_minus){ auto nxtIt2 = tminus.upper_bound(p); if(nxtIt2 != tminus.end()) nxt = min(nxt,(*nxtIt2).first); } return nxt; } int find_prev(int q,bool allow_minus){ auto preIt = tplus.lower_bound(q); int pre = q; if(preIt != tplus.begin()){ preIt--; pre = (*preIt).first; } if(allow_minus){ auto preIt2 = tminus.lower_bound(q); if(preIt2 != tminus.begin()){ preIt2--; pre = max(pre,(*preIt2).first); } } return pre; } bool test(){ cin>>n; long long maxA=0,maxB=0; long long minA=1e7,minB=1e7; long long heatA=0,heatB=0; tplus.clear(); tminus.clear(); for(int i=0;i<n;i++){ cin>>l[i]>>a[i]>>b[i]; insert(l[i],a[i],b[i]); maxA = max(maxA,a[i]); maxB = max(maxB,b[i]); minA = min(minA,a[i]); minB = min(minB,b[i]); heatA += l[i]*a[i]; heatB += l[i]*b[i]; } if(maxA < maxB || minA > minB || heatA != heatB) return 0; /*for(int i=1;i<=maxA;i++){ if(tplus[i] != 0 || tminus[i] != 0) cout<<i<<" "<<tplus[i]<<" "<<tminus[i]<<endl; if(!tplus[i]) tplus.erase(i); if(!tminus[i]) tminus.erase(i); } cout<<endl; cout<<endl;*/ int p = find_next(0,false); int q = find_next(find_next(p,true),false); int prevP = -1; int prevQ = -1; while(p+1 <= q){ prevP = p; prevQ = q; if(tminus[p] != 0){ int dt = min(tplus[p],-1*tminus[p]); tplus[p] -= dt; tminus[p] += dt; if(tplus[p] == 0) tplus.erase(p); if(tminus[p] == 0) tminus.erase(p); } if(tminus[q] != 0){ int dt = min(tplus[q],-1*tminus[q]); tplus[q] -= dt; tminus[q] += dt; if(tplus[q] == 0) tplus.erase(q); if(tminus[q] == 0) tminus.erase(q); } p = find_next(0,false); q = find_next(find_next(p,true),false); /*if(tplus[p] < 0 || tplus[q] < 0){ cout<<p<<" "<<q<<endl; return 0; }*/ int d = min(tplus[p],tplus[q]); int nxt = find_next(p,true); int pre = find_prev(q,true); /*if(tminus.count(p) && tminus[p] != 0) nxt = p; if(tminus.count(q) && tminus[q] != 0) pre = q;*/ int step = min(nxt-p,q-pre); //if(step == 0) //d = -1*max(tminus[p],tminus[q]); tplus[p] -= d; tplus[q] -= d; if(tplus[p] < 0){ tminus[p] += tplus[p]; tplus[p] = 0; } if(tplus[q] < 0){ tminus[q] += tplus[q]; tplus[q] = 0; } //cout<<p<<" "<<q<<endl; tminus[p+step] += d; tminus[q-step] += d; if(tminus[p+step] > 0){ tplus[p+step] += tminus[p+step]; tminus[p+step] = 0; } if(tminus[q-step] > 0){ tplus[q-step] += tminus[q-step]; tminus[q-step] = 0; } if(tplus[p] == 0) tplus.erase(p); if(tplus[q] == 0) tplus.erase(q); if(tminus[p+step] == 0) tminus.erase(p+step); if(tminus[q-step] == 0) tminus.erase(q-step); p = find_next(0,false); q = find_next(find_next(p,true),false); //if(p == prevP && q == prevQ) //q = find_next(q,false); if(p == prevP && q == prevQ) break; } //cout<<p<<"____________"<<q<<endl; /*auto nxtIt = tminus.upper_bound(p); auto preIt = tminus.lower_bound(q); if(preIt != tminus.begin()) preIt--; int nxt = (*nxtIt).first; int pre = (*preIt).first; if(nxtIt == tminus.end()) nxt = p; if(preIt == tminus.begin()) pre = q;*/ // cout<<nxt<<"__"<<pre<<endl; /*for(int i=1;i<=maxA;i++) if(tplus[i] != 0 || tminus[i] != 0) cout<<i<<" "<<tplus[i]<<" "<<tminus[i]<<endl;*/ for(auto temp : tplus){ if(temp.second != 0) return 0; } for(auto temp : tminus){ if(temp.second != 0) return 0; } return 1; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>t; for(int i=0;i<t;i++){ if(test()) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | #include <bits/stdc++.h> using namespace std; const int N = 100001; int t; int n; long long a[N]; long long b[N]; long long l[N]; map<int,long long>tplus; map<int,long long>tminus; void insert(long long l,long long a,long long b){ tplus[a] += l; tminus[b] -= l; /*long long aval = tplus[a] + tminus[a]; long long bval = tplus[b] + tminus[b]; tplus.erase(a); tplus.erase(b); tminus.erase(a); tminus.erase(b); if(aval > 0) tplus[a] = aval; else if(aval < 0) tminus[a] = aval; if(bval > 0) tplus[b] = bval; else if(bval < 0) tminus[b] = bval;*/ } void normalize(){ // potrzebne? } int find_next(int p,bool allow_minus){ auto nxtIt = tplus.upper_bound(p); int nxt = (*nxtIt).first; if(nxtIt == tplus.end()) nxt = p; if(allow_minus){ auto nxtIt2 = tminus.upper_bound(p); if(nxtIt2 != tminus.end()) nxt = min(nxt,(*nxtIt2).first); } return nxt; } int find_prev(int q,bool allow_minus){ auto preIt = tplus.lower_bound(q); int pre = q; if(preIt != tplus.begin()){ preIt--; pre = (*preIt).first; } if(allow_minus){ auto preIt2 = tminus.lower_bound(q); if(preIt2 != tminus.begin()){ preIt2--; pre = max(pre,(*preIt2).first); } } return pre; } bool test(){ cin>>n; long long maxA=0,maxB=0; long long minA=1e7,minB=1e7; long long heatA=0,heatB=0; tplus.clear(); tminus.clear(); for(int i=0;i<n;i++){ cin>>l[i]>>a[i]>>b[i]; insert(l[i],a[i],b[i]); maxA = max(maxA,a[i]); maxB = max(maxB,b[i]); minA = min(minA,a[i]); minB = min(minB,b[i]); heatA += l[i]*a[i]; heatB += l[i]*b[i]; } if(maxA < maxB || minA > minB || heatA != heatB) return 0; /*for(int i=1;i<=maxA;i++){ if(tplus[i] != 0 || tminus[i] != 0) cout<<i<<" "<<tplus[i]<<" "<<tminus[i]<<endl; if(!tplus[i]) tplus.erase(i); if(!tminus[i]) tminus.erase(i); } cout<<endl; cout<<endl;*/ int p = find_next(0,false); int q = find_next(find_next(p,true),false); int prevP = -1; int prevQ = -1; while(p+1 <= q){ prevP = p; prevQ = q; if(tminus[p] != 0){ int dt = min(tplus[p],-1*tminus[p]); tplus[p] -= dt; tminus[p] += dt; if(tplus[p] == 0) tplus.erase(p); if(tminus[p] == 0) tminus.erase(p); } if(tminus[q] != 0){ int dt = min(tplus[q],-1*tminus[q]); tplus[q] -= dt; tminus[q] += dt; if(tplus[q] == 0) tplus.erase(q); if(tminus[q] == 0) tminus.erase(q); } p = find_next(0,false); q = find_next(find_next(p,true),false); /*if(tplus[p] < 0 || tplus[q] < 0){ cout<<p<<" "<<q<<endl; return 0; }*/ int d = min(tplus[p],tplus[q]); int nxt = find_next(p,true); int pre = find_prev(q,true); /*if(tminus.count(p) && tminus[p] != 0) nxt = p; if(tminus.count(q) && tminus[q] != 0) pre = q;*/ int step = min(nxt-p,q-pre); //if(step == 0) //d = -1*max(tminus[p],tminus[q]); tplus[p] -= d; tplus[q] -= d; if(tplus[p] < 0){ tminus[p] += tplus[p]; tplus[p] = 0; } if(tplus[q] < 0){ tminus[q] += tplus[q]; tplus[q] = 0; } //cout<<p<<" "<<q<<endl; tminus[p+step] += d; tminus[q-step] += d; if(tminus[p+step] > 0){ tplus[p+step] += tminus[p+step]; tminus[p+step] = 0; } if(tminus[q-step] > 0){ tplus[q-step] += tminus[q-step]; tminus[q-step] = 0; } if(tplus[p] == 0) tplus.erase(p); if(tplus[q] == 0) tplus.erase(q); if(tminus[p+step] == 0) tminus.erase(p+step); if(tminus[q-step] == 0) tminus.erase(q-step); p = find_next(0,false); q = find_next(find_next(p,true),false); //if(p == prevP && q == prevQ) //q = find_next(q,false); if(p == prevP && q == prevQ) break; } //cout<<p<<"____________"<<q<<endl; /*auto nxtIt = tminus.upper_bound(p); auto preIt = tminus.lower_bound(q); if(preIt != tminus.begin()) preIt--; int nxt = (*nxtIt).first; int pre = (*preIt).first; if(nxtIt == tminus.end()) nxt = p; if(preIt == tminus.begin()) pre = q;*/ // cout<<nxt<<"__"<<pre<<endl; /*for(int i=1;i<=maxA;i++) if(tplus[i] != 0 || tminus[i] != 0) cout<<i<<" "<<tplus[i]<<" "<<tminus[i]<<endl;*/ for(auto temp : tplus){ if(temp.second != 0) return 0; } for(auto temp : tminus){ if(temp.second != 0) return 0; } return 1; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>t; for(int i=0;i<t;i++){ if(test()) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; } return 0; } |