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
#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long int64;
typedef pair<int, int> PII;
typedef pair<int64, int64> PLL;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

const int INF = 1010 * 1000 * 1000;

// k - numer maszyny
// i - numer na wejsciu
// j - numer na mintree koncow
struct request{
	int a, b, k, i, j;
};

bool cmpK(request a, request b){
	return a.k < b.k;
}

bool cmpA(request a, request b){
	return a.a < b.a;
}

void increase(vector<PII> &tree, int u, int lo, int hi, int a, int b){
	if(a <= lo && hi <= b){
		++tree[u].st;
		++tree[u].nd;
		return;
	}
	if(hi <= a || b <= lo){
		return;
	}
	increase(tree, 2*u, lo, (lo+hi)/2, a, b);
	increase(tree, 2*u+1, (lo+hi)/2, hi, a, b);
	tree[u].st = min(tree[2*u].st, tree[2*u+1].st) + tree[u].nd;
}

void reset(vector<PII> &tree, int u){
	tree[u].st = INF;
	u /= 2;
	while(u){
		tree[u].st = min(tree[2*u].st, tree[2*u+1].st) + tree[u].nd;
		u /= 2;
	}
}

int n, k, res;
vector<request> reqs;
int D[1000010];
vector<PII> tree[1000010];
int ans[1000010];
priority_queue<PII> Q;
priority_queue<PII> waiting[1000010];
vector<int> active;
bool act[1000010];

bool solve(){
	int s = 0;
	int t, i;
	int p = 0;
	FWD(j,0,k){
		Q.push(PII(-tree[j][1].st, j));
		//printf("musze uzyc %d w %d\n", j, tree[j][1].st);
	}
	sort(reqs.begin(), reqs.end(), cmpA);
	while(!Q.empty()){
		t = -Q.top().st;
		i = Q.top().nd;
		Q.pop();
		if(tree[i][1].st != t) continue;
		if(t <= s){
			//printf("dwa razy pod rzad\n");
			return 0;
		}
		s = t;
		//printf("uzywam wszystkiego w %d\n", t);
		++res;
		while(p < SIZE(reqs) && reqs[p].a <= t){
			//printf("aktywny [%d %d]\n", reqs[p].a, reqs[p].b);
			if(!act[reqs[p].k]){
				act[reqs[p].k] = 1;
				active.push_back(reqs[p].k);
			}
			waiting[reqs[p].k].push(PII(-reqs[p].b, p));
			++p;
		}
		FWD(j,0,SIZE(active)){
			int m = active[j];
			if(-waiting[m].top().st < t){
				assert(0);
			}
			int q = waiting[m].top().nd;
			waiting[m].pop();
			if(waiting[m].empty()){
				act[m] = 0;
				swap(active[j], active.back());
				active.pop_back();
				--j;
			}
			ans[reqs[q].i] = t;
			increase(tree[reqs[q].k], 1, 0, D[reqs[q].k], reqs[q].j, D[reqs[q].k]);
			reset(tree[reqs[q].k], D[reqs[q].k] + reqs[q].j);
			if(tree[reqs[q].k][1].st < INF)
				Q.push(PII(-tree[reqs[q].k][1].st, reqs[q].k));
		}
	}
	FWD(j,0,n) if(!ans[j]) return 0;
	return 1;
}

int main(){
	scanf("%d %d", &n, &k);
	FWD(i,0,n){
		request r;
		scanf("%d %d %d", &r.a, &r.b, &r.k);
		r.i = i;
		reqs.push_back(r);
	}
	sort(reqs.begin(), reqs.end(), cmpK);

	k = 0;
	vector<PII> ends;
	FWD(i,0,n){
		int ok = reqs[i].k;

		reqs[i].k = k;
		ends.push_back(PII(reqs[i].b, i));

		if(i+1 == n || ok != reqs[i+1].k){
			sort(ends.begin(), ends.end());

			int d = 1;
			while(d < SIZE(ends)) d *= 2;
			D[k] = d;
			tree[k].resize(2*d);

			FWD(j,0,SIZE(ends)){
				reqs[ends[j].nd].j = j;
				tree[k][d+j] = PII(ends[j].st - j, 0);
			}
			FWD(j,SIZE(ends),d){
				tree[k][d+j] = PII(INF, 0);
			}
			BCK(j,d-1,0){
				tree[k][j].st = min(tree[k][2*j].st, tree[k][2*j+1].st);
				tree[k][j].nd = 0;
			}

			ends.clear();
			++k;
		}
	}

	if(!solve()){
		printf("NIE\n");
		return 0;
	}
	printf("%d\n", res);
	FWD(i,0,n) printf("%d\n", ans[i]);
	return 0;
}