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

using namespace std;

struct car {
  int id;
  int x;
  int height;
};

bool xcompare(const car& a, const car& b) {
  return a.x < b.x;
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, w;
    scanf("%d%d", &n, &w);
    vector<car> srccars(n), dstcars(n);
    for (int i = 0; i < n; ++i) {
      int x1, x2, y1, y2;
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      car c = {i, min(x1,x2), max(y1,y2) - min(y1,y2)};
      srccars[i] = c;
    }

    for (int i = 0; i < n; ++i) {
      int x1, x2, y1, y2;
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      car c = {i, min(x1, x2), max(y1, y2) - min(y1,y2)};
      dstcars[i] = c;
    }

    sort(srccars.begin(), srccars.end(), xcompare);
    sort(dstcars.begin(), dstcars.end(), xcompare);

    vector<int> endpos(n);
    {
      map<int, int> rearmap;

      for (int i = 0; i < n; ++i) {
        rearmap[srccars[i].id] = i;
        srccars[i].id = i;
      }

      for (int i = 0; i < n; ++i) {
        dstcars[i].id = rearmap[dstcars[i].id]; 
        endpos[dstcars[i].id] = i;
      }
    }

    bool fail = false;
    
    for (int i = 0; i < n; ++i) {
      const car& thiscar = srccars[i];
      int ep = endpos[thiscar.id];
      
      for (int k = ep-1; k >= 0; k--) {
        if (i < dstcars[k].id && thiscar.height+dstcars[k].height > w) {
          fail = true;
          break;
        }
      }
      if (fail)
        break;
    }

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