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
// Solution to 3B_PAR
// Author: Michal Czuczman <michal.czuczman@gmail.com>
// created on Wed May 14 21:23:01 CEST 2014

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef unsigned int uint;
typedef unsigned long long ull;

struct Car {
	int id;
	int offset;
	int width;
	Car(int id, int x1, int y1, int x2, int y2)
		: id(id), offset(min(x1, x2)), width(abs(y1 - y2)) {}

	bool operator<(const Car& b) const {
		return offset < b.offset;
	}
};

struct Parking {
	vector<Car> cars;
	int width;
	vector<int> positions;
	Parking(int num_cars, int width) : width(width) {
		for(int id = 0; id < num_cars; ++id) {
			int x1, y1, x2, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			cars.push_back(Car(id, x1, y1, x2, y2));
		}
		sort(cars.begin(), cars.end());
		positions.resize(cars.size());
		for(int i = 0; i < num_cars; ++i) {
			positions[cars[i].id] = i;
		}
	}
	bool move_left(int id) {
		int position = positions[id];
		if(cars[position].width + cars[position - 1].width > width) {
			return false;
		}
		swap(cars[position], cars[position - 1]);
		positions[cars[position].id] = position;
		positions[id] = position - 1;
		return true;
	}
};

void test()
{
	int num_cars, width;
	cin >> num_cars >> width;
	Parking current(num_cars, width);
	Parking target(num_cars, width);

	for(int i = 0; i < num_cars; ++i) {
		while(current.positions[target.cars[i].id] > i) {
			if(!current.move_left(target.cars[i].id)) {
				cout << "NIE\n";
				return;
			}
		}
	}
	cout << "TAK\n";
}

int main()
{
	ios::sync_with_stdio(false);
	int z;
	for (cin >> z; z; --z)
		test();
}