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
//#pragma warning(disable:4996)

#include <cstdio>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;

struct Task {
	int length;
	int winStart;
	int winEnd;
	int processed;
	int marginLost;

	bool operator<(const Task& t2) const {
		if (winStart == t2.winStart) {
			return winEnd < t2.winEnd;
		}
		return winStart < t2.winStart;
	}

	int getMargin() const {
		return winEnd - winStart - length - marginLost;
	}

	int notProcessed() const {
		return length - processed;
	}
};

struct Range {
	int start;
	int end;
	set<int> tasks;

	bool operator<(const Range& other) const {
		return start < other.start;
	}

	int rangeSize() const {
		return end - start;
	}
};

inline int createRanges(const vector<Task>& tasks, vector<Range>& ranges) {
	list<Range> tmpRanges;
	int n = tasks.size();
	for (int i = 0; i < n; ++i) {
		if (tmpRanges.size() == 0 || tmpRanges.rbegin()->end <= tasks[i].winStart) {
			Range r;
			r.start = tasks[i].winStart;
			r.end = tasks[i].winEnd;
			r.tasks.insert(i);
			tmpRanges.push_back(r);
		}
		else {
			list<Range>::reverse_iterator it;
			for (it = tmpRanges.rbegin(); it != tmpRanges.rend() && it->start > tasks[i].winStart; ++it) {
				it->tasks.insert(i);
			}

			if (it != tmpRanges.rend() && it->start == tasks[i].winStart) {
				it->tasks.insert(i);
			}
			else if (it != tmpRanges.rend()) {
				Range r;
				r.start = tasks[i].winStart;
				r.end = it->end;
				r.tasks.insert(it->tasks.begin(), it->tasks.end());
				r.tasks.insert(i);
				it->end = tasks[i].winStart;
				tmpRanges.insert(it.base(), r);
			}

			if (tmpRanges.rbegin()->end < tasks[i].winEnd) {
				Range r;
				r.start = tmpRanges.rbegin()->end;
				r.end = tasks[i].winEnd;
				r.tasks.insert(i);
				tmpRanges.push_back(r);
			}
		}
	}

	ranges.reserve(tmpRanges.size());
	ranges.assign(tmpRanges.begin(), tmpRanges.end());

	int windowsMax = 1;
	for (vector<Range>::iterator it = ranges.begin(); it != ranges.end(); ++it) {
		if (windowsMax < it->tasks.size()) {
			windowsMax = it->tasks.size();
		}
	}
	return windowsMax;
}

bool missingCPUSlots(const vector<Range>& ranges, const vector<Task>& tasks, const int m) {
	int timeNeeded = 0;
	for (vector<Task>::const_iterator it = tasks.begin(); it != tasks.end(); ++it) {
		timeNeeded += it->notProcessed();
	}
	int rangesTime = 0;
	for (vector<Range>::const_iterator it = ranges.begin(); it != ranges.end(); ++it) {
		rangesTime += (it->end - it->start);
	}
	return (timeNeeded > m * rangesTime);
}

inline int consumeSingleRangeTasks(const int id, vector<Task>& tasks, vector<Range>& ranges) {
	int consumed = 0;
	int rangeSize = ranges[id].rangeSize();
	vector<int> tasksConsumed;
	for (set<int>::iterator it = ranges[id].tasks.begin(); it != ranges[id].tasks.end(); ++it) {
		if ((id == 0 || ranges[id - 1].tasks.find(*it) == ranges[id - 1].tasks.end())
			&& ((id == ranges.size()-1) || ranges[id + 1].tasks.find(*it) == ranges[id + 1].tasks.end())) {
			// assert(tasks[*it].length - tasks[*it].processed <= rangeSize);
			consumed += tasks[*it].notProcessed();
			tasks[*it].processed = tasks[*it].length;
			tasksConsumed.push_back(*it);
		}
	}
	for (vector<int>::iterator it = tasksConsumed.begin(); it != tasksConsumed.end(); ++it) {
		ranges[id].tasks.erase(*it);
	}
	return consumed;
}

inline bool processPuzzleSingleRange(const int id, vector<Task>& tasks, vector<Range>& ranges, const int m)
{
	int rangeSize = ranges[id].rangeSize();
	int capacityLeft = m * rangeSize;
	
	capacityLeft -= consumeSingleRangeTasks(id, tasks, ranges);
	if (capacityLeft < 0) return false;

	set<pair<int, pair<int, int> > > tasksToPuzzle;
	set<int>::iterator it = ranges[id].tasks.begin();
	while (it != ranges[id].tasks.end()) {
		int consumeNow = 0;
		if ((tasks[*it].notProcessed() > 0) && (rangeSize > tasks[*it].getMargin())) {
			consumeNow = min(rangeSize - tasks[*it].getMargin(), tasks[*it].notProcessed());
			capacityLeft -= consumeNow;
			tasks[*it].processed += (rangeSize - tasks[*it].getMargin());
		}
		if (tasks[*it].notProcessed() > 0) {
			int toPuzzle = rangeSize - consumeNow;
			tasksToPuzzle.insert(make_pair(tasks[*it].winEnd, make_pair(toPuzzle, *it)));
		}

		ranges[id].tasks.erase(*it);
		it = ranges[id].tasks.begin();
	}

	if (capacityLeft < 0) return false;

	for (set<pair<int, pair<int, int> > >::iterator it = tasksToPuzzle.begin(); it != tasksToPuzzle.end(); ++it) {
		int toPuzzle = it->second.first;
		int taskId = it->second.second;

		if (capacityLeft >= toPuzzle) {
			int consumeNow = min(toPuzzle, tasks[taskId].notProcessed());
			capacityLeft -= consumeNow;
			tasks[taskId].processed += consumeNow;
		}
		else if (capacityLeft > 0) {
			int consumeNow = min(capacityLeft, tasks[taskId].notProcessed());
			tasks[taskId].processed += consumeNow;
			tasks[taskId].marginLost += toPuzzle - capacityLeft;
			capacityLeft -= consumeNow;
		}
		else {  // capacityLeft == 0
			tasks[taskId].marginLost += toPuzzle;
		}

		if ((tasks[taskId].notProcessed() > 0) && tasks[taskId].getMargin() < 0) {
			return false;
		}
	}

	return true;
}

inline void processPuzzle(vector<Task>& tasks, vector<Range>& ranges, const int m)
{
	for (int i = 0; i < ranges.size(); ++i) {
		if (!processPuzzleSingleRange(i, tasks, ranges, m)) {
			return;
		}
	}

	vector<Range> newRanges;
	for (vector<Range>::iterator it = ranges.begin(); it != ranges.end(); ++it) {
		if (it->tasks.size() != 0) {
			newRanges.push_back(*it);
		}
	}
	swap(newRanges, ranges);
}

int main() 
{
	int n, m;
	scanf("%d %d", &n, &m);

	vector<Task> tasks;
	vector<Range> ranges;
	for (int i = 0; i < n; ++i) {
		Task task;
		scanf("%d %d %d", &task.winStart, &task.winEnd, &task.length);
		task.processed = 0;
		task.marginLost = 0;
		tasks.push_back(task);
	}
	sort(tasks.begin(), tasks.end());

	bool result = false;
	do {
		int windowsMax = createRanges(tasks, ranges);

		if (windowsMax <= m) {
			result = true;
			break;
		} else if (missingCPUSlots(ranges, tasks, m)) {
			result = false;
			break;
		}

		processPuzzle(tasks, ranges, m);		
		result = ranges.empty();
	}  while (0);

	printf("%s\n", (result ? "TAK" : "NIE"));
	return 0;
}