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
#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

struct Event {
	bool start;
	int index;
	int time;
	bool operator<(const Event& other) const {
		return time < other.time || (time == other.time && start < other.start);
	}
};

int main() {
    int n, m;
	std::ios_base::sync_with_stdio(false);
    std::cin >> n >> m;
    std::vector<int> starts(n), times(n);
    std::vector<Event> events(n << 1);
    for (int i = 0; i < n; i++) {
		Event& first = events[i << 1];
		Event& last = events[i << 1 | 1];
		first.start = true;
		last.start = false;
		first.index = last.index = i;
		std::cin >> first.time >> last.time >> times[i];
		starts[i] = first.time;
    }
    std::sort(events.begin(), events.end());
    std::vector<int> stepsSince(n);
    std::set<int> activeTasks;
    int lastEvent = 0;
    for (const auto& event : events) {
    	int value = (event.time - lastEvent) * std::min(m, (int)activeTasks.size());
    	lastEvent = event.time;
		for (const auto& task : activeTasks) {
			stepsSince[task] += value;
		}
		if (event.start) {
			activeTasks.insert(event.index);
		} else {
			for (const auto& task : activeTasks) {
				stepsSince[task] -= times[event.index];
				if (stepsSince[task] < 0) {
					if (task == event.index) {
						std::cout << "NIE\n";
						return 0;
					}
					stepsSince[task] = 0;
				}
			}
			activeTasks.erase(event.index);
		}
    }
	std::cout << "TAK\n";
    return 0;
}