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
// tested by Hightail: https://github.com/dj3500/hightail
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

int aw,bw,x; // temp
struct Tree {
   int *incr, *minn, n;

   void zwieksz2 (int v, int a, int b) {
      if (aw > b || bw < a) return;
      if (a >= aw && b <= bw) {
         incr[v] += x;
         minn[v] += x;
      } else {
         int mid = (a+b)/2;
         zwieksz2(2*v, a, mid);
         zwieksz2(2*v+1, mid+1, b);
         minn[v] = incr[v] + min(minn[2*v], minn[2*v + 1]);
      }
   }

   void zwieksz (int _aw, int _bw, int _x) {
      aw = _aw; bw = _bw; x = _x;
      zwieksz2(1,0,n-1);
   }

   void build (int v, int a, int b) {
      if (a > b) return; // chyba nigdy
      if (a == b) {
         minn[v] = mapping[a].fi - mapping[a].se;
         incr[v] = 0;
      } else {
         int mid = (a+b) / 2;
         build(2*v, a, mid);
         build(2*v+1, mid+1, b);
         minn[v] = min(minn[2*v], minn[2*v + 1]);
         incr[v] = 0;
      }
   }

   pii *mapping; // mapping[shortS] = {s, cnt}
   Tree (const vi &vals) { // vals: sorted
      int prevVal = -1;
      n = 0;
      for (int val : vals) {
         if (prevVal != val) {
            ++n;
            prevVal = val;
         }
      }
      mapping = new pii[n];

      prevVal = -1;
      int q = 0;
      for (int val : vals) {
         if (prevVal != val) {
            mapping[q++] = mp(val,1);
            prevVal = val;
         } else {
            mapping[q-1].se++;
         }
      }
      //n = SZ(mapping);
      int sz = 1;
      while (sz < 2 * n) sz *= 2;
      incr = new int[sz];
      minn = new int[sz];
      /*FOR(i,sz) incr[i] = 0;
      FOR(i,sz) minn[i] = 0;
      int cnt = 0;
      FOR(i,n) {
         cnt += mapping[i].se;
         zwieksz(i,i,mapping[i].fi - cnt);
      }*/
      REP(i,1,n-1) mapping[i].se += mapping[i-1].se;
      build(1,0,n-1);
      REPD(i,n-1,1) mapping[i].se -= mapping[i-1].se;
   }
   int getMin () {
      return minn[1];
   }
   void remove (int val) {
      // wywal jedno wystapienie v, i jesli to bylo ostatnie, to ustaw tam INF
      // i zaplusuj w prawo
      int shortS = lower_bound(mapping, mapping+n, mp(val, -1)) - mapping;
      mapping[shortS].se--;
      if (mapping[shortS].se == 0) {
         zwieksz(shortS, shortS, INF);
      }
      zwieksz(shortS, n-1, 1);
   }
};

const int N = 1000007;
int jobStart[N], jobEnd[N], jobEq[N], res[N];

struct comp {
   inline bool operator () (int v, int w) const {
      if (jobEnd[v] != jobEnd[w]) return jobEnd[v] > jobEnd[w];
      return v > w;
   }
};

struct Eq {
   vector<pii> jobsForIF; // jobs for initialFeasible
   vi jobEnds;

   void addJob (int i) {
      jobsForIF.pb(mp(jobStart[i], jobEnd[i]));
      jobEnds.pb(jobEnd[i]);
   }

   bool initiallyFeasible () {
      sort(jobsForIF.rbegin(), jobsForIF.rend());
      priority_queue<int,vector<int>,greater<int>> deadlinesOfActive;
      for (int t = 1; ; ) {
         while (!jobsForIF.empty() && jobsForIF.back().fi == t) {
            deadlinesOfActive.push(jobsForIF.back().se);
            jobsForIF.pop_back();
         }
         if (!deadlinesOfActive.empty()) {
            if (deadlinesOfActive.top() < t) {
               return false;
            }
            deadlinesOfActive.pop();
            ++t;
         } else {
            // brak aktywnych
            if (jobsForIF.empty()) {
               return true;
            }
            // fast-forward do chwili gdy cos bedzie sie dzialo
            t = jobsForIF.back().fi;
         }
      }
   }

