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
//Jakub "Cubix651" Cisło
//Zadanie: Parking
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 50005;
int n, w;
int h[MAX], rev[MAX], hrev[MAX], order[MAX], maxH[MAX], tmp[MAX];
pair<int, int> from[MAX], to[MAX];

bool mergeSort(int a, int b)
{
	if(a == b)
		return true;

	int m = (a+b)/2;
	if(!mergeSort(a, m) || !mergeSort(m+1, b))
		return false;

	maxH[m] = hrev[order[m]];
	for(int i = m-1; i >= a; --i)
		maxH[i] = max(maxH[i+1], hrev[order[i]]);

	int x = a, y = m+1, i = 0;
	while(x <= m && y <= b)
	{
		if(order[x] <= order[y])
			tmp[i++] = order[x++];
		else
		{
			if(maxH[x] + hrev[order[y]] > w)
				return false;
			tmp[i++] = order[y++];
		}
	}
	while(x <= m)
		tmp[i++] = order[x++];
	while(y <= b)
		tmp[i++] = order[y++];
	for(int j = a; j <= b; ++j)
		order[j] = tmp[j-a];

	return true;
}

bool solve()
{
	scanf("%d%d", &n, &w);
	int x1, x2, y1, y2;
	for(int i = 0; i < n; ++i)
	{
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		h[i] = abs(y1-y2);
		from[i] = make_pair(min(x1, x2), i);
	}
	for(int i = 0; i < n; ++i)
	{
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		to[i] = make_pair(min(x1, x2), i);
	}
	sort(from, from + n);
	sort(to, to + n);
	for(int i = 0; i < n; ++i)
	{
		rev[from[i].second] = i;
		hrev[i] = h[from[i].second];
	}
	for(int i = 0; i < n; ++i)
		order[i] = rev[to[i].second];
	/*for(int i = 0; i < n; ++i)
		printf("%d : %d\n", order[i], hrev[order[i]]);*/

	return mergeSort(0, n-1);
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
		printf(solve()?"TAK\n":"NIE\n");
	return 0;
}