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
#include <cstdio>
#include <algorithm>
#define scanf(args...) (scanf(args) ? : 0)
const int MAXN = 1 << 16;

struct Car {
    int x1, x2, height, sortId;
} car[MAXN];

bool cmp1(const Car& c1, const Car& c2) {
    return c1.x1 < c2.x1;
}

bool cmp2(const Car& c1, const Car& c2) {
    return c1.x2 < c2.x2;
}

int tree[2*MAXN];

void set(int where, int val) {
    where += MAXN+1;
    tree[where] = val;
    while (where /= 2)
        tree[where] = std::max(tree[2*where], tree[2*where+1]);
}

int query(int a, int b) {
    if (a > b)
        return 0;

    a += MAXN, b += MAXN+2;
    
    int res = 0;
    while (a/2 != b/2) {
        if (a%2 == 0)
            res = std::max(tree[a+1], res);
        if (b%2 == 1)
            res = std::max(tree[b-1], res);
        a /= 2, b /= 2;
    }
    return res;
}

int main() {
    int t;
    scanf("%d", &t);

    while (t--) {
        std::fill(tree, tree+2*MAXN, 0);

        int n, w;
        scanf("%d%d", &n, &w);

        for (int i=0; i<n; i++) {
            int x1, y1, x2, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            car[i].x1 = std::min(x1, x2);
            car[i].height = std::abs(y2-y1);
        }

        for (int i=0; i<n; i++) {
            int x1, x2;
            scanf("%d%*d%d%*d", &x1, &x2);
            car[i].x2 = std::min(x1, x2);
        }

        std::sort(car, car+n, cmp1);
        for (int i=0; i<n; i++) {
            car[i].sortId = i;
            set(i, car[i].height);
        }
        
        bool success = true;
        std::sort(car, car+n, cmp2);
        for (int i=0; i<n && success; i++) {
            if (query(0, car[i].sortId-1)+car[i].height > w)
                success = false;
            set(car[i].sortId, 0);
        }

        printf("%s\n", success ? "TAK" : "NIE");
    }
}