   priority_queue<int,vector<int>,comp> activeDeadlines; // uwaga: zawiera indeksy i

   void notifyThatJobIsStarting (int i) {
      activeDeadlines.push(i);
   }

   Tree *tr;
   int nextForce;

   void setup () {
      sort(ALL(jobEnds));
      tr = new Tree(jobEnds);
      nextForce = tr->getMin() + 1;
   }

   bool isActive () const {
      return !activeDeadlines.empty();
   }

   void makeMove (int t) {
      int i = activeDeadlines.top();
      activeDeadlines.pop();
      res[i] = t;
      tr->remove(jobEnd[i]);
      nextForce = tr->getMin() + 1;
   }
};

// fast io
const int max_size=100000;
char buf[max_size];
int buf_size=0,pos=0;

#define getbuf() buf_size=fread(buf,1,max_size,stdin)
#define get(tok) {if (pos>=buf_size) { if (feof(stdin)) tok=0;	else { getbuf();pos=0; tok=buf[pos++];}}else tok=buf[pos++];}
#define in(v) {int tok;get(tok);while (tok==' ' || tok=='\n')get(tok);v=tok-'0';get(tok);while(tok>='0' && tok<='9'){v*=10;v+=(tok-'0');get(tok);}}
// koniec fast io

int main () {
   int n, syf;
   in(n);
   in(syf);
   pii *sorter; // {<eq, i}>
   sorter = new pii[n];
   FORI(i,n) {
      //scanf("%d %d %d", jobStart+i, jobEnd+i, jobEq+i);
      in(jobStart[i]);
      in(jobEnd[i]);
      in(jobEq[i]);
      sorter[i-1] = mp(jobEq[i], i);
   }
   sort(sorter, sorter+n);
   int prevEq = -1, k = 0;
   FOR(i,n) {
      if (prevEq != sorter[i].fi) ++k;
      prevEq = sorter[i].fi;
      jobEq[sorter[i].se] = k-1;
   }
   delete [] sorter;
   // przyrzady od 0 do k-1
   Eq *eq = new Eq[k];
   FORI(i,n) eq[jobEq[i]].addJob(i);

   FOR(j,k) if (!eq[j].initiallyFeasible()) {
      printf("NIE");
      return 0;
   }

   priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>> events;
   FORI(i,n) events.push(mp(jobStart[i], mp(0, i)));
   FOR(j,k) {
      eq[j].setup();
      events.push(mp(eq[j].nextForce, mp(1, j)));
   }

   int movesMade = 0;
   set<int> activeEq;
   while (!events.empty()) {
      int t = events.top().fi, type = events.top().se.fi, index = events.top().se.se;
      events.pop();

      if (t > 1000000000) break; // koniec

      if (type == 0) {
         eq[jobEq[index]].notifyThatJobIsStarting(index);
         //assert(eq[jobEq[index]].isActive());
         activeEq.insert(jobEq[index]);
      } else { // type == 1
         if (eq[index].nextForce != t) {
            continue; // olej
         }
         // index jest sforsowany
         //assert(activeEq.count(index));
         ++movesMade;
         for (set<int>::iterator j = activeEq.begin(); j != activeEq.end(); ) {
            eq[*j].makeMove(t);
            events.push(mp(eq[*j].nextForce, mp(1,*j)));
            set<int>::iterator temp = j;
            ++j;
            if (!eq[*temp].isActive()) {
               activeEq.erase(temp);
            }
         }
      }
   }

   pisz(movesMade);
   FORI(i,n) pisz(res[i]);
}

// TODO: fast IO
// TODO: wywal asserta