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

using namespace std;

struct S
{
    S() {}
    int x1, y1, w;
    int x2, y2;
    int wyj_pos, doc_pos;
};

vector<S> v;
vector<int> wyj, doc;

int np2(int n)
{
    int b = 1;
    --n;
    while(n)
    {
        n>>=1;
        b<<=1;
    }
    return b;
}

class Tree
{
    public:
    Tree();
    int M;
    vector<int> d;
    void zgas(int pos);
    int query(int pos);
};

Tree::Tree()
{
    M = np2(v.size());
    d.resize(2*M+2);
    for(int i = M; i < v.size()+M; ++i)
        d[i] = v[wyj[i-M]].w;
    for(int i = M-1; i >= 1; --i)
        d[i] = max(d[2*i], d[2*i+1]);
}

void Tree::zgas(int pos)
{
    int v = pos+M;
    d[v] = 0;
    while(v != 1)
    {
        v /= 2;
        d[v] = max(d[2*v], d[2*v+1]);
    }
}

int Tree::query(int pos)
{
    --pos;
    if(pos < 0)
        return 0;
    int va = M, vb = M+pos;
    int res = max(d[vb], d[va]);
    while(va/2 != vb/2)
    {
        if(va%2 == 0) res = max(d[va+1], res);
        if(vb%2 == 1) res = max(d[vb-1], res);
        va /= 2;
        vb /= 2;
    }
    return res;
}
//sortuje wyjściowy, czyli po x1, y1
inline bool cmp1(const int& a, const int& b)
{
    if(v[a].x1 == v[b].x1)
        return v[a].y1 < v[b].y1;
    return v[a].x1 < v[b].x1;
}
//sortuje docelowy
inline bool cmp2(const int& a, const int& b)
{
    if(v[a].x2 == v[b].x2)
        return v[a].y2 < v[b].y2;
    return v[a].x2 < v[b].x2;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, w;
        scanf("%d%d", &n, &w);
        v.resize(n);
        for(int i = 0; i < n; ++i)
        {
            int x1, y1, x2, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            v[i].x1 = min(x1, x2);
            v[i].y1 = min(y1, y2);
            v[i].w = abs(y2-y1);
        }
        for(int i = 0; i < n; ++i)
        {
            int x1, y1, x2, y2;
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            v[i].x2 = min(x1, x2);
            v[i].y2 = min(y1, y2);
        }
        wyj.resize(n);
        doc.resize(n);
        for(int i = 0; i < n; ++i)
            wyj[i] = doc[i] = i;

        sort(wyj.begin(), wyj.end(), cmp1);
        sort(doc.begin(), doc.end(), cmp2);

        for(int i = 0; i < n; ++i)
        {
            v[wyj[i]].wyj_pos = i;
            v[doc[i]].doc_pos = i;
        }

        Tree* t = new Tree();
        bool flag = true;
        for(int i = 0; i < n; ++i)
        {
            //v[doc[i]] to ten nasz
            //query na wyjściowym od naszego w dół
            //mp najw wys
            int mp = t->query(v[doc[i]].wyj_pos);
            if(v[doc[i]].w + mp > w)
            {
                flag = false;
                break;
            }
            t->zgas(v[doc[i]].wyj_pos);
        }
        if(flag)
            printf("TAK\n");
        else
            printf("NIE\n");
        delete t;
    }
    return 0;
}