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
/* 2016
 * Maciej Szeptuch
 */
#include <cstdio>
#include <algorithm>
#include <vector>

const int MAX_TASKS = 128;
const int MAX_MACHINES = 128;
const int MAX_TIME = 1048576;

struct Task
{
    int start;
    int end;
    int length;

    bool operator<(const Task &cmp) const
    {
        if(end < cmp.end)
            return true;

        if(end > cmp.end)
            return false;

        if(start < cmp.start)
            return true;

        if(start > cmp.start)
            return false;

        return length < cmp.length;
    }
};

typedef std::vector<std::pair<int, int>> Machine;
typedef std::vector<std::pair<std::pair<int, int>, int>> Possibilities;

Task task[MAX_TASKS];
int machines;
int tasks;
Machine machine[MAX_MACHINES];

void calculate_possibilities(const Task &_task, int m, Possibilities &possibilities);
void use_machine(int m, int from, int to);

int main(void)
{
    scanf("%d %d", &tasks, &machines);
    for(int t = 0; t < tasks; ++ t)
        scanf("%d %d %d", &task[t].start, &task[t].end, &task[t].length);

    for(int m = 0; m < machines; ++ m)
        machine[m].push_back(std::make_pair(-1, MAX_TIME));

    std::sort(task, task + tasks);
    for(int t = 0; t < tasks; ++ t)
    {
        Possibilities possibilities;
        for(int m = 0; m < machines; ++ m)
            calculate_possibilities(task[t], m, possibilities);

        std::sort(std::begin(possibilities), std::end(possibilities));
        for(auto possibility: possibilities)
        {
            if(!task[t].length)
                break;

            if(possibility.first.second < task[t].start)
                continue;

            task[t].start = std::max(task[t].start, possibility.first.first);
            int end_time = std::min(possibility.first.second, task[t].start + task[t].length);
            use_machine(possibility.second, task[t].start, end_time);
            task[t].length -= end_time - task[t].start;
            task[t].start = end_time;
        }

        if(task[t].length)
        {
            puts("NIE");
            return 0;
        }
    }

    puts("TAK");
    return 0;
}

inline
void calculate_possibilities(const Task &_task, int m, Possibilities &possibilities)
{
    std::pair<int, int> needle{_task.start, MAX_TIME};
    auto it = std::upper_bound(std::begin(machine[m]), std::end(machine[m]), needle) - 1;
    while(it != std::end(machine[m]) && it->first < _task.end)
    {
        if(it->second <= _task.start)
        {
            ++ it;
            continue;
        }

        std::pair<std::pair<int, int>, int> possibility;
        possibility.second = m;
        possibility.first.first = std::max(it->first, _task.start);
        possibility.first.second = std::min(it->second, _task.end);
        possibilities.push_back(possibility);
        ++ it;
    }
}

inline
void use_machine(int m, int from, int to)
{
    std::pair<int, int> needle{from, MAX_TIME};
    auto it = std::upper_bound(std::begin(machine[m]), std::end(machine[m]), needle) - 1;
    if(it->first == from && it->second == to)
    {
        machine[m].erase(it);
        return;
    }

    if(it->second == to)
    {
        it->second = from;
        return;
    }

    if(it->first == from)
    {
        it->first = to;
        return;
    }

    std::pair<int, int> fill{it->first, from};
    it->first = to;
    machine[m].insert(it, fill);
}