#include <iostream> using namespace std; int pocz1[50001],idk1[50001],wys1[50001]; int pocz2[50001],idk2[50001]; int ind1[50001],ind2[50001]; int reind[50001]; void qs( int tab[],int tab2[],int tab3[], int left, int right ) { int i = left; int j = right; int x = tab[( left + right ) / 2 ]; do { while( tab[ i ] < x ) i++; while( tab[ j ] > x ) j--; if( i <= j ) { swap( tab[ i ], tab[ j ] ); swap( tab2[ i ], tab2[ j ] ); swap( tab3[ i ], tab3[ j ] ); i++; j--; } } while( i <= j ); if( left < j ) qs( tab,tab2,tab3, left, j ); if( right > i ) qs( tab,tab2,tab3, i, right ); } void qs2( int tab[],int tab2[], int left, int right ) { int i = left; int j = right; int x = tab[( left + right ) / 2 ]; do { while( tab[ i ] < x ) i++; while( tab[ j ] > x ) j--; if( i <= j ) { swap( tab[ i ], tab[ j ] ); swap( tab2[ i ], tab2[ j ] ); i++; j--; } } while( i <= j ); if( left < j ) qs2( tab,tab2, left, j ); if( right > i ) qs2( tab,tab2, i, right ); } void fix(int &x1,int &y1,int &x2,int &y2){ int tmp; if(x1>x2){ tmp=x2; x2=x1; x1=tmp; } if(y1>y2){ tmp=y2; y2=y1; y1=tmp; } } int main(){ int t,n,h,tmp,step,hs,last2,first2,last1,first1,max,ind,indtmp,maxind; int i,j,x1,x2,y1,y2; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&h); for(i=0;i<n;i++){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); fix(x1,y1,x2,y2); pocz1[i]=x1; wys1[i]=y2-y1; idk1[i]=i; } for(i=0;i<n;i++){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); fix(x1,y1,x2,y2); pocz2[i]=x1; //ind[i]=y2-y1; idk2[i]=i; } qs(pocz1,idk1,wys1,0,n-1); qs2(pocz2,idk2,0,n-1); /* for(i=0;i<n;i++){ cout<<idk1[i]<<","<<pocz1[i]<<","<<wys1[i]<<endl; } cout<<"----------------------------"<<endl; for(i=0;i<n;i++){ cout<<idk2[i]<<","<<pocz2[i]<<endl; } cout<<"**************************************"<<endl; */ for(i=0;i<n;i++){ reind[idk2[i]]=i; } for(i=0;i<n;i++){ ind1[i]=reind[idk1[i]]; ind2[i]=reind[idk2[i]]; } /* for(i=0;i<n;i++){ cout<<ind1[i]<<endl; } cout<<"====================================================================================================="<<endl; */ // mysle ze jest ok teraz mamy tablice ind1 i wys1 i trzeba je posortowac bool flag=true; // posortuj dwojki for(i=0;i<n;i+=2){ if(i!=n-1){ if(ind1[i]>ind1[i+1]){ // swap if(wys1[i]+wys1[i+1]>h){ flag=false; break; } else{ tmp=ind1[i]; ind1[i]=ind1[i+1]; ind1[i+1]=tmp; // reszta tmp=wys1[i]; wys1[i]=wys1[i+1]; wys1[i+1]=tmp; } } } } // mergesort // cout<<"flag : "<<flag<<endl; for(step=4;step/2<=n;step*=2){ // cout<<"merge: "<<step<<endl; for(i=0;i<n;i+=step){ hs=step/2; if((i+hs>=n)||(flag==false)) break; if(i+step>=n){ last2=n-1; } else{ last2=i+step-1; } first2=i+hs; last1=i+hs-1; first1=i; max=0; // maxind=0; // cout<<"|f1:"<<first1<<"|l1:"<<last1<<"|f2:"<<first2<<"|l2:"<<last2<<endl; indtmp=ind=last2-first2+hs; while((last1>=first1) && (last2>=first2)){ if(ind1[last1]>ind1[last2]){ // cout<<"A"<<endl; // aktualizuj max // maxind = max>wys1[last1]?maxind:last1; max = max>wys1[last1]?max:wys1[last1]; pocz2[ind]=ind1[last1]; // tu indeks idk2[ind--]=wys1[last1--]; // tu wysokosc } else{ // cout<<"B"<<first2<<" "<<last2<<endl; if(max+wys1[last2]>h){ // cout<<"B wykluczenie bo"<<max<<"+"<<wys1[last2]<<">"<<h<<endl; // cout<<"B wykluczenie bo ind"<<maxind<<"."<<ind1[last2]<<" -- "<<h<<endl; flag=false; break; } pocz2[ind]=ind1[last2]; // tu indeks idk2[ind--]=wys1[last2--]; // tu wysokosc } } if(flag==false){ break; } // zmergowane ktoras zostala niedokonczona if(last1>=first1){ for(j=0;j<=ind;j++){ pocz2[j]=ind1[first1+j]; idk2[j]=wys1[first1+j]; } } else{ for(j=0;j<=ind;j++){ if(wys1[first2+j]+max>h){ flag=false; break; } pocz2[j]=ind1[first2+j]; idk2[j]=wys1[first2+j]; } } // przepisz do docelowego for(j=0;j<=indtmp;j++){ ind1[i+j]=pocz2[j]; wys1[i+j]=idk2[j]; } } } /* for(i=0;i<n;i++){ cout<<ind1[i]<<endl; } cout<<"====================================================================================================="<<flag<<endl; */ if(flag)printf("TAK\n"); else printf("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 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 228 229 230 | #include <iostream> using namespace std; int pocz1[50001],idk1[50001],wys1[50001]; int pocz2[50001],idk2[50001]; int ind1[50001],ind2[50001]; int reind[50001]; void qs( int tab[],int tab2[],int tab3[], int left, int right ) { int i = left; int j = right; int x = tab[( left + right ) / 2 ]; do { while( tab[ i ] < x ) i++; while( tab[ j ] > x ) j--; if( i <= j ) { swap( tab[ i ], tab[ j ] ); swap( tab2[ i ], tab2[ j ] ); swap( tab3[ i ], tab3[ j ] ); i++; j--; } } while( i <= j ); if( left < j ) qs( tab,tab2,tab3, left, j ); if( right > i ) qs( tab,tab2,tab3, i, right ); } void qs2( int tab[],int tab2[], int left, int right ) { int i = left; int j = right; int x = tab[( left + right ) / 2 ]; do { while( tab[ i ] < x ) i++; while( tab[ j ] > x ) j--; if( i <= j ) { swap( tab[ i ], tab[ j ] ); swap( tab2[ i ], tab2[ j ] ); i++; j--; } } while( i <= j ); if( left < j ) qs2( tab,tab2, left, j ); if( right > i ) qs2( tab,tab2, i, right ); } void fix(int &x1,int &y1,int &x2,int &y2){ int tmp; if(x1>x2){ tmp=x2; x2=x1; x1=tmp; } if(y1>y2){ tmp=y2; y2=y1; y1=tmp; } } int main(){ int t,n,h,tmp,step,hs,last2,first2,last1,first1,max,ind,indtmp,maxind; int i,j,x1,x2,y1,y2; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&h); for(i=0;i<n;i++){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); fix(x1,y1,x2,y2); pocz1[i]=x1; wys1[i]=y2-y1; idk1[i]=i; } for(i=0;i<n;i++){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); fix(x1,y1,x2,y2); pocz2[i]=x1; //ind[i]=y2-y1; idk2[i]=i; } qs(pocz1,idk1,wys1,0,n-1); qs2(pocz2,idk2,0,n-1); /* for(i=0;i<n;i++){ cout<<idk1[i]<<","<<pocz1[i]<<","<<wys1[i]<<endl; } cout<<"----------------------------"<<endl; for(i=0;i<n;i++){ cout<<idk2[i]<<","<<pocz2[i]<<endl; } cout<<"**************************************"<<endl; */ for(i=0;i<n;i++){ reind[idk2[i]]=i; } for(i=0;i<n;i++){ ind1[i]=reind[idk1[i]]; ind2[i]=reind[idk2[i]]; } /* for(i=0;i<n;i++){ cout<<ind1[i]<<endl; } cout<<"====================================================================================================="<<endl; */ // mysle ze jest ok teraz mamy tablice ind1 i wys1 i trzeba je posortowac bool flag=true; // posortuj dwojki for(i=0;i<n;i+=2){ if(i!=n-1){ if(ind1[i]>ind1[i+1]){ // swap if(wys1[i]+wys1[i+1]>h){ flag=false; break; } else{ tmp=ind1[i]; ind1[i]=ind1[i+1]; ind1[i+1]=tmp; // reszta tmp=wys1[i]; wys1[i]=wys1[i+1]; wys1[i+1]=tmp; } } } } // mergesort // cout<<"flag : "<<flag<<endl; for(step=4;step/2<=n;step*=2){ // cout<<"merge: "<<step<<endl; for(i=0;i<n;i+=step){ hs=step/2; if((i+hs>=n)||(flag==false)) break; if(i+step>=n){ last2=n-1; } else{ last2=i+step-1; } first2=i+hs; last1=i+hs-1; first1=i; max=0; // maxind=0; // cout<<"|f1:"<<first1<<"|l1:"<<last1<<"|f2:"<<first2<<"|l2:"<<last2<<endl; indtmp=ind=last2-first2+hs; while((last1>=first1) && (last2>=first2)){ if(ind1[last1]>ind1[last2]){ // cout<<"A"<<endl; // aktualizuj max // maxind = max>wys1[last1]?maxind:last1; max = max>wys1[last1]?max:wys1[last1]; pocz2[ind]=ind1[last1]; // tu indeks idk2[ind--]=wys1[last1--]; // tu wysokosc } else{ // cout<<"B"<<first2<<" "<<last2<<endl; if(max+wys1[last2]>h){ // cout<<"B wykluczenie bo"<<max<<"+"<<wys1[last2]<<">"<<h<<endl; // cout<<"B wykluczenie bo ind"<<maxind<<"."<<ind1[last2]<<" -- "<<h<<endl; flag=false; break; } pocz2[ind]=ind1[last2]; // tu indeks idk2[ind--]=wys1[last2--]; // tu wysokosc } } if(flag==false){ break; } // zmergowane ktoras zostala niedokonczona if(last1>=first1){ for(j=0;j<=ind;j++){ pocz2[j]=ind1[first1+j]; idk2[j]=wys1[first1+j]; } } else{ for(j=0;j<=ind;j++){ if(wys1[first2+j]+max>h){ flag=false; break; } pocz2[j]=ind1[first2+j]; idk2[j]=wys1[first2+j]; } } // przepisz do docelowego for(j=0;j<=indtmp;j++){ ind1[i+j]=pocz2[j]; wys1[i+j]=idk2[j]; } } } /* for(i=0;i<n;i++){ cout<<ind1[i]<<endl; } cout<<"====================================================================================================="<<flag<<endl; */ if(flag)printf("TAK\n"); else printf("NIE\n"); } } |