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
#include <stdlib.h>
//---===---
#include <stdio.h>
#include <unistd.h>

typedef unsigned int uint;

char buf_in[4096000];
int buf_in_head = 0;
int buf_in_tail = 0;

static int getChar(void) {
  while (buf_in_head == buf_in_tail) {
    int rv = read(0, buf_in, sizeof(buf_in));
    if (rv > 0) {
      buf_in_head = 0;
      buf_in_tail = rv;
      break;
    };
    if (rv == 0) return EOF;
  }
  return buf_in[buf_in_head++];
}

// only 1 call is safe, and only if previously getChar() had been called.
static void ungetChar(void) {
  --buf_in_head;
}

static uint getUInt() {
  uint v;
  int c;
  for (;;) {
    c = getChar();
    if (c < '0') continue;
    if (c > '9') continue;
    v = c - '0';
    break;
  };
  for (;;) {
    c = getChar();
    if (c < '0') break;
    if (c > '9') break;
    v *= 10;
    v += c - '0';
  };
  ungetChar();
  return v;
}
//---===---

typedef struct {
  uint size;
  int pos;
} ss;

ss S[50000+7];
int sp;

typedef struct {
  uint dx, dy, old_x, new_x;
  int pos, t_old, t_new;
} tt;

tt T[50000];

int cmp_old_x(const tt * x, const tt * y) {
  if (x->old_x < y->old_x) return -1;
  if (x->old_x > y->old_x) return +1;
  return 0;
}

int cmp_new_x(const tt * x, const tt * y) {
  if (x->new_x < y->new_x) return -1;
  if (x->new_x > y->new_x) return +1;
  return 0;
}

int void_cmp_old_x(const void * x, const void * y) {
  return cmp_old_x((const tt*)x, (const tt*)y);
}

int void_cmp_new_x(const void * x, const void * y) {
  return cmp_new_x((const tt*)x, (const tt*)y);
}

void error(int a, int b) {
#ifdef CHECK
  fprintf(stderr, "Error %d %d\n", a, b);
  exit(1);
#endif
}

//#define DUMP
int stack_linear_search(uint at_most, int i, int dst) {
  int j, k;

#ifdef DUMP
  fprintf(stderr, "need to push forward %d [at most size %u barrier] to %d\n", i, at_most, dst);
#endif /* DUMP */
  k = -1;
  for (j = 0; j <= sp; ++j) {
#ifdef DUMP
    fprintf(stderr, "[%u@%d] ", S[j].size, S[j].pos);
#endif /* DUMP */
    if (S[j].size > at_most) k = j;
  };
#ifdef DUMP
  fprintf(stderr, "-> %d [size:%u pos:%d]\n", k, S[k].size, S[k].pos);
#endif /* DUMP */

  return k;
}
//#undef DUMP

//#define CHECKBIN

// find last which is > at_most
int stack_bin_search(uint at_most, int i, int dst) {
#ifdef CHECKBIN
  int linear = stack_linear_search(at_most, i, dst);
#endif /* CHECKBIN */
  int front = 0;
  int end = sp;
  int mid;

  while (front < end) {
    mid = (front + end) / 2;
    if (S[mid].size > at_most) {
      front = mid + 1;
    } else {
      end = mid;
    };
  };

  if (S[front].size <= at_most) --front;

#ifdef CHECKBIN
  if (front != linear) {
    fprintf(stderr, "found: %d/%d should be %d\n", front, end, linear);
    printf("WRONG\n");
    exit(1);
  };
#endif /* CHECKBIN */

  return front;
}

int total_trucks;

