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
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cstdarg>
using namespace std;
#define PB push_back
#define IT iterator
#define MP make_pair
#define EB emplace_back
#define F first
#define S second
#define X first
#define Y second
#define NL printf("\n")
#define SZ(v) ((int)(v).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define FORC(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)
#ifdef DBG
#define W(x) do { printf("%s:%d %s = %d\n", __func__, __LINE__, #x, x); } while (0)
#define DBGprint(format, ...) do { fprintf(stderr, "[%s:%d] " format "\n", __func__, __LINE__, ## __VA_ARGS__); } while (0)
#else
#define W(X) do { } while (0)
#define DBGprint(...) do { } while (0)
#endif

typedef long double LD;
typedef double DL;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LD,LD> PLD;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<PII> VPII;
void memcheck()
{
    #ifdef DBG
    char c[100];
    FILE *p = fopen("/proc/self/status","r");
    while (fgets(c,100,p) )
        if (strncmp("VmPeak",c,6)==0)
            fputs(c, stderr);
        fclose(p);
    #endif
}
//------ /headers --------

struct Tree {
    static const int OFFSET = (1 << 18);
    static const int MAXN = 2 * OFFSET + 10;
    vector<uint> tree;
    vector<uint> used;

    Tree() : tree(MAXN, -1) {
    }
    void clear();
    void add(uint x, uint val);
    // min { val(p) : x(p) > x }
    int min(uint x);
};

void Tree::clear() {
    for (int u : used) {
        while (u > 0) {
            tree[u] = -1;
            u /= 2;
        }
    }
    used.clear();
}

void Tree::add(uint x, uint val) {
    x += OFFSET;
    used.PB(x);
    while (x > 0) {
        tree[x] = ::min(tree[x], val);
        x /= 2;
    }
}

int Tree::min(uint x) {
    x += OFFSET;
    uint result = tree[x];
    while (x > 0) {
        if ((x & 1) == 0) {
            // if (x % 2 == 0)
            result = ::min(result, tree[x + 1]);
        }
        x /= 2;
    }
    return result;
}

struct Rect {
    int x1, x2, y1, y2;
};

struct Car {
    Rect init, target;
    uint orig_w;
    int id;
};

void ensure_greater(int& x1, int& x2) {
    if (x1 < x2) {
        swap(x1, x2);
    }
}

const int MAXN = 50020;
int tests;
Tree tree;
int cars, w;
Car car[MAXN];
VI coords_x, coords_y;

void print_car(int i) {
    DBGprint("car %d: [%d]  (%d, %d, %d, %d) -> (%d, %d, %d, %d)", car[i].id, car[i].orig_w,
             car[i].init.x1, car[i].init.y1, car[i].init.x2, car[i].init.y2,
             car[i].target.x1, car[i].target.y1, car[i].target.x2, car[i].target.y2);
}

void read_cars() {
    for (int i = 0; i < cars; ++i) {
        scanf("%d%d%d%d", &car[i].init.x1, &car[i].init.y1, &car[i].init.x2, &car[i].init.y2);
        car[i].id = i;
        ensure_greater(car[i].init.x2, car[i].init.x1);
        ensure_greater(car[i].init.y2, car[i].init.y1);
        coords_x.PB(car[i].init.x1);
        coords_x.PB(car[i].init.x2);
        coords_y.PB(car[i].init.y1);
        coords_y.PB(car[i].init.y2);
        car[i].orig_w = car[i].init.y2 - car[i].init.y1;
    }
    for (int i = 0; i < cars; ++i) {
        scanf("%d%d%d%d", &car[i].target.x1, &car[i].target.y1, &car[i].target.x2, &car[i].target.y2);
        ensure_greater(car[i].target.x2, car[i].target.x1);
        ensure_greater(car[i].target.y2, car[i].target.y1);
        coords_x.PB(car[i].target.x1);
        coords_x.PB(car[i].target.x2);
        coords_y.PB(car[i].target.y1);
        coords_y.PB(car[i].target.y2);
    }
}

void rescale_aux(int& i, VI& coords) {
    i = lower_bound(coords.begin(), coords.end(), i) - coords.begin();
}

void rescale() {
    sort(coords_x.begin(), coords_x.end());
    sort(coords_y.begin(), coords_y.end());
    coords_x.erase(unique(coords_x.begin(), coords_x.end()), coords_x.end());
    coords_y.erase(unique(coords_y.begin(), coords_y.end()), coords_y.end());
    for (int i = 0; i < cars; ++i) {
        rescale_aux(car[i].init.x1, coords_x);
        rescale_aux(car[i].init.x2, coords_x);
        rescale_aux(car[i].init.y1, coords_y);
        rescale_aux(car[i].init.y2, coords_y);
        rescale_aux(car[i].target.x1, coords_x);
        rescale_aux(car[i].target.x2, coords_x);
        rescale_aux(car[i].target.y1, coords_y);
        rescale_aux(car[i].target.y2, coords_y);
    }
}

void sort_cars() {
    // sort by init
    sort(car, car + cars, [] (const Car& c1, const Car& c2) {
        if (c1.init.x1 == c2.init.x1) {
            return c1.init.y1 < c2.init.y1;
        }
        return c1.init.x1 < c2.init.x1;
    });
    // set order
    for (int i = 0; i < cars; ++i) {
        car[i].id = i;
    }
    // sort by target
    sort(car, car + cars, [] (const Car& c1, const Car& c2) {
        if (c1.target.x1 == c2.target.x1) {
            return c1.target.y1 < c2.target.y1;
        }
        return c1.target.x1 < c2.target.x1;
    });
}

bool compute() {
    for (int i = 0; i < cars; ++i) {
        print_car(i);
    }
    for (int i = 0; i < cars; ++i) {
        uint mn = tree.min(car[i].id);
        DBGprint("min(%d) = %u\n", car[i].id, mn);
        if (mn < car[i].orig_w) {
            return false;
        }
        // trzymamy wielkosc "przesmykow"
        tree.add(car[i].id, w - car[i].orig_w);
    }
    return true;
}

int main() {
    scanf("%d", &tests);
    while (tests--) {
        scanf("%d%d", &cars, &w);
        read_cars();
        rescale();
        sort_cars();
        printf("%s\n", compute() ? "TAK" : "NIE");
        tree.clear();
        coords_x.clear();
        coords_y.clear();
    }
}