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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <cstdio>
#include <utility>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

#if (defined WIN32 || defined WINDOWS || defined _WIN32 || defined _WINDOWS)
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif

int read_int()
{
    register char c = 0;
	int result = 0;
    while (c < 33) c=getchar_unlocked();
    while (c>32) {result=result*10 + (c-'0'); c=getchar_unlocked();}
	return result;
}

void write_int(int x)
{
	if (x >= 10) write_int(x / 10);
	putchar_unlocked('0' + (x % 10));
}

void write_newline()
{
	putchar_unlocked('\n');
}

void write_space()
{
	putchar_unlocked(' ');
}

//----------------------------------

template <class T, typename K = int>
class Interval {
public:
    K start;
    K stop;
    T value;
    Interval(K s, K e, const T& v)
        : start(s)
        , stop(e)
        , value(v)
    { }
};

template <class T, typename K>
int intervalStart(const Interval<T,K>& i) {
    return i.start;
}

template <class T, typename K>
int intervalStop(const Interval<T,K>& i) {
    return i.stop;
}

template <class T, typename K>
ostream& operator<<(ostream& out, Interval<T,K>& i) {
    out << "Interval(" << i.start << ", " << i.stop << "): " << i.value;
    return out;
}

template <class T, typename K = int>
class IntervalStartSorter {
public:
    bool operator() (const Interval<T,K>& a, const Interval<T,K>& b) {
        return a.start < b.start;
    }
};

template <class T, typename K = int>
class IntervalTree {

public:
    typedef Interval<T,K> interval;
    typedef vector<interval> intervalVector;
    typedef IntervalTree<T,K> intervalTree;
    
    intervalVector intervals;
    intervalTree* left;
    intervalTree* right;
    int center;

    IntervalTree<T,K>(void)
        : left(NULL)
        , right(NULL)
        , center(0)
    { }

    IntervalTree<T,K>(const intervalTree& other) {
        center = other.center;
        intervals = other.intervals;
        if (other.left) {
            left = (intervalTree*) malloc(sizeof(intervalTree));
            *left = *other.left;
        } else {
            left = NULL;
        }
        if (other.right) {
            right = new intervalTree();
            *right = *other.right;
        } else {
            right = NULL;
        }
    }

    IntervalTree<T,K>& operator=(const intervalTree& other) {
        center = other.center;
        intervals = other.intervals;
        if (other.left) {
            left = new intervalTree();
            *left = *other.left;
        } else {
            left = NULL;
        }
        if (other.right) {
            right = new intervalTree();
            *right = *other.right;
        } else {
            right = NULL;
        }
        return *this;
    }

    IntervalTree<T,K>(
            intervalVector& ivals,
            unsigned int depth = 16,
            unsigned int minbucket = 64,
            int leftextent = 0,
            int rightextent = 0,
            unsigned int maxbucket = 512
            )
        : left(NULL)
        , right(NULL)
    {

        --depth;
        IntervalStartSorter<T,K> intervalStartSorter;
        if (depth == 0 || (ivals.size() < minbucket && ivals.size() < maxbucket)) {
            sort(ivals.begin(), ivals.end(), intervalStartSorter);
            intervals = ivals;
        } else {
            if (leftextent == 0 && rightextent == 0) {
                // sort intervals by start
                sort(ivals.begin(), ivals.end(), intervalStartSorter);
            }

            int leftp = 0;
            int rightp = 0;
            int centerp = 0;
            
            if (leftextent || rightextent) {
                leftp = leftextent;
                rightp = rightextent;
            } else {
                leftp = ivals.front().start;
                vector<K> stops;
                stops.resize(ivals.size());
                transform(ivals.begin(), ivals.end(), stops.begin(), intervalStop<T,K>);
                rightp = *max_element(stops.begin(), stops.end());
            }

            //centerp = ( leftp + rightp ) / 2;
            centerp = ivals.at(ivals.size() / 2).start;
            center = centerp;

            intervalVector lefts;
            intervalVector rights;

            for (typename intervalVector::iterator i = ivals.begin(); i != ivals.end(); ++i) {
                interval& interval = *i;
                if (interval.stop < center) {
                    lefts.push_back(interval);
                } else if (interval.start > center) {
                    rights.push_back(interval);
                } else {
                    intervals.push_back(interval);
                }
            }

            if (!lefts.empty()) {
                left = new intervalTree(lefts, depth, minbucket, leftp, centerp);
            }
            if (!rights.empty()) {
                right = new intervalTree(rights, depth, minbucket, centerp, rightp);
            }
        }
    }

