Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8. Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
  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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Piotr Golda
#define _CRT_SECURE_NO_WARNINGS  
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define LLD long long int

#define MAX_NUM 50000

class Car;

template< class _Object >
class PartitionTree;

int T, N, W;
int POS[ MAX_NUM ];
Car* Parking;
PartitionTree< int >* Height;  

/**************************
DRZEWO PRZEDZIALOWE
pytanie-przedzial-max
modyfikacja-element-dodawanie
***************************/
template< class _Object >
class PartitionTree
{
	unsigned int m_size;
	_Object* m_T;
	_Object m_neutral;

	const _Object& (*m_questionFunction)( const _Object&, const _Object&);
	void (*m_modificationFunction)( _Object&, _Object&);


	static const _Object& maximum_query(const _Object&, const _Object&);
	static void add( _Object&, _Object&);

public:

	PartitionTree(unsigned int);
	PartitionTree(unsigned int, _Object );
	~PartitionTree();

	void modify(unsigned int, _Object );
	_Object query(unsigned int, unsigned int);

	//friend ostream& operator<< <>( ostream&, PartitionTree<_Object> );

	const _Object& getObject( unsigned int );
	unsigned int getSize();
};


class Car
{
public:
	unsigned m_num;
	unsigned m_position;
	unsigned m_height;

	Car( unsigned p_num, unsigned p_pos, unsigned p_height )
		:m_num(p_num), m_position(p_pos), m_height(p_height){}
	Car()
		:m_num(0), m_position(0), m_height(0){}

	bool operator<( const Car& p_rhs ) const
	{
		if( m_position < p_rhs.m_position )
			return true;
		return false;
	}

};


//bool cmp(const Car& p_lhs, const Car& p_rhs )
//{
//
//}

void scan()
{
	scanf("%d %d", &N, &W);
	Parking = new Car[N];
	int a,b,c,d;
	for(int i = 0; i < N; ++i)
	{
			scanf( "%d %d %d %d", &a, &b, &c, &d);
			if( c < a )
				swap(a,c);
			Parking[i] = Car(i, a, abs(d - b) );
	}
	sort(Parking, Parking+N);
}

void initialize()
{
	
	for(int i = 0; i < N; ++i)
	{
		//cout<< Parking[i].m_num<<' ';
		POS[ Parking[i].m_num ] = i;
		Height->modify(i, Parking[i].m_height );
	}
	//cout<<endl;
}

bool scanCheckDestination()
{
	int a,b,c,d;
	for(int i = 0; i < N; ++i)
	{
		scanf( "%d %d %d %d", &a, &b, &c, &d);
		if( c < a )
			swap(a,c);
		Parking[i] = Car(i, a, abs(d - b) );
	}

	sort(Parking, Parking+N);

	int pos;
	int maxPrev;
	for(int i = 0; i < N; ++i)
	{
		pos = POS[ Parking[i].m_num ];
		if( pos != 0 )
			maxPrev = Height->query(0, pos-1 );
		else
			maxPrev = 0;
		if( maxPrev + Parking[i].m_height > W )
			return false;
		Height->modify(pos, -((int)Parking[i].m_height) );
	}

	return true;
}


int main()
{
	scanf("%d", &T);
	for(int t = 0; t < T; ++t)
	{
		scan();
		Height = new PartitionTree<int>( N );
		initialize();
		if( scanCheckDestination() )
			printf("TAK\n");
		else
			printf("NIE\n");
		delete Height;
	}
	return 0;
}


/**************************
DRZEWO PRZEDZIALOWE
***************************/
template< class _Object >
inline PartitionTree<_Object>::PartitionTree( unsigned int p_size )
	:m_neutral( _Object() )
{
	if(p_size == 0)
		throw;
	m_size = 1;
	while( m_size < p_size )
	{
		m_size = m_size << 1;
	}
	m_size = m_size << 1;

	m_T = new _Object[m_size+1];
	

	for(unsigned int i = 0; i <= m_size; ++i)
		m_T[i] = m_neutral;
	m_questionFunction = (&PartitionTree<_Object>::maximum_query);
	m_modificationFunction = (&PartitionTree<_Object>::add);

	m_size = m_size >> 1;
}

template< class _Object >
inline PartitionTree<_Object>::PartitionTree( unsigned int p_size, _Object p_neutralObject )
	:m_neutral( p_neutralObject )
{
	if(p_size == 0)
		throw;
	m_size = 1;
	while( m_size < p_size )
	{
		m_size = m_size << 1;
	}
	m_size = m_size << 1;

	m_T = new _Object[m_size+1];
	

	for(unsigned int i = 0; i <= m_size; ++i)
		m_T[i] = m_neutral;
	m_questionFunction = (&PartitionTree<_Object>::maximum_query);
	m_modificationFunction = (&PartitionTree<_Object>::add);

	m_size = m_size >> 1;
}

template< class _Object >
inline PartitionTree<_Object>::~PartitionTree()
{
	delete[] m_T;
}


template< class _Object >
inline unsigned int PartitionTree<_Object>::getSize()
{
	return m_size;
}

template< class _Object >
inline const _Object& PartitionTree<_Object>::getObject( unsigned int p_index )
{
	return m_T[ p_index + m_size ];
}

template< class _Object >
inline const _Object& PartitionTree<_Object>::maximum_query( const _Object& p_lhs, const _Object& p_rhs)
{
	return max(p_lhs, p_rhs);
}

template< class _Object >
inline void PartitionTree<_Object>::add( _Object& p_desValue, _Object& p_srcValue )
{
	p_desValue = p_desValue + p_srcValue;
}

template< class _Object >
inline void PartitionTree<_Object>::modify(unsigned int p_index, _Object p_parameter )
{
	p_index += m_size;
	m_modificationFunction( m_T[ p_index ], p_parameter );
	while ( p_index != 1 ) 
	{
		p_index = p_index >> 1;
		m_T[p_index] = m_questionFunction( m_T[ p_index << 1 ] , m_T[ (p_index << 1) + 1] );
    }
}

template< class _Object >
inline _Object PartitionTree<_Object>::query(unsigned int p_a, unsigned int p_b)
{
	unsigned int va = m_size + p_a, vb = m_size + p_b;
   /* Skrajne przedzia�y do rozk�adu. */
	_Object wyn = m_questionFunction( m_T[va], m_T[vb]);
   /* Spacer a� do momentu spotkania. */
   while ( (va >> 1) != (vb >> 1) ) {
     if (va % 2 == 0) wyn =  m_questionFunction( wyn, m_T[va + 1] ); /* prawa bombka na lewej �cie�ce */
     if (vb % 2 == 1) wyn =  m_questionFunction( wyn, m_T[vb - 1] ); /* lewa bombka na prawej �cie�ce */
     va = va >> 1; vb = vb >> 1;
   }
   return wyn;
}

//template< class _Object >
//ostream& operator<<( ostream& p_out, PartitionTree<_Object> p_Tree )
//{
//	unsigned int pot = 2;
//	for(int i = 1; i < p_Tree.m_size << 1; ++i)
//	{
//		p_out << p_Tree.m_T[i] <<' ';
//		if( i+1 == pot )
//		{
//			pot = pot << 1;
//			p_out<<'\n';
//		}
//	}
//	return p_out;
//}