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
#include <stdlib.h>
#include <iostream>
#include <vector>

struct task_t
{
	int start;
	int end;
	int left;
};

int n, m;
std::vector< task_t > tasks;

int main()
{
	int pos = 1 << 30;
	std::cin >> n >> m;
	for( int i = 0; i < n; ++i )
	{
		task_t t;
		std::cin >> t.start >> t.end >> t.left;
		
		tasks.push_back( t );
		
		if ( pos > t.start )
		{
			pos = t.start;
		}
	}
	
	while ( !tasks.empty() )
	{
		int next_pos = 1 << 30;
		
		std::vector< int > curr;
		
		// collect must-execute tasks
		
		for ( int i = 0; i < tasks.size(); ++i )
		{
			const task_t& t = tasks[ i ];
			if ( t.start <= pos && pos < t.end && t.end - pos == t.left )
			{
				if ( curr.size() == m )
				{
					std::cout << "NIE";
					return 0;
				}

				curr.push_back( i );
				if ( next_pos > t.end )
				{
					next_pos = t.end;
				}
			}
			else if ( pos < t.start )
			{
				if ( next_pos > t.start )
				{
					next_pos = t.start;
				}
			}
		}
		
		// collect optional tasks
		
		for ( int i = 0; i < tasks.size(); ++i )
		{
			const task_t& t = tasks[ i ];
			if ( t.start <= pos && pos < t.end && t.end - pos > t.left )
			{
				if ( curr.size() < m )
				{
					curr.push_back( i );
					if ( next_pos > pos + t.left )
					{
						next_pos = pos + t.left;
					}
				}
				else if ( next_pos > t.end - t.left )
				{
					next_pos = t.end - t.left;
				}
			}
		}
		
		// record execution
		
		const int offset = next_pos - pos;
		for ( int i = 0; i < curr.size(); ++i )
		{
			task_t& t = tasks[ curr[ i ] ];
			t.left -= offset;
		}
		
		// remove finished tasks
		
		int new_size = 0;
		for ( int i = 0; i < tasks.size(); ++i )
		{
			const task_t& t = tasks[ new_size ] = tasks[ i ];
			if ( t.left )
			{
				++new_size;
			}
		}
		
		tasks.resize( new_size );

		pos = next_pos;
	}
	
	std::cout << "TAK";
	return 0;
}