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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct point
{
	int dx, dy, gx, gy;
	int id;

	point(int a, int b, int c, int d, int e)
	{
		this -> dx = a;
		this -> dy = b;
		this -> gx = c;
		this -> gy = d;
		this -> id = e;
	}
};

struct height
{
	int h;
	int id;
	int ord;
	height(int a, int b, int c = 0)
	{
		this -> h = a;
		this -> id = b;
		this -> ord = c;
	}
};

bool comp(point a, point b)
{
	if (a.dx == b.dx)
	{
		return a.dy < b.dy;
	}
	else
		return a.dx < b.dx;
}

const int MAXN = 50000;

int id[MAXN];
int hei[MAXN];
int w;
vector <int> wys;
vector <point> v1,v2;
vector <height> przed, po;
vector <int> toMerge;

bool merge(vector <int> &a, vector <int> &b, int pocz, int mid, int kon)
{
	int h,i,j,k;
    h=pocz;
    i=pocz;
    j=mid+1;
    //vector <int> b(a.size());
    while((h <= mid)&&(j<=kon))
    {
    //	cout<<"XD "<<a[h]<<" "<<a[j]<<endl;
    //	cout<<"lel "<<hei[a[h]]<<" "<<hei[a[j]]<<endl;
        if(a[h] <= a[j])
        {
        	
            b[i]=a[h];
            h++;
        }
        else
        {
        	if(hei[a[h]] + hei[a[j]] > w)
            	{
    //        		cout<<hei[a[h]]<<" "<<hei[a[j]]<<" "<<w<<endl;
            		return false;
            	}
            b[i]=a[j];
            
            j++;
        }
        i++;
    }
    if(h>mid)
    {
        for(k=j; k<=kon; k++)
        {
            b[i]=a[k];
            i++;
        }
    }
    else
    {
        for(k=h; k<=mid; k++)
        {
            b[i]=a[k];
            i++;
        }
    }
    for(k=pocz; k<=kon; k++) a[k]=b[k];
    return true;
}
bool wyn;
int merge_sort(vector <int> &v, vector<int> &b, int start, int koniec)
{
	int srodek;
 	//int start = 0;
 	//int koniec = toMerge.size();
	if (start < koniec)
	{
		srodek = (start + koniec)/2;
		merge_sort(v,b, start, srodek);
		merge_sort(v,b, srodek+1, koniec);
		bool res = merge(v,b, start, srodek, koniec);
		if(!res)
		{
			wyn = false;
			//cout<<"NIE"<<endl;
			//return 0;
		}
		//else
			//cout<<"TAK"<<endl;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int t;
	cin>>t;
	for(int q = 0 ; q < t; ++q)
	{
		int n;
		cin>> n >> w;
		wyn = true;
		for(int i = 0 ; i<n; ++i)
		{
			int x1,y1,x2,y2;
			cin>>x1>>y1>>x2>>y2;
			v1.push_back(point(x1, y1, x2, y2, i+1));
		}
		for(int i = 0 ; i<n; ++i)
		{
			int x1,y1,x2,y2;
			cin>>x1>>y1>>x2>>y2;
			v2.push_back(point(x1, y1, x2, y2, i+1));
		}
		sort(v1.begin(), v1.end(), comp);
		sort(v2.begin(), v2.end(), comp);
		for(int j = 0; j < v1.size(); ++j)
		{
			przed.push_back(height(abs(v1[j].gy - v1[j].dy), v1[j].id, j+1));
			id[v1[j].id] = j+1;
			hei[j+1] = abs(v1[j].gy - v1[j].dy);
			//cout<<v1[j].gy - v1[j].dy<<" "<< v1[j].id<<endl;
			po.push_back(height(abs(v2[j].gy - v2[j].dy), v2[j].id));
		//	cout<<abs(v2[j].gy - v2[j].dy)<<" "<< v2[j].id<<endl;
		}
		for(int i = 0; i<po.size(); ++i)
		{
			po[i].ord = id[po[i].id];
			toMerge.push_back(po[i].ord);
		}
		
		vector <int> b(toMerge.size()-1);
		merge_sort(toMerge,b,0,toMerge.size()-1);
	/*	for(int i = 0; i< toMerge.size(); ++i)
		{
			cout<<toMerge[i]<<" ";
		}*/
		b.clear();
		toMerge.clear();
		po.clear();
		przed.clear();
		v1.clear();
		v2.clear();
		if(!wyn)
			cout<<"NIE"<<endl;
		else
			cout<<"TAK"<<endl;
	}
}