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
#include <stdio.h>
#include<stdlib.h>

typedef struct el {
    int x1;
    int y1;
    int x2;
    int y2;
    int i;
} samochod;

typedef struct el2 {
    struct el2 *next;
    struct el2 *prev;
    int x;
    int h;
} element;

element *start=NULL;
element *curr=NULL;

void add(int x, int h, element *start_ptr) {
    element *tmp_el;
    element *new_el;
    //new_el = (element*)malloc(sizeof(element));
    
    tmp_el=start_ptr;
    while(tmp_el!=NULL && tmp_el->x<x && tmp_el->next!=NULL) {
	tmp_el = tmp_el->next;
    }
    
    if(tmp_el!=NULL && tmp_el->x==x) {
	tmp_el->h+=h;
	return 0;
    }

    new_el = (element*)malloc(sizeof(element));
    new_el->x=x;
    new_el->h=h;
    new_el->next=NULL;
    new_el->prev=NULL;

    if(start==NULL) {
	start = new_el;
	curr = new_el;
	return;
    }
}

int cmp(const void * a, const void * b)
{
   if( (*(samochod*)a).x1 -(*(samochod*)b).x1 == 0) {
    return ( (*(samochod*)a).i - (*(samochod*)b).i );
    //return ( ( (*(samochod*)a).x2 - (*(samochod*)a).x1 ) - ( (*(samochod*)b).x2 - (*(samochod*)b).x1 ));
   } else {
   return ( (*(samochod*)a).x1 - (*(samochod*)b).x1 );
   }
}

int sprawdz(samochod a, samochod b) {
//printf("por %d %d\n",a.i,b.i);
return (a.x1 < b.x2 && a.x2 > b.x1 && a.y1 < b.y2 && a.y2 > b.y1);
}

samochod par[50000],plan[50000];
int sort[50000];

main() {
int t,n,w,x1,y1,x2,y2,i,j,tak,ii,t1,t2,wys;

scanf("%d",&t);

while(t--) {
scanf("%d %d",&n,&w);
for(i=0;i<n;i++) {
scanf("%d %d %d %d",&par[i].x1,&par[i].y1,&par[i].x2,&par[i].y2);
if(par[i].x1>par[i].x2) {
    t1=par[i].x1;
    t2=par[i].y1;
    par[i].x1=par[i].x2;
    par[i].y1=par[i].y2;
    par[i].x2=t1;
    par[i].y2=t2;
}
par[i].i=i;
}
for(i=0;i<n;i++) {
scanf("%d %d %d %d",&plan[i].x1,&plan[i].y1,&plan[i].x2,&plan[i].y2);
if(plan[i].x1>plan[i].x2) {
    t1=plan[i].x1;
    t2=plan[i].y1;
    plan[i].x1=plan[i].x2;
    plan[i].y1=plan[i].y2;
    plan[i].x2=t1;
    plan[i].y2=t2;
}
plan[i].i=i;
}

//for(i=0;i<n;i++)printf("%d ",par[i].i);
//printf("\n");

qsort(par,n,sizeof(samochod),cmp);
//for(i=0;i<n;i++)printf("%d ",par[i].i);
//printf("\n");
qsort(plan,n,sizeof(samochod),cmp);
//for(i=0;i<n;i++)printf("%d ",plan[i].i);
//printf("\n");

//sprawdzenie planu
tak=0;
for(i=0;i<n&&tak==0;i++) {
for(j=i+1;j<n&&tak==0;j++) {
tak=sprawdz(plan[i],plan[j]);
}
}

if(tak==0) {
//przepychanie
for(i=0;i<n;i++) {
sort[par[i].i]=i;
}
//for(i=0;i<n;i++)printf("%d ",sort[i]);
//printf(" -sort\n");

for(i=0;i<n&&tak==0;i++) {
//printf("poz %d numer %d na pozycje %d\n",i,plan[i].i,sort[plan[i].i]);
if(i<sort[plan[i].i]) {
for(j=i+1;j<=sort[plan[i].i]&&tak==0;j++) {
//printf("zamieniam %d %d\n",i,j);

if((abs(plan[i].y2-plan[i].y1)+abs(plan[j].y2-plan[j].y1))>w)tak=1;
}
}
}
}

if(tak) {
    printf("NIE\n");
} else {
    printf("TAK\n");
}
}


}