void solve (void) {
  // 1 <= n <= 50000
  int n = getUInt();
  // 1 <= w <= 1,000,000,000
  uint w = getUInt();

  int i;

  for (i = 0; i < n; ++i) {
    // 0 <= x1, x2 <= 1,000,000,000
    // 0 <= y1, y2 <= w
    // x1 != x2, y1 != y2
    uint x1 = getUInt();
    uint y1 = getUInt();
    uint x2 = getUInt();
    uint y2 = getUInt();

    if (x1 == x2) error(1, i);
    if (y1 == y2) error(2, i);
    if (x1 > 1000000000) error(3, i);
    if (x2 > 1000000000) error(4, i);
    if (y1 > w) error(5, i);
    if (y2 > w) error(6, i);

    T[i].dy = (y1 < y2) ? (y2 - y1) : (y1 - y2);
    T[i].dx = (x1 < x2) ? (x2 - x1) : (x1 - x2);
    T[i].old_x = (x1 < x2) ? x1 : x2;
  };
  for (i = 0; i < n; ++i) {
    // 0 <= x1, x2 <= 1,000,000,000
    // 0 <= y1, y2 <= w
    // x1 != x2, y1 != y2
    uint x1 = getUInt();
    uint y1 = getUInt();
    uint x2 = getUInt();
    uint y2 = getUInt();

    if (x1 == x2) error(11, i);
    if (y1 == y2) error(12, i);
    if (x1 > 1000000000) error(13, i);
    if (x2 > 1000000000) error(14, i);
    if (y1 > w) error(15, i);
    if (y2 > w) error(16, i);

    if (( (y1 < y2) ? (y2 - y1) : (y1 - y2) ) != T[i].dy) error(21, i);
    if (( (x1 < x2) ? (x2 - x1) : (x1 - x2) ) != T[i].dx) error(22, i);
    T[i].new_x = (x1 < x2) ? x1 : x2;
  };

#define TRUCK(_i_) (2 * T[_i_].dy >  w)
#define CAR(_i_)   (2 * T[_i_].dy <= w)

  qsort(T, n, sizeof(tt), void_cmp_new_x);
  {
    int trucks = 1;
    for (i = 0; i < n; ++i) {
      T[i].pos = i;
      if (TRUCK(i)) ++trucks;
      T[i].t_new = trucks;
      if (TRUCK(i)) ++trucks;
    };
    total_trucks = 1 + trucks;
  };

// trucks are even -- furthermore 0 and total_trucks are barrier trucks
// cars are odd

  qsort(T, n, sizeof(tt), void_cmp_old_x);
  {
    int trucks = 1;
    for (i = 0; i < n; ++i) {
      //T[i].old_x = i;
      if (TRUCK(i)) ++trucks;
      T[i].t_old = trucks;
      if (TRUCK(i)) ++trucks;
    };
  };

#ifdef DUMP
  fprintf(stderr, "i: dx, dy, old_x, new_x, pos, car/truck, t_old, t_new\n");
  for (i = 0; i < n; ++i) {
    fprintf(stderr, "%d: (%u) %u (%u %u) %d [%s]\n",
      i, T[i].dx, T[i].dy, T[i].old_x, T[i].new_x, T[i].pos,
      TRUCK(i) ? "truck" : "car", T[i].t_old, T[i].t_new);
  };
#endif /* DUMP */

  {
    int curpos = -1;
    for (i = 0; i < n; ++i) {
      if (CAR(i)) continue;
      if (T[i].pos <= curpos) { printf("NIE\n"); return; };
      curpos = T[i].pos;
    };
  };

#ifdef DUMP
  fprintf(stderr, "[%d]\n", w);
  for (i = 0; i < n; ++i) fprintf(stderr, "%d->%d [%d->%d] %u\n", i, T[i].pos, T[i].t_old, T[i].t_new, T[i].dy);
#endif /* DUMP */

  {
    int last_truck = 0;

    S[0].size = w + 1; // infinity
    S[0].pos = last_truck;
    sp = 0; // last used on stack

    for (i = 0; i < n; ++i) {
      if (TRUCK(i)) {
        while (T[i].dy >= S[sp].size) --sp;
        S[++sp].size = T[i].dy;
        last_truck = S[sp].pos = T[i].t_old; // == T[i].t_new
      } else {
        uint at_most = w - T[i].dy;
        int dst = T[i].t_new;
        int k;
        if (dst > last_truck) continue; // will check on reverse pass
        k = stack_bin_search(at_most, i, dst); // i, dst only for debug
        if (dst < S[k].pos) { printf("NIE\n"); return; };
      };
    };
  };

  {
    int last_truck = total_trucks;

    S[0].size = w + 1; // infinity
    S[0].pos = last_truck;
    sp = 0; // last used on stack

    for (i = n - 1; i >= 0; --i) {
      if (TRUCK(i)) {
        while (T[i].dy >= S[sp].size) --sp;
        S[++sp].size = T[i].dy;
        last_truck = S[sp].pos = T[i].t_old; // == T[i].t_new
      } else {
        uint at_most = w - T[i].dy;
        int dst = T[i].t_new;
        int k;
        if (dst < last_truck) continue; // was checked on forward pass
        k = stack_bin_search(at_most, i, dst); // i, dst only for debug
        if (dst > S[k].pos) { printf("NIE\n"); return; };
      };
    };
  };

  printf("TAK\n");
}

int main (void) {
  // 1 <= t <= 20
  int t = getUInt();
  while (t--) solve();
  return 0;
}