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
/*
 *  Copyright (C) 2014  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;

//#define DEBUG 666
#ifdef DEBUG
	#define DBG(x) x
#else
	#define DBG(x)
#endif


class Car {
public:
	int x, height;

	Car(int x1, int y1, int x2, int y2) {
		height = abs(y1 - y2);
		x = min(x1, x2);
	}

	bool operator<(const Car& other) const {
		if (x == other.x) {
			return height < other.height;
		}
		return x < other.x;
	}
};


class Max {
public:
	int height, index;

	Max(int h, int i): height(h), index(i) {}

	bool operator<(const Max& other) const {
		return height < other.height;
	}

	bool operator==(const Max& other) const {
		return height == other.height;
	}
};


int main() {
	int t, n, size;
	int x1, x2;
	int y1, y2;

	vector<Car> cars;
	vector<int> indices;

	vector<pair<int, int>> constraints;
	deque<Max> maxima;

	ios::sync_with_stdio (false);
	cin >> t;

	for (int i = 0; i < t; ++i) {
		cin >> n >> size;
		cars.resize(n, Car(0, 0, 1, 1));
		indices.resize(n, 0);

		constraints.clear();
		maxima.clear();

		// read starting parking setup
		for (int j = 0; j < n; ++j) {
			cin >> x1 >> y1 >> x2 >> y2;
			cars[j] = Car(x1, y1, x2, y2);
			indices[j] = j;
		}

		// index sort cars by position (left to right)
		sort(begin(indices), end(indices), [&cars](int a, int b) {
			return cars[a] < cars[b];
		});

DBG(
cout << "h = ";
for (auto x : indices) {
	cout << cars[x].height << " ";
}
cout << endl;

cout << "i_start = ";
for (auto x : indices) {
	cout << x << " ";
}
cout << endl;
);

		for (int j = 0; j < n; ++j) {
			int current = indices[j];

			// check if there is a car blocking the current car from moving left
			auto m = Max(cars[current].height, -1);
			auto iter = upper_bound(begin(maxima), end(maxima), m);
			if (iter != begin(maxima)) {
				--iter;
				constraints.push_back(make_pair(current, iter->index));
			}

			// update the maxima using the current car height
			m = Max(size - cars[current].height + 1, current);
			iter = lower_bound(begin(maxima), end(maxima), m);
			if (iter == end(maxima)) {
				maxima.push_back(m);
			} else {
				// remove all greater values
				while (end(maxima) - 1 != iter) {
					maxima.pop_back();
				}
				// set new value of maxima
				*iter = m;
			}

DBG(
cout << "maxima = ";
for (auto z : maxima) {
	cout << z.height << " " << z.index << ", ";
}
cout << endl;
);

		}

DBG(
cout << "c = ";
for (auto c: constraints) {
	cout << c.first << "," << c.second << " ";
}
cout << endl;
);

		// read target parking setup
		for (int j = 0; j < n; ++j) {
			cin >> x1 >> y1 >> x2 >> y2;
			cars[j] = Car(x1, y1, x2, y2);
		}

		// check if any of the constraints is violated
		bool correct = true;
		for (auto c: constraints) {

DBG(
cout << c.first << "," << c.second << "  " << cars[c.first].x << " vs " << cars[c.second].x << endl;
);

			if (cars[c.first].x < cars[c.second].x) {
				correct = false;
				break;
			}
		}

		if (correct) {
			cout << "TAK" << endl;
		} else {
			cout << "NIE" << endl;
		}
DBG(
cout << endl;
);
	}
	return 0;
}