    void findOverlapping(K start, K stop, intervalVector& overlapping) {
        if (!intervals.empty() && ! (stop < intervals.front().start)) {
            for (typename intervalVector::iterator i = intervals.begin(); i != intervals.end(); ++i) {
                interval& interval = *i;
                if (interval.stop >= start && interval.start <= stop) {
                    overlapping.push_back(interval);
                }
            }
        }

        if (left && start <= center) {
            left->findOverlapping(start, stop, overlapping);
        }

        if (right && stop >= center) {
            right->findOverlapping(start, stop, overlapping);
        }

    }

    void findContained(K start, K stop, intervalVector& contained) {
        if (!intervals.empty() && ! (stop < intervals.front().start)) {
            for (typename intervalVector::iterator i = intervals.begin(); i != intervals.end(); ++i) {
                interval& interval = *i;
                if (interval.start >= start && interval.stop <= stop) {
                    contained.push_back(interval);
                }
            }
        }

        if (left && start <= center) {
            left->findContained(start, stop, contained);
        }

        if (right && stop >= center) {
            right->findContained(start, stop, contained);
        }

    }

    ~IntervalTree(void) {
        // traverse the left and right
        // delete them all the way down
        if (left) {
            delete left;
        }
        if (right) {
            delete right;
        }
    }

};

//----------------------------------





template<class T> T abs(T a, T b)
{
	return a < b ? b-a : a-b;
}

typedef Interval<int> Car;
typedef vector<Car> carVector;
typedef IntervalTree<int> intervalTree;

void load_car_data(int n, vector<Car> &cars)
{
	for (int i = 0; i < n; ++i) {
		long x1 = read_int();
		long y1 = read_int();
		long x2 = read_int();
		long y2 = read_int();
		int start = min(x1, x2);
		int h = abs(y1, y2);
		cars.push_back(Car(start, start, h));
	}
	for (int i = 0; i < n; ++i) {
		long x1 = read_int();
		long y1 = read_int();
		long x2 = read_int();
		long y2 = read_int();
		cars[i].stop = min(x1, x2);
	}
}

bool are_colliding(const Car& a, const Car& b, int& w)
{
//	printf("%d, %d, %d -> %d, %d, %d\n", a.start, a.stop, a.value, b.start, b.stop, b.value);
	int da = a.stop - a.start;
	int db = b.stop - b.start;
	if ((a.value + b.value) <= w)
		return false;
	
	// probably same car
	if (a.start == b.start && a.stop == b.stop && a.value == b.value)
		return false;
	
	if ((a.start < b.start && a.stop < b.stop) ||
		(a.start > b.start && a.stop > b.stop))
			return false;
	
	return true;
}

bool greedyValidator(const vector<Car>& cars, int& n, int& w)
{
	for(size_t i = 0; i < n; ++i)
	{
		for(size_t j = i + 1; j < n; ++j)
		{
			const Car& a = cars[i];
			const Car& b = cars[j];
			if (are_colliding(a, b, w))
			{
				return false;
			}
		}
	}
	return true;
}


bool istValidator(carVector& cars, int& n, int& w)
{
	carVector rights, lefts;
	for(carVector::iterator itCars = cars.begin(); itCars != cars.end(); ++itCars)
	{
		Car& c = *itCars;
		
		if (c.start <= c.stop)
			rights.push_back(Car(c.start, c.stop, c.value));
		else
			lefts.push_back(Car(c.stop, c.start, c.value));
	}
	intervalTree rightsTree(rights);
	intervalTree leftsTree(lefts);
	for(carVector::iterator itCars = cars.begin(); itCars != cars.end(); ++itCars)
	{
		Car& c1_r = *itCars;
		Car c1_n(min(c1_r.start, c1_r.stop), max(c1_r.start, c1_r.stop), c1_r.value);
		
		carVector candidates;
		rightsTree.findOverlapping(c1_n.start, c1_n.stop, candidates);
		for(carVector::iterator it = candidates.begin(); it != candidates.end(); ++it)
		{
			Car& c2_r = *it;
			Car& c2_n = c2_r;
			if (are_colliding(c1_r, c2_n, w))
				return false;
		}
		candidates.clear();
		leftsTree.findOverlapping(c1_n.start, c1_n.stop, candidates);
		for(carVector::iterator it = candidates.begin(); it != candidates.end(); ++it)
		{
			Car& c2_n = *it;
			Car c2_r(c2_n.stop, c2_n.start, c2_n.value);
			if (are_colliding(c1_r, c2_r, w))
				return false;
		}
	}
	return true;
}

int main() {
	int t = read_int();
	for (int testCaseIndex = 0; testCaseIndex < t; ++testCaseIndex) {
		int n = read_int();
		int w = read_int();
		vector<Car> cars;
		load_car_data(n, cars);
		
//				bool found = greedyValidator(cars, n, w);
		bool found = istValidator(cars, n, w);
		printf("%s\n", found ? "TAK" : "NIE");
	}
	return 0;
}