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
#include<cstdio>
#include<algorithm>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>

#define FORE(c, a, b) for(int c=a; c<= int(b); ++c)
#define FORD(c, a, b) for(int c=a; c>= int(b); --c)
#define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ALL(a) a.begin(), a.end() 
#define PR(n) printf("%d ", (int) (n)) 
#define PRL(n) printf("%lld ", (ll) (n)) 
#define SC(n) scanf("%d ", &n) 

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define itr iterator

#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;

typedef vector<int> vi;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
typedef set<int> si;
typedef set<pi> spi;
typedef set<pii> spii;

const int NMAX = 1000 * 1000 + 9, TMAX = NMAX * 1000;

int n, k, ans, jj, jj2;
int a[NMAX], b[NMAX], p[NMAX], prevClient[NMAX], done[NMAX];
si zip, moments;
mi unzip, previousFree[NMAX];
vpi blocks[NMAX];
vpii events; // time, tool, client

int getLastGlobal(int time) { // -1 jak nie ma nikogo
	si::itr globalLast = moments.upper_bound(time);
	if(globalLast == moments.begin())
		return -1;
	globalLast--;
	return *globalLast;
}

int getFirstInRow(int tool, int time) { // -1 tylko nie ma w ogole nikogo, -2 jak pierwszy jest wolny 
	jj2++;
	prd("getLastFree tool %d time %d\n", tool, time);
	time = getLastGlobal(time);
	prd("global %d\n", time);
	if (time == -1) return -1;
	mi::itr afterMe = previousFree[tool].upper_bound(time);
	
	if(afterMe == previousFree[tool].begin()) { // tool nie uzywa nic w (..., time] a w (time, arg] nic nie ma
		prd("return1 global %d\n", -2);
		return -2;
	} else {
		afterMe--;
		prd("afterMe %d -> %d\n", afterMe->xx, afterMe->yy);
		if(afterMe->xx < time) {  // global pusty
			prd("return2 global %d\n", -2);
			return -2;
		}
			
		int time2 = getFirstInRow(tool, afterMe->yy - 1);
		prd("%d returned %d\n", afterMe->yy - 1, time2);			
		if(time2 < 0)
			afterMe->yy = afterMe->xx;
		else
			afterMe->yy = time2;
		prd("afterMe2 %d -> %d\n", afterMe->xx, afterMe->yy);
		prd("return3 %d\n", afterMe->yy);
		return afterMe->yy;
	}
}

int main() {
	scanf("%d%d", &n, &k);
	k = 0;
	REP(i, n) {
		scanf("%d%d%d", &a[i], &b[i], &p[i]);
		zip.insert(p[i]);
	}
	FORIT(sit, zip, si)
		unzip[*sit] = k++;
	REP(i, n) {
		p[i] = unzip[p[i]];
		blocks[p[i]].pb(mp(a[i], i));
	}
	REP(i, k)
		sort(ALL(blocks[i]));
		
	REP(tool, k) {
		prd("tool: %d\n", tool);
		int time = 0, i = 0, lastClient = -1;
		spi ends;
		pii lastReg;
		
		while(i < blocks[tool].size() || !ends.empty()) {
			while(i < blocks[tool].size() && blocks[tool][i].xx <= time) {
				int client = blocks[tool][i].yy;
				prd("time: %d add %d\n", time, client);
				ends.insert(mp(b[client], client));
				i++;
			}
			if(!ends.empty()) {
				spi::itr sit = ends.begin();
				int client = sit->yy;
				ends.erase(sit);
				
				if(time > b[client]) {
					printf("NIE\n");
					return 0;
				}
				
				prd("time: %d placeFor %d\n", time, client);
				events.pb(mp(time, mp(tool, client)));
				time++;
			} else if(i < blocks[tool].size()) {
				time = blocks[tool][i].xx;
			}		
		}
	}
	
	sort(ALL(events));
	reverse(ALL(events));
	moments.insert(TMAX);
	REP(i, k)
		previousFree[i][TMAX] = -1; 						  // mapy nigdy nie puste
		
	FORIT(eit, events, vpii) {
		int time = eit->xx, tool = eit->yy.xx, client = eit->yy.yy;
		int time2 = getFirstInRow(tool, b[client]);
		
		if(time2 >= 0) { // no ktos jest
			time2 = getLastGlobal(time2 - 1);
			if(time2 != -1)
				time = time2;
		}
		else if(time2 == -2) // pierwszy wolny!
			time = getLastGlobal(b[client]);
		// jak -1 to nic nie kombinujemy									
		
		previousFree[tool][time] = time;
		done[client] = time;
		moments.insert(time);
		prd("client %d takes time %d\n", client, time); // TODO
		/*jj++;
		if(jj % 1000 == 0)
			printf("%d %d\n", jj, jj2);*/
	}
	printf("%d\n", (int) moments.size() - 1);
	REP(i, n)
		printf("%d\n", done[i]);
	return 0;
}