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

using namespace std;

struct Procesik {
    int p;
    int k;
    int z;
    
    Procesik(int p, int k, int z)
    : p(p)
    , k(k)
    , z(z) {}

    bool operator<(const Procesik& p2) const{
        return k - z < p2.k - p2.z;
    }
};

int l, j, k, m, n;
vector<Procesik> pv;
vector<Procesik> actives;

bool funkcj(const Procesik& p1, const Procesik& p2){
    return p1.p < p2.p;
}

void pp(Procesik p1, int i){
    cout << p1.p << " " << p1.k << " " << p1.z << " (" << p1.k - i + 1 - p1.z << ")" << endl;
}

int main() {
    cin >> n >> m;
    int pmin = 1000001;
    int kmax = -1;
    for(int i = 0; i < n; ++i){
        cin >> l >> j >> k;
        j -= 1;
        if(l < pmin){
            pmin = l;
        }
        if(j > kmax){
            kmax = j;
        }
        pv.push_back(Procesik(l, j, k));
    }
    sort(pv.begin(), pv.end(), funkcj);
    int pidx = 0;
    // cout << "MIN/MAX:" << endl;
    // cout << pmin << " " << kmax << endl;
    for(int i = pmin; i <= kmax; ++i){
        // cout << "BEFORE MOMENT " << i << endl;
        while(true){
            if(pidx < n && pv[pidx].p <= i){
                actives.push_back(pv[pidx]);
                ++pidx;
            }else{
                break;
            }
        }
        sort(actives.begin(), actives.end());
        // cout << "ACTIVE PROCESSES:" << endl;
        // for(int j = 0; j < int(actives.size()); ++j){
        //     pp(actives[j], i);
        // }
        int posbs = min(int(actives.size()), m);
        // cout << "CAN EXECUTE " << posbs << endl;
        if(int(actives.size()) > posbs){
            if(actives[posbs].z > actives[posbs].k - i + 1){
                cout << "NIE" << endl;
                return 0;
            }
        }
        for(int i = 0; i < posbs; ++i){
            actives[i].z -= 1;
            if(actives[i].z == 0){
                actives.erase(actives.begin() + i);
                i--;
                posbs--;
            }
        }
    }
    
    if(actives.size() > 0){
        cout << "NIE" <<endl;
        return 0;
    }
    
    cout << "TAK" << endl;
    /*
    for(unsigned i = 0; i < pv.size(); ++i){
        cout << pv[i].p << " " << pv[i].k << " " << pv[i].z << endl;
    }
    */
    return 0;
}