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
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 50010;

int height, numOfCars;

struct rectangle
{
	int xlb, ylb; //left bottom = lb
	int xrt, yrt; //right top = rt
};

bool operator<(const rectangle & a, const rectangle & b)
{
	if(a.xlb != b.xlb)
		return a.xlb < b.xlb;
	return a.ylb < b.ylb;
}

struct whereIs
{
	rectangle begin;
	rectangle end;
	int height;
	int number;
};

int orderBegin[MAXN];
int orderEnd[MAXN];
whereIs setting[MAXN];
int tree[1 << 17 + 5]; // 1 is the heighest one
const int treeHeight = 1 << 16;

void setValue(int where, int value)
{
	where += treeHeight;
	tree[where] = value;
	where >>= 1;
	while(where > 0)
	{
		tree[where] = max(tree[where * 2], tree[where * 2 + 1]);
		where >>= 1;
	}
}

int getMax(int from, int to, int BEGIN = 0, int END = treeHeight - 1, int node = 1)
{
	//printf("%d %d %d %d %d \n", from, to, BEGIN, END , node);
	if(from <= BEGIN && to >= END){
		//printf("R %d\n", tree[node]);
		return tree[node];
	}
	int result = 0;
	int center = (BEGIN + END) / 2;
	if(from <= center)
		result = max(getMax(from, to, BEGIN, center, node * 2), result);
	if(to > center)
		result = max(getMax(from, to, center + 1, END, node * 2 + 1), result);
	//printf("R %d\n", result);
	return result;
}

bool cmpBegin(const whereIs & a, const whereIs & b)
{
	return a.begin < b.begin;
}

bool cmpEnd(const whereIs & a, const whereIs & b)
{
	return a.end < b.end;
}


bool get() 
{
	scanf("%d %d",&numOfCars, &height);
	for(int i = 0; i < numOfCars; ++i){
		scanf("%d %d %d %d", &setting[i].begin.xlb, &setting[i].begin.ylb, &setting[i].begin.xrt, &setting[i].begin.yrt);
	}
	for(int i = 0; i < numOfCars; ++i){
		scanf("%d %d %d %d", &setting[i].end.xlb, &setting[i].end.ylb, &setting[i].end.xrt, &setting[i].end.yrt);
	}
	for(int i = 0; i < numOfCars; ++i){
		setting[i].number = i;
		if(setting[i].begin.xlb > setting[i].begin.xrt)
			swap(setting[i].begin.xlb, setting[i].begin.xrt);
		if(setting[i].end.xlb > setting[i].end.xrt)
			swap(setting[i].end.xlb, setting[i].end.xrt);
		
		if(setting[i].begin.ylb > setting[i].begin.yrt)
			swap(setting[i].begin.ylb, setting[i].begin.yrt);
		if(setting[i].end.ylb > setting[i].end.yrt)
			swap(setting[i].end.ylb, setting[i].end.yrt);
		
		/*if(setting[i].begin.xrt - setting[i].begin.xlb != setting[i].end.xrt - setting[i].end.xlb)
			return true;
		if(setting[i].begin.yrt - setting[i].begin.ylb != setting[i].end.yrt - setting[i].end.ylb)
			return true;*/
		setting[i].height = setting[i].begin.yrt - setting[i].begin.ylb;
	}
	return false;
}

bool perform()
{
	sort(setting, setting + numOfCars, cmpBegin);
	for(int i = 0; i < numOfCars; ++i){
		orderBegin[setting[i].number] = i;
		setValue(i, setting[i].height);
	}
	sort(setting, setting + numOfCars, cmpEnd);
	for(int i = 0; i < numOfCars; ++i){
		orderEnd[setting[i].number] = i;
	}
	//usaw wielkosci
	/*for(int i = 0; i < numOfCars; ++i){
		
		printf("setting[i].number = %d", setting[i].number);
		printf("(0,%d) = %d, (%d, %d) = %d\n", i, getMax(0,i), i, i, getMax(i,i));
	}*/
	
	//dla kazdego sprawdz ile musial pokonac
	int heighestOne;
	for(int i = 0; i < numOfCars; ++i){
		setValue(orderBegin[setting[i].number], 0);
		heighestOne = getMax(0, orderBegin[setting[i].number]);
		//printf("%d %d %d\n",  orderBegin[setting[i].number], heighestOne, setting[i].height);
		if(heighestOne + setting[i].height > height)
			return false;
	}
	return true;
	
}

int main()
{
	/*// Tree test
	setValue(0, 2);
	setValue(4, 3);
	setValue(1, 3);
	setValue(2, 7);
	setValue(3, 4);
	printf("R %d", getMax(0,4));
	printf("R %d", getMax(0,0));
	printf("R %d", getMax(1,3));
	setValue(2,2);
	printf("R %d", getMax(0,4));
	printf("R %d", getMax(0,0));
	printf("R %d", getMax(1,3));*/
	int t;
	scanf("%d", &t);
	while(t--){
		if(get()){
			//printf("NIE\n");
			//continue;
		}	
		if(perform() == true)
			printf("TAK\n");
		else
			printf("NIE\n");
	}
	return 0;
}