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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <vector>
#include <list>
#include <stdio.h>
#include <algorithm>


//#define READ_FROM_FILE

#ifdef READ_FROM_FILE
#include <iostream>
#include <fstream>
#endif // READ_FROM_FILE

using namespace std;

struct Car
{
	int xMin;
	int xMax;
	int yMin;
	int yMax;
};

struct DoubleCar
{
	Car cur;
	Car next;
	int h;
};

void fillCar( Car & carToFill, Car & rawData )
{
	carToFill.xMin = min( rawData.xMin, rawData.xMax );
	carToFill.xMax = max( rawData.xMin, rawData.xMax );
	carToFill.yMin = min( rawData.yMin, rawData.yMax );
	carToFill.yMax = max( rawData.yMin, rawData.yMax );
}

struct ParkData
{
	int x;
	int h;

	bool start;
	bool right;

	int index;
};
bool sortParkData( const ParkData & p1, const ParkData & p2 )
{
	if ( p1.x == p2.x )
	{
		if ( !p1.right && p2.right )
			return true;
		if ( p1.right && !p2.right )
			return false;
		return true;
	}
	return p1.x < p2.x;
}

int main( )
{
#ifdef READ_FROM_FILE
	clock_t start = clock();
#endif

	int tc_quantity;

#ifdef READ_FROM_FILE
	std::ifstream fin ( "sample_par.txt" );

	if ( !fin.is_open( ) )
	{
		std::cout << "No data file" << std::endl;
		return 0;
	}
#endif // READ_FROM_FILE

#ifdef READ_FROM_FILE
	fin >> tc_quantity;
	std::cout << "TC quanity " << tc_quantity << std::endl;
#else
	scanf( "%d", &tc_quantity );
#endif

	for ( int tc=0; tc<tc_quantity; ++tc )
	{
		//cout << "Case #" << tc+1 << endl;
		int carQ;
		int parkH;

#ifdef READ_FROM_FILE
		fin >> carQ >> parkH;
#else
		scanf( "%d %d", &carQ, &parkH );
#endif

		vector< DoubleCar > cars( carQ );

		Car tempCar;

		for ( int i=0; i<carQ; ++i )
		{
#ifdef READ_FROM_FILE
			fin >> tempCar.xMin >> tempCar.yMin >> tempCar.xMax >> tempCar.yMax;
#else
			scanf( "%d %d %d %d", &tempCar.xMin, &tempCar.yMin, &tempCar.xMax, &tempCar.yMax );
#endif
			fillCar( cars[i].cur, tempCar );
			cars[i].h = cars[i].cur.yMax - cars[i].cur.yMin;
		}

		vector< ParkData > pdArray;

		ParkData parkDataTemp;
		for ( int i=0; i<carQ; ++i )
		{
#ifdef READ_FROM_FILE
			fin >> tempCar.xMin >> tempCar.yMin >> tempCar.xMax >> tempCar.yMax;
#else
			scanf( "%d %d %d %d", &tempCar.xMin, &tempCar.yMin, &tempCar.xMax, &tempCar.yMax );
#endif
			fillCar( cars[i].next, tempCar );
			if ( cars[i].h != ( cars[i].next.yMax - cars[i].next.yMin ) )
			{
				printf( "Invalid data - %d\n", i );
			}

			parkDataTemp.h = cars[i].h;
			parkDataTemp.index = i;
			if ( cars[i].cur.xMin <= cars[i].next.xMin )
			{
				parkDataTemp.x = cars[i].cur.xMin;
				parkDataTemp.start = true;
				parkDataTemp.right = true;
				pdArray.push_back( parkDataTemp );

				parkDataTemp.x = cars[i].next.xMax;
				parkDataTemp.start = false;
				parkDataTemp.right = false;
				pdArray.push_back( parkDataTemp );
			}
			else
			{
				parkDataTemp.x = cars[i].cur.xMax;
				parkDataTemp.start = true;
				parkDataTemp.right = false;
				pdArray.push_back( parkDataTemp );

				parkDataTemp.x = cars[i].next.xMin;
				parkDataTemp.start = false;
				parkDataTemp.right = true;
				pdArray.push_back( parkDataTemp );
			}
		}

		sort( pdArray.begin( ), pdArray.end( ), sortParkData );

		list< ParkData* > pdList;

		bool cantDoThat = false;
		for ( int i=0; i<pdArray.size( ); ++i )
		{

				for ( list< ParkData* >::iterator li=pdList.begin( ); li!=pdList.end( ); ++li )
				{
					if ( (*li)->index == pdArray[i].index )
					{
						pdList.erase( li );
						break;
					}

					if ( parkH - pdArray[i].h < (*li)->h && pdArray[i].start != (*li)->start )
					{
						cantDoThat = true;
						break;
					}
				}
				if ( pdArray[i].start && pdArray[i].right || !pdArray[i].start && pdArray[i].right )
				{
					pdList.push_back( &pdArray[i] );
				}

			
			if ( cantDoThat )
			{
				break;
			}
		}

		if ( cantDoThat )
			printf( "NIE\n" );
		else
			printf( "TAK\n" );
	}

#ifdef	READ_FROM_FILE
	clock_t end = clock();
	float seconds = (float)(end - start) / CLOCKS_PER_SEC;

	printf( "time - %f\n", seconds);
#endif

	return 0;
}