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
// par.cpp : Defines the entry point for the console application.
//



#include "stdio.h"
#include "stdlib.h"

#define	 NN	50000
#define	 MM	65336
#define	 LL	17

struct	ppp	{
				int	 x0, x1, y0, y1;
				int	 dy;
			} ;

struct	ppp		*pi;

int
compii(const void *x, const void *y)
{
	int		 ix, iy, r;

	ix	= *((int *) x);
	iy	= *((int *) y);

	r	= pi[ix].x0 - pi[iy].x0;
	if (r==0)
		r	= pi[ix].y0 - pi[iy].y0;;

	return r;
}

struct	uuu	{
				int			 n;				// liczba obiektów
				int			 w;				// wysokość 
				int			 k;				// liczba obiektów w kk
				int			 w2;			// wysokość / 2

				struct ppp	 pp[NN];		// pozycja pojazdu
				int			 ii[NN];		// lista numerów pojazdów posortowana względem x0, y0
				int			 kk[NN];		// lista powyżej z filtrem: dy > w2
				int			 oo[NN];		// lista odwrotna do ii

				int			 l;
				int			*ll[LL+1];		
				int			 mm[MM+MM];
			} ;


void
Init(struct uuu *u, int n, int w)
{
	int		 i, p;

	u->n	= n;
	u->w	= w;
	u->w2	= w / 2;

	p	= 1;
	for (i=1; i<=LL; i++)
	{
		if (n>=p)
			u->l	= i +1;

		u->ll[i]	= &(u->mm[p]);
		p	+= p;
	}
}

void
Read(struct uuu *u)
{
	int			 i, j, k, l, x0, x1, y0, y1, x, y, n, w2;
	struct ppp	*pp;
	int			*ii, *kk, *oo, *ll, *jj;

	pp	= u->pp;
	ii	= u->ii;
	kk	= u->kk;

	n	= u->n;

	for (i=0; i<n; i++)
	{
		scanf("%d%d%d%d", &x0, &y0, &x1, &y1);

		if (x0>x1)
			{	x	= x0;	x0	= x1;	x1	= x; }

		if (y0>y1)
			{	y	= y0;	y0	= y1;	y1	= y; }

		pp[i].x0	= x0;
		pp[i].y0	= y0;
		pp[i].x1	= x1;
		pp[i].y1	= y1;
		pp[i].dy	= y1 - y0;
		ii[i]		= i;
	}

	pi	= pp;
	qsort(ii, n, sizeof(int), compii);

	w2	= u->w2;
	k	= 0;
	for (i=0; i<n; i++)
		if (pp[ii[i]].dy>w2)
			kk[k++]	= ii[i];

	u->k	= k;

	l	= u->l;
	ll	= u->ll[l];
	oo	= u->oo;
	for (i=0; i<n; i++)
	{
		k		= ii[i];
		ll[k]	= pp[i].dy;
		oo[k]	= i;
	}

	if (n%2)
		ll[n]	= 0;

	j	= l;
	k	= n;
	while (--j)
	{
		jj	= u->ll[j];
		k	= (k + 1) / 2;
		for (i=0; i<k; i++)
			jj[i]	= ll[i+i] >= ll[i+i+1] ? ll[i+i] : ll[i+i+1];

		if ((k%2) && j>1)
			jj[k]	= 0;

		ll	= jj;
	}

}

int
SzukajWLewo(struct uuu *u, int p)
{
	int		 i, j, l, *ll, dy, dw, r;

	l	= u->l;
	ll	= u->ll[l];
	
	j	= u->oo[p];
	dy	= ll[j];
	dw	= u->w - dy;
	r	= -1;

	for (i=j-1; i>=0; i--)
		if (ll[i]>dw)
		{
			r	= i;
			i	= -1;
		}

	return r;
}


struct	uuu		 u1, u2;



void
par()
{
	int		 i, j, k, n, w, w2;

	scanf("%d%d", &n, &w);

	w2	= w / 2;

	Init(&u1, n, w);
	Init(&u2, n, w);

	Read(&u1);
	Read(&u2);

	k	= u1.k;
	if (k!=u2.k)
		k	= -1;
	else
	{
		for (i=0; i<k; i++)
			if (u1.kk[i]!=u2.kk[i])
				k	= -1;
	}

	if (k>=0)
	{
		for (i=0; i<n; i++)
			if (u1.pp[i].dy<=w2)
				if (SzukajWLewo(&u1, i)!=SzukajWLewo(&u2, i))
				{
					i	= n;
					k	= -1;
				}
	}

	printf("%s\n", (k<0) ? "NIE" : "TAK");
}

int
main()
{
	int		 t;

	scanf("%d", &t);
	while (t--)
		par();

	return 0;
}