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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <climits>

using namespace std;

typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PII;

#define FOR(x, b, e) for (int x = b; x <= (e); ++x)
#define FORD(x, b, e) for (int x = b; x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair

#define INF 1000000001

struct Interval {
    int low;    // Lewy koniec przedziału
    int high;    // Prawy koniec przedziału
    
    // Sortuj przedziały wg. ich końcy
    bool operator < ( const Interval& i ) const {
        return low < i.low;
    }
    
    bool operator == ( const Interval& i ) const {
        return low == i.low && high == i.high;
    }
    
    Interval(int l = 0, int h = 0) : low(l), high(h) { }
};

struct IntervalNode {
    char color;        // Kolor węzła 'r' (czerwony) lub 'b' (czarny)
    Interval key;    // Klucz
    
    int max;        // Największa wartość końca przedziału;
    
    IntervalNode* parent;        // Ojciec
    IntervalNode* children[2];    // Dzieci 0-lewy 1-prawy
};

struct RBTree {
    IntervalNode* root;
    IntervalNode* sentinel;
    
    RBTree() {
        sentinel = new IntervalNode();
        sentinel->parent      = sentinel;
        sentinel->children[0] = sentinel;
        sentinel->children[1] = sentinel;
        sentinel->color       = 'b';
        
        root = sentinel;
    }
    
    ~RBTree() {
        clear_tree();
        delete sentinel;
    }
    
    int other_child( int c ) { return c == 0 ? 1 : 0; }
    
    void delete_tree( IntervalNode* x ) {
        if ( x == sentinel ) 
            return;
        
        delete_tree(x->children[0]);
        delete_tree(x->children[1]);
        delete x;
    }
    
    void clear_tree() {
        delete_tree(root);
        root = sentinel;
    }
    
    int get_max( IntervalNode* x ) {
        int i0 = x->key.high;
        int i1 = (x->children[0] != sentinel ? x->children[0]->max : INT_MIN);
        int i2 = (x->children[1] != sentinel ? x->children[1]->max : INT_MIN);
        return max(i0, max(i1, i2));
    }
    
    void rotate( int l, IntervalNode* x ) {
        int r = other_child(l);
        
        //printf("%s_rotate ", l==0 ? "left" : "right");
        IntervalNode* y        = x->children[r];
        x->children[r]         = y->children[l];
        y->children[l]->parent = x;
        y->parent              = x->parent;
        
        if ( x->parent == sentinel )
            root = y;
        else
            x->parent->children[x == x->parent->children[l] ? l : r] = y;
        
        y->children[l] = x;
        x->parent      = y;
        
        x->max = get_max(x);
        y->max = get_max(y);
    }
    
    void insert_fixup( IntervalNode* z ) {
        //printf("insert_fixup ");
        IntervalNode* y;
        int l, r;
        
        while ( z->parent->color == 'r' ) {
            l = (z->parent == z->parent->parent->children[0] ? 0 : 1);
            r = other_child(l);
            
            //printf("%s: ", l == 0 ? "left" : "right");
            y = z->parent->parent->children[r];
            
            if ( y->color == 'r' ) {    // Przypadek 1
                //printf("case_1 ");
                z->parent->color = 'b';
                y->color = 'b';
                z->parent->parent->color ='r';
                z = z->parent->parent;
            } else {
                if ( z == z->parent->children[r] ) {    // Przypadek 2
                    //printf("case_2 ");
                    z = z->parent;
                    rotate(l, z);
                }
                
                // Przypadek 3
                //printf("case_3 ");
                z->parent->color         = 'b';
                z->parent->parent->color = 'r';
                rotate(r, z->parent->parent);
            }
        }
        
        root->color = 'b';
        //printf("\n");
    }
    
