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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <functional>
#include <queue>

struct P
{
	P(int x, int height, int index)
		: _x(x)
		, _height( height )
		, _leftblock(-1)
		, _rightblock(-1)
		, _startIndex(0)
		, _targetIndex(0)
	{}

	void setTargetPos( int x ) { _targetX = x; }

	int getX() const { return _x; }
	int getTargetX() const { return _targetX; }
	int getHeight() const { return _height; }

	int _x;
	int _height;
	int _leftblock;
	int _rightblock;
	int _targetX;

	int _startIndex;
	int _targetIndex;
};

typedef std::vector<P> VP;
typedef std::vector<int> VI;

template <class T>
struct lesserx
{
	lesserx( VP &vp, T &mf ) : _mem_fn(mf), _vp(vp) {}

	bool operator()(int lhs, int rhs) const
	{
		return _mem_fn(_vp[lhs]) < _mem_fn(_vp[rhs]);
	}

	T &_mem_fn;
	VP &_vp;
};

int main()
{
	int T;

	scanf("%d ", &T);

	for (int t = 0; t < T; ++t)
	{
		int N, W;
		scanf("%d %d ", &N, &W);

		VP v;
		v.reserve(N);
		std::vector<int> startstate;
		startstate.reserve(N);
		std::vector<int> targetstate;
		targetstate.reserve(N);
		for (int n = 0; n < N; ++n)
		{
			int x1, y1, x2, y2;
			scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);
			
			v.push_back(P(std::min(x1,x2), std::abs(y2-y1), n));	
			startstate.push_back(n);
			targetstate.push_back(n);
		}
		for (int n = 0; n < N; ++n)
		{
			int x1, y1, x2, y2;
			scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);

			v[n].setTargetPos(std::min(x1,x2));
		}

		auto xgetter = std::mem_fn(&P::getX);
		std::sort( startstate.begin(), startstate.end(),
			 lesserx<decltype(xgetter)>(v, xgetter));

		auto targetXgetter = std::mem_fn(&P::getTargetX);
		std::sort( targetstate.begin(), targetstate.end(),
			 lesserx<decltype(targetXgetter)>(v, targetXgetter));

		// both indexes sorted
		// now, get data into objects
		for (int i = 0; i < N; i++)
		{
			v[startstate[i]]._startIndex = i;
			v[targetstate[i]]._targetIndex = i;
		}

		// create relations
		auto heightGetter = std::mem_fn(&P::getHeight);
		typedef lesserx<decltype(heightGetter)> HComp;
		std::priority_queue<int, std::deque<int>, HComp> PQ(HComp(v, heightGetter)); 

		for (int i = 0; i < N; ++i)
		{
			while (!PQ.empty() && v[PQ.top()].getHeight() + v[i].getHeight() > W)
			{
				v[PQ.top()]._rightblock = i;
				PQ.pop();
			}
			PQ.push(i);
		}
		while (!PQ.empty()) PQ.pop();
		for (int i = N-1; i >= 0; --i)
		{
			while (!PQ.empty() && v[PQ.top()].getHeight() + v[i].getHeight() > W)
			{
				v[PQ.top()]._leftblock = i;
				PQ.pop();
			}
			PQ.push(i);
		}
		
		// check if related are swapped
		bool yes = true;
		for (int i = 0; i < N && yes; ++i)
		{
			auto differentOrder =
				[](const P &lhs, const P &rhs) {
					return (lhs._startIndex < rhs._startIndex)
						!=
						(lhs._targetIndex < rhs._targetIndex);
				};
			if (v[i]._leftblock != -1 && differentOrder(v[i], v[v[i]._leftblock]))
			{
				yes = false;
			}
			if (v[i]._rightblock != -1 && differentOrder(v[i], v[v[i]._rightblock]))
			{
				yes = false;
			}
		}
		printf("%s\n", yes ? "TAK" : "NIE");
	}
	return 0;
}