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
//#define DEBUG

#ifdef DEBUG
#define tracef(fmt, ...) \
        do { printf("%s:%d:%s(): " fmt "\n", __FILE__, \
                                __LINE__, __func__, ## __VA_ARGS__); } while (0)
#else
#define tracef(fmt, ...)
#define NDEBUG
#endif

#include <algorithm>
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <set>

using namespace std;

#define NO_CONSTRAINT_ID (-1)

class ConstraintFinder
{
private:
  struct ConstraintPoint
  {
    ConstraintPoint() {}
    ConstraintPoint(int id, int spaceLeft) : id(id), spaceLeft(spaceLeft) {}
    bool operator< (const ConstraintPoint& b) const
    {
      return spaceLeft < b.spaceLeft;
    }
    int id;
    int spaceLeft;
  };
  vector<ConstraintPoint> helper;
  const int maxHeight;
public:
  ConstraintFinder(int height, size_t expectedSize) : maxHeight(height)
  {
    helper.reserve(expectedSize);
    helper.emplace_back(NO_CONSTRAINT_ID, 0);
  }

  int push_back_and_get_constraint_id(const int id, const int height)
  {
    assert(!helper.empty());
    auto retPos = lower_bound(helper.begin(), helper.end(), ConstraintPoint(0, height));
    assert(retPos != helper.begin());
    --retPos;
    const int ret = retPos->id;

    const int spaceLeft = maxHeight - height;
    const auto pos = lower_bound(helper.begin(), helper.end(), ConstraintPoint(0, spaceLeft));
    helper.erase(pos, helper.end());

    assert(spaceLeft >= 0);
    helper.emplace_back(id, spaceLeft);
    #ifdef DEBUG
      printf("[(%d, %d)", helper[0].spaceLeft, helper[0].id);
      for (int i = 1; i < helper.size(); i++)
      {
        printf(", (%d, %d)", helper[i].spaceLeft, helper[i].id);
      }
      printf("]\n");
    #endif
    return ret;
  }
};

struct Rect
{
  int id;
  int x;
  int height;
  int leftConstraint;
  int rightConstraint;
};

bool rect_cmp_x_asc(const Rect& a, const Rect& b)
{
  return a.x < b.x;
}

bool rect_cmp_id_asc(const Rect& a, const Rect& b)
{
  return a.id < b.id;
}

bool meets_constraints(const vector<Rect>& constraints, const vector<Rect>& v)
{
  assert(a.size() == b.size());
  set<int> rectanglesOnLeft;
  for (int i = 0; i < v.size(); i++)
  {
    int leftConstraint = constraints[v[i].id].leftConstraint;
    int rightConstraint = constraints[v[i].id].rightConstraint;
    if (leftConstraint != NO_CONSTRAINT_ID && rectanglesOnLeft.find(leftConstraint) == rectanglesOnLeft.end())
    {
      return false;
    }
    if (rightConstraint != NO_CONSTRAINT_ID && rectanglesOnLeft.find(rightConstraint) != rectanglesOnLeft.end())
    {
      return false;
    }
    rectanglesOnLeft.insert(v[i].id);
  }
  return true;
}

int main()
{
  int t;
  scanf("%d", &t);
  while (t--)
  {
    int n, h;
    scanf("%d%d", &n, &h);
    vector<Rect> v[2];
    ConstraintFinder leftConstraintFinder(h, n);
    ConstraintFinder rightConstraintFinder(h, n);
    for (int k = 0; k < 2; k++)
    {
      for (int id = 0; id < n; id++)
      {
        Rect r;
        int x1, x2, y1, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        r.id = id;
        r.x = min(x1, x2);
        r.height = abs(y1 - y2);
        v[k].push_back(r);
      }
      sort(v[k].begin(), v[k].end(), rect_cmp_x_asc);
    }

    for (auto it = v[0].begin(); it != v[0].end(); ++it)
    {
      it->leftConstraint = leftConstraintFinder.push_back_and_get_constraint_id(it->id, it->height);
    }
    for (auto it = v[0].rbegin(); it != v[0].rend(); ++it)
    {
      it->rightConstraint = rightConstraintFinder.push_back_and_get_constraint_id(it->id, it->height);
    }
    sort(v[0].begin(), v[0].end(), rect_cmp_id_asc);

    #ifdef DEBUG
      for (int i = 0; i < v[0].size(); i++)
      {
        printf("[%d, %d]\n", v[0][i].leftConstraint, v[0][i].rightConstraint);
      }
    #endif
    printf(meets_constraints(v[0], v[1]) ? "TAK\n" : "NIE\n");
  }
  return 0;
}