    void insert( IntervalNode* z ) {
        //printf("insert\n");
        IntervalNode* y = sentinel;
        IntervalNode* x = root;
        
        // Nie dopuszczamy powtórzeń w drzewie
        while ( x != sentinel ) {
            y = x;
            
            if ( z->key == x->key ) { // Element o takim kluczu już jest w drzewie
                delete z;
                return;
            }
            
            if ( x->max < z->max )    // uaktualnianie pola max
                x->max = z->max;
            
            x = x->children[z->key < x->key ? 0 : 1];
        }
        
        z->parent = y;
        
        if ( y == sentinel )
            root = z;
        else
            y->children[z->key < y->key ? 0 : 1] = z;
        
        z->children[0] = sentinel;
        z->children[1] = sentinel;
        z->color       = 'r';
        
        insert_fixup(z);
    }
    
    void insert( const Interval& i ) {
        IntervalNode* z = new IntervalNode();
        z->key = i;
        z->max = i.high;
        
        insert(z);
    }
    
    void remove_fixup( IntervalNode* x ) {
        //printf("remove_fixup ");
        IntervalNode* w;
        int l, r;
        
        while ( x != root && x->color == 'b' ) {
            l = (x == x->parent->children[0] ? 0 : 1);
            r = other_child(l);
            
            //printf("%s: ", l == 0 ? "left" : "right");
            w = x->parent->children[r];
            
            if ( w->color == 'r' ) {    // Przypadek 1
                //printf("case_1 ");
                w->color         = 'b';
                x->parent->color = 'r';
                rotate(l, x->parent);
                w = x->parent->children[r];
            }
            
            // Przypadek 2
            if ( w->children[l]->color == 'b' && w->children[r]->color == 'b' ) {
                //printf("case_2 ");
                w->color = 'r';
                x = x->parent;
            } else {
                if ( w->children[r]->color == 'b' ) {    // Przypadek 3
                    //printf("case_3 ");
                    w->children[l]->color = 'b';
                    w->color       = 'r';
                    rotate(r, w);
                    w = x->parent->children[r];
                }
                
                // Przypadek 4
                //printf("case_4 ");
                w->color               = x->parent->color;
                x->parent->color       = 'b';
                w->children[r]->color  = 'b';
                rotate(l, x->parent);
                x = root;
            }
        }
        
        x->color = 'b';
        //printf("\n");
    }
    
    IntervalNode* remove( IntervalNode* z ) {
        IntervalNode* x;
        IntervalNode* y;
        
        if ( z->children[0] == sentinel || z->children[0] == sentinel )
            y = z;
        else
            y = successor(z);
            
        x = y->children[y->children[0] != sentinel ? 0 : 1];
        x->parent = y->parent;
        
        if ( y->parent == sentinel )
            root = x;
        else
            y->parent->children[y == y->parent->children[0] ? 0 : 1] = x;
    
        if ( y != z ) {
            // Przekopiuj dane z y-ka do z-ta
            z->key = y->key;
        }
        
        IntervalNode* w = y->parent;
        while ( w != sentinel ) {
            w->max = get_max(w);
            w = w->parent;
        }
        
        if ( y->color == 'b' )
            remove_fixup(x);
        
        return y;
    }
    
    // Usuń klucz k
    void remove( const Interval& i ) {
        IntervalNode* z = find(i);
        if ( z == sentinel )
            return;
            
        IntervalNode* del = remove(z);
        if ( del != sentinel )
            delete del;
    }
    
    bool check_car(int i, int h, int w) {
        IntervalNode* x = root;

        while (x != sentinel) {
            if (w - x->key.high >= h) return true;

            IntervalNode* l = x->children[0];
            IntervalNode* r = x->children[1];

            if (x->key.low <= i && l != sentinel && w - l->key.high < h)
                return false;
            
            if (x->key.low > i)
                x = l;
            else if (x->key.low < i)
                x = r;
            else
                return w - x->key.high >= h;
        }

        return true;
    }
    
    // Znajdź węzeł o kluczu k
    IntervalNode* find( const Interval& i ) {
        IntervalNode* x = root;
        
        while ( x != sentinel ) {
            if ( x->key == i )
                break;
            else
                x = x->children[i < x->key ? 0 : 1];
        }
        
        //if ( x != sentinel )
        //    printf("Looking for (%d, %d) found (%d, %d)\n", 
        //           i.low, i.high, x->key.low, x->key.high);
            
        return x;
    }
    
