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
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
//#define REP(i,n) FOR(i,1,(n)+1)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
//#define VAR(n,v) typeof(v) n=(v)
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)

#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

struct task {
	int u, v, c;
	bool operator<(const task &t) const {
		int r1 = v-u-c;
		int r2 = t.v-t.u-t.c;
		return tie(u,v,c) < tie(t.u,t.v,t.c);
	}
};


enum event_type {NEW_TASK, TIMEOUT};

struct event {
	event_type type;
	int task_id;
	int time;
	bool operator<(const event &e) const {
		return tie(time,type,task_id) > tie(e.time,e.type,e.task_id);
	}
};

struct waiting_task {
	int task_id;
	int timeout;
	bool operator<(const waiting_task &t) const {
		return tie(timeout, task_id) > tie(t.timeout, t.task_id);
	}
	
};

void finish_tasks(const vector<task> &processor, const int time, int &running,
		int &running_from, vi &run_times,
		priority_queue<waiting_task> &waiting, set<int> &finished) {

	while(running != -1) {
		int finish_time = running_from + processor[running].c - run_times[running];
		if(finish_time <= time) {
			dprintf("finishing %d\n", running);
			finished.insert(running);
			if(!waiting.empty()) {
				running = waiting.top().task_id;
				waiting.pop();
				running_from = finish_time;
				dprintf("running %d from %d\n", running, running_from);
			} else {
				running = -1;
			}
		} else break;
	}	
}

bool check(vector<task> processor) {
	dprintf("------------------ check t = ");
	FOR(i,0,processor.sz) {
		dprintf("(%d,%d,%d) ", processor[i].u, processor[i].v, processor[i].c);
	}
	dprintf("  -------\n");
	priority_queue<event> events;

	FOR(i,0,processor.sz) {
		task &t = processor[i];
		events.push({NEW_TASK, i, t.u});
		events.push({TIMEOUT, i, t.v});
	}

	int running = -1;
	int running_from = 0;
	vi run_times(processor.sz, 0);
	set<int> finished;
	priority_queue<waiting_task> waiting;

	while(!events.empty()) {
		event e = events.top();
		events.pop();
		
		finish_tasks(processor, e.time, running, running_from, run_times, waiting, finished);
		if(e.type == NEW_TASK) {
			dprintf("event) NEW_TASK %d (time=%d)\n", e.task_id, e.time);
			if(-1 == running) {
				running = e.task_id;
				running_from = e.time;
				dprintf("running %d from %d\n", running, running_from);
			} else {
				if(processor[e.task_id].v < processor[running].v) {
					run_times[running] += e.time - running_from;
					waiting.push({running, processor[running].v});
					running = e.task_id;
					running_from = e.time;
					dprintf("running %d from %d\n", running, running_from);
				} else {
					waiting.push({e.task_id, processor[e.task_id].v});
				}
			}
		} else if(e.type == TIMEOUT) {
			dprintf("event) TIMEOUT %d (time=%d)\n", e.task_id, e.time);
			finish_tasks(processor, e.time, running, running_from, run_times, waiting, finished);
			if(!finished.count(e.task_id)) {
				return false;
			}
		}
	}
	return true;
}

////////////////////////////////////////////////////////////////////////////////

struct free_processor {
	int proc_id;
	int free_since;
	bool operator<(const free_processor &t) const {
		return tie(free_since, proc_id) > tie(t.free_since, t.proc_id);
	}
	
};

void finish_tasks2(int p, const vector<task> &tasks, const int time, vi &running,
		vi &running_from, vi &run_times,
		priority_queue<waiting_task> &waiting, set<int> &finished) {

	priority_queue<free_processor> free;
	FOR(i,0,p) {
		if(running[i] == -1) {
			free.push({i, time});
		} else {
			int finish_time = running_from[i] + tasks[running[i]].c - run_times[running[i]];
			if(finish_time <= time) {
				dprintf("finishing %d\n", running[i]);
				finished.insert(running[i]);
				free.push({i, finish_time});
			}
		}
	}
	while(!free.empty() && !waiting.empty()) {
		free_processor proc = free.top();
		int i = proc.proc_id;
		free.pop();
		running[i] = waiting.top().task_id;
		waiting.pop();
		running_from[i] = proc.free_since;

		int finish_time = running_from[i] + tasks[running[i]].c - run_times[running[i]];
		if(finish_time <= time) {
			dprintf("finishing %d\n", running[i]);
			finished.insert(running[i]);
			free.push({i, finish_time});
		}
	}	
}

bool check2(int p, vector<task> tasks) {
	priority_queue<event> events;

	FOR(i,0,tasks.sz) {
		task &t = tasks[i];
		events.push({NEW_TASK, i, t.u});
		events.push({TIMEOUT, i, t.v});
	}

	vi running(p, -1);
	vi running_from(p, 0);
	vi run_times(tasks.sz, 0);
	set<int> finished;
	priority_queue<waiting_task> waiting;

	while(!events.empty()) {
		event e = events.top();
		events.pop();
		
		finish_tasks2(p, tasks, e.time, running, running_from, run_times, waiting, finished);
		if(e.type == NEW_TASK) { //TODO: consider checking free
			dprintf("event) NEW_TASK %d (time=%d)\n", e.task_id, e.time);
			bool ran = false;

			FOR(i,0,p) {
				if(-1 == running[i]) {
					running[i] = e.task_id;
					running_from[i] = e.time;
					dprintf("running %d from %d on proc %d\n", running[i], running_from[i], 'A' + i);
					ran = true;
					break;
				}
			}
			if(ran) continue;
			int best = 0;
			FOR(i,1,p) {
				if(tasks[running[best]].v < tasks[running[i]].v) {
					best = i;
				}
			}

			if(tasks[e.task_id].v < tasks[running[best]].v) {
				run_times[running[best]] += e.time - running_from[best];
				waiting.push({running[best], tasks[running[best]].v});
				running[best] = e.task_id;
				running_from[best] = e.time;
				dprintf("running %d from %d on proc %d\n", running[best], running_from[best], 'A' + best);
				ran = true;
			}
			if(ran) continue;
			waiting.push({e.task_id, tasks[e.task_id].v});
		} else if(e.type == TIMEOUT) {
			dprintf("event) TIMEOUT %d (time=%d)\n", e.task_id, e.time);
			if(!finished.count(e.task_id)) {
				return false;
			}
		}
	}
	return true;
}


int main() {
	GET(n);
	GET(p);
	vector<task>tasks;
	vector<vector<task>> processors(p);
	bool ok = true;
	while(n--) {
		GET(u);
		GET(v);
		GET(c);
		tasks.pb({u,v,c});
	}
	sort(ALL(tasks));
	for(auto &t : tasks) {
		bool processor_found = false;
		FOR(i,0,p) {
			vector<task> to_check = processors[i];
			to_check.pb(t);
			if(check(to_check)) {
				processor_found = true;
				processors[i] = to_check;
				break;
			}
		}
		if(!processor_found) {
			ok = false;
			break;
		}
	}

	if(!ok) ok = check2(p, tasks);
	printf("%s\n", ok ? "TAK" : "NIE");
	return 0;
}