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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <iomanip>
#include <iostream>
#include <queue>
#include <utility>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cstdlib>
#include <unordered_map>

using namespace std;

#define ALL(x) x.begin(), x.end()
#define VAR(a,b) __typeof (b) a = b
#define REP(i,n) for (int _n=(n), i=0; i<_n; ++i)
#define FOR(i,a,b) for (int _b=(b), i=(a); i<=_b; ++i)
#define FORD(i,a,b) for (int _b=(b), i=(a); i>=_b; --i)
#define FORE(i,a) for (VAR(i,a.begin ()); i!=a.end (); ++i) 
#define IN(x) int x; cin >> x
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

typedef vector<int> VI;
typedef long long LL;
typedef pair<int,int> PII;
typedef double LD;

int n;

struct request {
  int a, b;
  int id;
  int deadline;
  request(int a, int b, int id) : a(a), b(b), id(id) {}
};

ostream& operator<<(ostream& o, const request& r) {
  return o << "(" << r.a << ", " << r.b << ", " << r.id << ")";
}

struct compare_by_neg_end {
  bool operator()(const request& r1, const request& r2) const {
    return -r1.b < -r2.b;
  }
};

struct compare_by_beg {
  bool operator()(const request& r1, const request& r2) const {
    return r1.a < r2.a;
  }
};

struct compare_by_neg_beg {
  bool operator()(const request& r1, const request& r2) const {
    return -r1.a < -r2.a;
  }
};

struct compare_by_end {
  bool operator()(const request& r1, const request& r2) const {
    return r1.b < r2.b;
  }
};

struct per_machine {
  vector<request> requests;
  priority_queue<int, vector<int>, greater<int>> deadlines;
  priority_queue<request, vector<request>, compare_by_neg_end> active;
  int num;
  void dump() const {
    //cout << "requests: \n";
    //FORE(it, requests) cout << *it << endl;
    //cout << "end of requests" << endl;
    //FORE(it, deadlines) cout << *it << endl;
    //cout << "end of deadlines\n";
    //FORE(it, active)
    //  cout << *it << endl;
    //cout << "end of active" << endl;
    cout << "end of dump of " << num << endl << endl << endl;
  }
  int next_event() const {
    int val = int(1e9) + 1;
    if (!deadlines.empty()) val = min(val, deadlines.top());
    if (!requests.empty()) val = min(val, requests.back().a);
    return val;
  }
  bool can_move() const {
    if (requests.empty()) return false;
    if (deadlines.empty()) return true;
    return requests.back().a <= deadlines.top();
  }
  int move_until_now(int now) {
    int cnt = 0;
    while (!requests.empty() && requests.back().a == now) {
      auto request = requests.back();
      requests.pop_back();
      deadlines.push(request.deadline);
      active.push(request);
      assert(deadlines.size() == active.size());
      ++cnt;
    }
    int new_now = next_event();
    assert(new_now >= now);
    return cnt;
  }
};

void process(int p, per_machine& mach) {
  sort(ALL(mach.requests), compare_by_neg_end());
  reverse(ALL(mach.requests));
  vector<request> sorted;
  priority_queue<request, vector<request>, compare_by_beg> pq;
  int next_to_alloc = int(1e9);
  do {
    while (!mach.requests.empty() && mach.requests.back().b == next_to_alloc) {
      auto request = mach.requests.back(); mach.requests.pop_back();
      pq.push(request);
    }
    if (pq.empty()) {
      if (mach.requests.empty()) break;
      assert(mach.requests.back().b < next_to_alloc);
      next_to_alloc = mach.requests.back().b;
      continue;
    }
    auto nxt = pq.top();
    pq.pop();
    if (nxt.a > next_to_alloc) {
      cout << "NIE" << endl;
      exit(0);
    }
    nxt.deadline = next_to_alloc;
    sorted.PB(nxt);
    --next_to_alloc;
  } while (true);
  sort(ALL(sorted), compare_by_neg_beg());
  swap(sorted, mach.requests);
  mach.num = p;
  //cout << "######" << endl;
  //cout << p << endl;
  //FORE(it, mach.requests) cout << "(" << it->a << ", " << it->b << ") ";
  //cout << endl;
  //cout << "######" << endl << endl;
}

int res = 0;

const int MAXN = int(1e6);

int alloted[MAXN];

unordered_map<int, per_machine> per_mach;

struct compare_machine {
  bool operator()(const per_machine* mach1, const per_machine* mach2) const {
    int event1 = mach1->next_event(), event2 = mach2->next_event();
    if (event1 != event2) return -event1 < -event2;
    if (mach2->can_move() && !mach1->can_move()) return true;
    return false;
  }
};

int cnt = 0;

