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
// no, jakies 20-30MB wygralem
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

template<typename TH>
void debug_vars(const char* data, TH head){
    cerr << data << "=" << head << "\n";
}

template<typename TH, typename... TA>
void debug_vars(const char* data, TH head, TA... tail){
    while(*data != ',') cerr << *data++;
    cerr << "=" << head << ",";
    debug_vars(data+1, tail...);
}

#ifdef LOCAL
#define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#endif

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


const int MaxN  = 1000005;
const int Infty = 1010101010;


vector<int> treeIncr[MaxN], treeMin[MaxN], treeBase, treeSize;
multiset<pair<int,int>> minima;
priority_queue<pair<int,int>> firstEnds[MaxN];

int get_minimum(){
    return minima.begin()->first;
}
void add_one(int id, int L, int R, int what){
    //debug("add_one", id, L, R, what);
    if(L > R) return;
    int proot = treeMin[id][1];
    vector<int>& tmin  = treeMin[id],
               & tincr = treeIncr[id];

    L += treeBase[id]; R += treeBase[id];
    tmin[L] += what; tincr[L] += what;
    if(L != R){ tmin[R] += what; tincr[R] += what; }

    while(L/2 != R/2){
        if(L % 2 == 0){ tmin[L+1] += what; tincr[L+1] += what; }
        if(R % 2 == 1){ tmin[R-1] += what; tincr[R-1] += what; }
        L /= 2; R /= 2;
        tmin[L] = min(tmin[2*L], tmin[2*L+1]) + tincr[L];
        tmin[R] = min(tmin[2*R], tmin[2*R+1]) + tincr[R];
    }

    L /= 2;
    while(L){
        tmin[L] = min(tmin[2*L], tmin[2*L+1]) + tincr[L];
        L /= 2;
    }
    int nroot = tmin[1];

    minima.erase(minima.find(make_pair(proot,id)));
    minima.insert(make_pair(nroot,id));
}
int get_min(int id){
    vector<int>& tmin  = treeMin[id],
               & tincr = treeIncr[id];

    assert(treeSize[id] > 0);
    int L = treeBase[id], R = treeBase[id]+treeSize[id]-1;
    int minl = tmin[L], minr = tmin[R], minv;
    while(L/2 != R/2){
        if(L % 2 == 0){ minl = min(minl, tmin[L+1]); }
        if(R % 2 == 1){ minr = min(minr, tmin[R-1]); }
        L /= 2; R /= 2;
        minl += tincr[L]; minr += tincr[R];
    }
    minv = min(minl, minr); L /= 2;
    while(L){
        minv += tincr[L]; L /= 2;
    }
    return minv;
}
void init_tree(int id, vector<int> values){
    //for(int v : values) cerr << v << " "; cerr << endl;
    vector<int>& tmin  = treeMin[id],
               & tincr = treeIncr[id];

    int S = (int)values.size(), B = 1;
    while(B < S) B *= 2;
    treeBase[id] = B; treeSize[id] = S;
    tincr.resize(2*B);
    tmin.resize(2*B);

    for(int i = B; i < 2*B; i++){
        int val;
        if(i < B+S){
            val = values[i-B];
        } else {
            val = Infty;
        }
        tincr[i] = tmin[i] = val;
    }

    for(int i = B-1; i > 0; i--){
        tmin[i] = min(tmin[2*i], tmin[2*i+1]);
    }

    minima.insert(make_pair(tmin[1], id));
}


int N, K, M;
int leftTime[MaxN], rightTime[MaxN], queryType[MaxN], treePos[MaxN], result[MaxN];
vector<vector<pair<int,int>>> xPos;
vector<int> types;

void input(){
    scanf("%d%d", &N, &K);
    types.resize(N);
    xPos.resize(N);
    for(int i = 0; i < N; i++){
        scanf("%d%d%d", &leftTime[i], &rightTime[i], &queryType[i]);
        M = max(M, rightTime[i]);
        types[i] = queryType[i];
    }
    // redukujemy typy zapytan
    sort(types.begin(), types.end());
    types.resize(distance(types.begin(), unique(types.begin(), types.end())));
    int S = (int)types.size();
    for(int i = 0; i < N; i++){
        queryType[i] = (int)distance(types.begin(),
                lower_bound(types.begin(), types.end(), queryType[i]));
        assert(queryType[i] < S);
        xPos[queryType[i]].emplace_back(rightTime[i], i);
    }
    K = S;
    cerr << K << endl;

    types.clear(); types.shrink_to_fit();
}

void build_tree(int type){
    vector<pair<int,int>>& V = xPos[type];
    sort(V.begin(), V.end());
    vector<int> toInsert(V.size());

    for(int i = 0; i < (int)V.size(); i++){
        toInsert[i] = V[i].first - i;
        treePos[V[i].second] = i;
    }
    init_tree(type, toInsert);
}


int main(){
    input();
    treeBase.resize(K); treeSize.resize(K);
    for(int type = 0; type < K; type++){
        build_tree(type);
    }
    xPos.clear(); xPos.shrink_to_fit();

    queue<int> anyIntv;
    priority_queue<pair<int,int>> inserts;

    for(int i = 0; i < N; i++){
        inserts.push(make_pair(-leftTime[i], i));
    }

    int curTime = -inserts.top().first;
    int hoursNeeded = 0;

    while(true){
        int insertTime = (inserts.empty() ? Infty : -inserts.top().first);
        int removeTime = get_minimum();
        //debug(insertTime, removeTime);

        if(insertTime >= Infty && removeTime >= Infty) break;
        if(removeTime < curTime){
            printf("NIE\n");
            return 0;
        }

        if(insertTime <= removeTime){
            // dodajemy w tym czasie
            int who = inserts.top().second;
            inserts.pop();
            int qtype = queryType[who];
            if(firstEnds[qtype].empty()) anyIntv.push(qtype);
            firstEnds[queryType[who]].push(make_pair(-rightTime[who], who));
            curTime = insertTime;
        } else {
            // usuwamy w tym czasie
            queue<int> nextAnyIntv;
            while(!anyIntv.empty()){
                int id = anyIntv.front(); anyIntv.pop();
                int person = firstEnds[id].top().second;
                firstEnds[id].pop();
                if(!firstEnds[id].empty()) nextAnyIntv.push(id);
                result[person] = removeTime;

                int tpos = treePos[person];
                //debug(id, person, tpos, treeSize[id]);
                add_one(id, tpos, tpos, Infty);
                add_one(id, tpos+1, treeSize[id]-1, 1);
            }
            anyIntv = nextAnyIntv;
            hoursNeeded++;
            curTime = removeTime + 1;
        }

        //curTime = min(insertTime, removeTime);
    }

    printf("%d\n", hoursNeeded);
    for(int i = 0; i < N; i++){
        printf("%d\n", result[i]);
    }
    return 0;
}