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

using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, int> tri;

vector<pii> events[1000001];
int ending[1000001];
set<pii> que;
int req[101];

/*bool throwing(int task, int ktory, int akt)
{
    if(req[task] == 0 )
        return true;
    
    if()
}*/

bool check(int m, int maxn)
{
    vector<int> helper;
    for(int i=0; i<=maxn+1; i++)
    {
        helper.clear();
        //cout << i << ":\n";

        for(int j=0; j<events[i].size(); j++)
        {
            int tem = events[i][j].second; 
            que.insert(pii( (ending[tem] - req[tem] + 1) , tem) );
        } 

        int j = 0;

        set<pii>::iterator it;
        //for(set<pii>::iterator it = que.begin(); it != que.end() && j<m; j++)
        while(!que.empty() && j < m)
        {
            it = que.begin();

            //cout << (*it).first << " " << (*it).second << "\n";
 
            int el = (*it).second;

            que.erase(it);

            if(ending[el] - req[el] < i-1)
                return false;

            req[el]--;
            j++;

            if(req[el] == 0 )
            {
                continue;
            }
            else if (ending[el] - req[el] < i)
                return false;
            
            helper.push_back(el);

        }


        //cout << "dupa\n";

        for(int k=0; k < helper.size(); k++)
        {
            int cur = helper[k];
            que.insert( pii(ending[cur] - req[cur] + 1, cur) );
        }
        /*
        while(!que.empty() && (*que.begin()).first == i)
        {
            if(req[(*que.begin()).second] != 0) 
                return false;
                
            que.erase(que.begin() );
        }*/       
    }

    return true;
}

int main()
{
    ios_base::sync_with_stdio(0);

    int n, m;
    
    cin >> n >> m;

    int maksi = 0;
    for(int i=0; i<n; i++)
    {
        int p, k, c;
        cin >> p >> ending[i] >> req[i];

        ending[i]--;
        events[p].push_back(pii(1, i) );
        maksi = max(maksi, ending[i] );
    }

    if(check(m, maksi) )
    {
        cout << "TAK\n";
    }
    else
    {
        cout << "NIE\n";
    }


    return 0;
}