void run2() {
  vector<per_machine*> creator, active_vec;
  creator.reserve(per_mach.size());
  for (auto& it : per_mach) creator.PB(&it.second);
  priority_queue<per_machine*, vector<per_machine*>, compare_machine>
    inactive(creator.begin(), creator.end()), active;
  creator.clear();
  int prev_now = -1;
  while (!inactive.empty() || !active.empty()) {
    int next_inactive = int(1e9) + 1, next_active = int(1e9) + 1;
    if (!inactive.empty()) {
      per_machine* nxt = inactive.top();
      next_inactive = nxt->next_event();
    }
    if (!active.empty()) {
      per_machine* nxt = active.top();
      next_active = nxt->next_event();
    }
    if (next_inactive <= next_active) {
      per_machine* nxt = inactive.top();
      inactive.pop();
      int now = nxt->next_event();
      assert(now >= prev_now);
      prev_now = now;
      assert(!nxt->requests.empty());
      int moved = nxt->move_until_now(now);
      assert(moved > 0);
      active.push(nxt);
      active_vec.PB(nxt);
    }
    else {
      assert(!active.empty());
      per_machine* nxt = active.top();
      active.pop();
      int now = nxt->next_event();
      assert(now >= prev_now);
      prev_now = now;
      assert(now <= int(1e9));
      int moved = nxt->move_until_now(now);
      active.push(nxt);
      if (moved > 0) continue;
      ++res;
      FORE(it, active_vec) {
        per_machine* nxt = *it;
        auto request = nxt->active.top();
        int this_now = nxt->next_event();
        nxt->active.pop();
        assert(request.a <= now);
        assert(now <= request.b);
        alloted[request.id] = now;
        ++cnt;
        nxt->deadlines.pop();
        if (nxt->active.empty()) {
          if (nxt->requests.empty()) continue;
          assert(this_now <= nxt->next_event());
          inactive.push(nxt);
        }
        else {
          assert(this_now <= nxt->next_event());
          creator.PB(nxt);
        }
      }
      active_vec = creator;
      active =
        priority_queue<per_machine*, vector<per_machine*>, compare_machine>(creator.begin(), creator.end());
      creator.clear();
    }
  }
  assert(cnt == n);
}

//struct compare_inactive {
//  bool operator()(const per_machine* mach1, const per_machine* mach2) const {
//    assert(mach1->next_pos < mach1->requests.size());
//    assert(mach2->next_pos < mach2->requests.size());
//    return mach1->requests[mach1->next_pos].a < mach2->requests[mach2->next_pos].a;
//  }
//};
//
//struct compare_active {
//  bool operator()(const per_machine* mach1, const per_machine* mach2) const {
//    assert(mach1->next_pos < mach1->deadline.size());
//    assert(mach2->next_pos < mach2->deadline.size());
//    return mach1->deadline[mach1->next_pos] < mach2->deadline[mach2->next_pos];
//  }
//};

//void run() {
//  multiset<per_machine*, compare_inactive> inactive;
//  for (auto& it : per_mach)
//    inactive.insert(&it.second);
//  multiset<per_machine*, compare_active> active;
//  while (!inactive.empty() || !active.empty()) {
//    int next_inactive = int(1e9) + 1, next_active = int(1e9) + 1;
//    if (!inactive.empty()) {
//      per_machine* nxt = *inactive.begin();
//      next_inactive = nxt->requests[nxt->next_pos].a;
//    }
//    if (!active.empty()) {
//      per_machine* nxt = *active.begin();
//      next_active = nxt->deadline[nxt->next_pos];
//    }
//    //cout << "inactive = " << next_inactive << ", active =  " << next_active << endl;
//    if (next_inactive <= next_active) {
//      per_machine* nxt = *inactive.begin();
//      inactive.erase(inactive.begin());
//      active.insert(nxt);
//    }
//    else {
//      per_machine* nxt = *active.begin();
//      int deadline = nxt->deadline[nxt->next_pos];
//      ++res;
//      multiset<per_machine*, compare_active> new_active;
//      FORE(it, active) {
//        per_machine* nxt = *it;
//        //cout << nxt->requests[nxt->next_pos] << " " << deadline << endl;
//        //cout << nxt->next_pos << " " << nxt->requests.size() << endl;
//        alloted[nxt->requests[nxt->next_pos].id] = deadline;
//        if (++nxt->next_pos == nxt->requests.size()) continue;
//        //cout << "hey" << endl;
//        if (nxt->requests[nxt->next_pos].a <= deadline)
//          new_active.insert(nxt);
//        else
//          inactive.insert(nxt);
//      }
//      swap(active, new_active);
//    }
//  }
//}

void read_input() {
  cin >> n;
  { int k; cin >> k; /* ignore k */ }
  REP(i, n) {
    IN(a); IN(b); IN(p);
    if (!per_mach.count(p)) per_mach[p] = per_machine();
    per_mach[p].requests.PB(request(a, b, i));
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cout.setf(ios::fixed);
  read_input();
  for (auto& it: per_mach)
    process(it.first, it.second);
  run2();
  cout << res << endl;
  REP(i, n) cout << alloted[i] << endl;
  return 0;
}