    // Węzeł, którego klucz jest pierwszym kluczem więszym od klucza x
    // Węzeł y w drzewie, tż y->key == min(z->key > x->key)
    IntervalNode* successor( IntervalNode* x ) {
        if ( x->children[1] != sentinel )
            return minT(x->children[1]);
        else {
            IntervalNode* y = x->parent;
            while ( y != sentinel && x == y->children[1] ) {
                x = y;
                y = y->parent;
            }
            
            return y;
        }
    }
    
    // Węzeł którego klucz jest pierwszym kluczem mniejszym od klucza x
    // Węzeł y w drzewie, tż y->key == max(z->key < x->key)
    IntervalNode* predecesor( IntervalNode* x ) {
        if ( x->children[0] != sentinel )
            return maxT(x->children[0]);
        else {
            IntervalNode* y = x->parent;
            while ( y != sentinel && x == y->children[0] ) {
                x = y;
                y = y->parent;
            }
            
            return y;
        }
    }
    
    // Minimalny element w drzewie o korzeniu r
    IntervalNode* minT( IntervalNode* r ) {
        IntervalNode* x = r->parent;
        
        while ( r != sentinel ) {
            x = r;
            r = r->children[0];
        }
        
        return x;
    }
    
    IntervalNode* minT() { return minT(root); }
    
    // Maksymalny element w drzewie o korzeniu r
    IntervalNode* maxT( IntervalNode* r ) {
        IntervalNode* x = r->parent;
        
        while ( r != sentinel ) {
            x = r;
            r = r->children[1];
        }
        
        return x;
    }
    
    IntervalNode* maxT() { return maxT(root); }
    
    void print_tree() {
        print_node(root);
        printf("\n");
    }
    
    void print_node( IntervalNode* x ) {
        if ( x == sentinel ) 
            return;
            
        print_node(x->children[0]);
        printf("(%d, %d, max = %d) ", x->key.low, x->key.high, x->max);
        print_node(x->children[1]);
    }
};

struct Car {
    int w, h;
    int xs, ys;
    int xd, yd;
    int is;

    Car(int x1 = 0, int x2 = 0, int y1 = 0, int y2 = 0) {
        w = abs(x1 - x2);
        h = abs(y1 - y2);
        xs = min(x1, x2);
        ys = min(y1, y2);
    }

    void AddDest(int x1, int x2, int y1, int y2) {
        xd = min(x1, x2);
        yd = min(y1, y2);
    }
};

bool SrcCmp(const Car& c1, const Car& c2) {
    return c1.xs == c2.xs ? c1.ys < c2.ys : c1.xs < c2.xs;
}

bool DstCmp(const Car& c1, const Car& c2) {
    return c1.xd == c2.xd ? c1.yd < c2.yd : c1.xd < c2.xd;
}

int main()
{
    int t, n, w, x1, x2, y1, y2;
    for (scanf("%d", &t); t > 0; --t) {
        scanf("%d %d\n", &n, &w);
        vector<Car> v;
        REP(i, n) {
            scanf("%d %d %d %d\n", &x1, &y1, &x2, &y2);
            v.PB(Car(x1, x2, y1, y2));
        }

        REP(i, n) {
            scanf("%d %d %d %d\n", &x1, &y1, &x2, &y2);
            v[i].AddDest(x1, x2, y1, y2);
        }

        RBTree T;
        sort(ALL(v), SrcCmp);
        REP(i, n) {
            v[i].is = i;
            T.insert(Interval(i, v[i].h));
        }

        //T.print_tree();

        bool res = true;
        sort(ALL(v), DstCmp);
        REP(i, n) {
            res = T.check_car(v[i].is - 1, v[i].h, w);
            if (!res) break;
            T.remove(Interval(v[i].is, v[i].h));
        }

        printf(res ? "TAK\n" : "NIE\n");
    }

    return 0;
}