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
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

class Bi;
class Pabi;

class Pabi {
 public:
  void Read(const int i) {
    i_ = i;
    scanf("%d%d%d", &a_, &b_, &p_);
  }

  int get_p() const { return p_; }
  int get_a() const { return a_; }
  int get_b() const { return b_; }
  int get_i() const { return i_; }

  void set_p(const int p) { p_ = p; }
  void set_a(const int a) { a_ = a; }
  void set_b(const int b) { b_ = b; }
  void set_i(const int i) { i_ = i; }

  bool operator<(const Pabi& rhs) const {
    if (p_ != rhs.p_) return p_ < rhs.p_;
    if (a_ != rhs.a_) return a_ < rhs.a_;
    if (b_ != rhs.b_) return b_ < rhs.b_;
    if (i_ != rhs.i_) return i_ < rhs.i_;
    return false;
  }

  void Print() const {
    cerr << p_ << ' ' << a_ << ' ' << b_ << ' ' << i_ << endl;
  }

 private:
  int p_;
  int a_;
  int b_;
  int i_;
};

class Bi {
 public:
  Bi() {}
  Bi(const int b, const int i) : b_(b), i_(i) {}
  Bi(const Pabi& pabi) : b_(pabi.get_b()), i_(pabi.get_i()) {}

  int get_b() const { return b_; }
  int get_i() const { return i_; }

  bool operator<(const Bi& rhs) const {
    if (b_ != rhs.b_) return b_ > rhs.b_;
    if (i_ != rhs.i_) return i_ < rhs.i_;
    return false;
  }

  void Print() const {
    cerr << b_ << ' ' << i_ << endl;
  }

 private:
  int b_;
  int i_;
};

int main() {
  int n;
  int k;
  scanf("%d%d", &n, &k);
  vector<Pabi> pabi(n);
  for (int i = 0; i < n; ++i) pabi[i].Read(i);

  sort(pabi.begin(), pabi.end());

  vector<priority_queue<int> > q;
  {
    int j = 0;
    for (int i = 0; i < pabi.size(); ++i) {
      const bool change = (i + 1) == pabi.size() || pabi[i].get_p() != pabi[i + 1].get_p();
      pabi[i].set_p(j);
      if (change) ++j;
    }
    q.resize(j);
  }

  // Rebuild input so that (p,a) is strictly increasing.
  {
    priority_queue<Bi> biq;
    int l = 0;
    for (int j = 0; j < q.size(); ++j) {
      int r = l;
      while (r < pabi.size() && pabi[r].get_p() == j) ++r;
      // Rebuild [l, r).
      int a_min = 0;
      int l_read = l;
      while (l < r) {
        while (l_read < r && pabi[l_read].get_a() <= a_min) biq.push(Bi(pabi[l_read++]));
        if (!biq.empty()) {
          const Bi bi = biq.top();
          biq.pop();
          pabi[l].set_a(a_min++);
          pabi[l].set_b(bi.get_b());
          pabi[l].set_i(bi.get_i());
          ++l;
        } else if (l_read < r) {
          a_min = pabi[l_read].get_a();
        }
      }
      l = r;
    }
  }

  // Check for empty intervals and exit early if any found.
  for (int i = 0; i < pabi.size(); ++i) {
    // cerr << i << ": ";
    // pabi[i].Print();
    if (pabi[i].get_a() > pabi[i].get_b()) {
      printf("NIE\n");
      return 0;
    }
  }

  // Main greedy loop.
  int hours = 0;
  vector<int> t(pabi.size());
  {
    set<int> s;
    vector<Bi> biv(pabi.size());
    for (int i = 0; i < pabi.size(); ++i) biv[i] = Bi(pabi[i].get_b(), i);
    sort(biv.begin(), biv.end());
    /*
    for (int i = 0; i < biv.size(); ++i) {
      cerr << i << ": ";
      biv[i].Print();
    }
    */
    int a_max = 0;
    int l = 0;
    while (l < biv.size() || !s.empty()) {
      if (s.empty()) a_max = 0;
      while (l < biv.size() && biv[l].get_b() >= a_max) {
        const int i = biv[l++].get_i();
        // cerr << "Queue " << i << endl;
        const int p = pabi[i].get_p();
        q[p].push(i);
        s.insert(p);
        a_max = max(a_max, pabi[i].get_a());
        // cerr << "a_max " << a_max << endl;
      }
      // cerr << "Schedule " << a_max << endl;
      // Schedule exercises at a_max.
      ++hours;
      vector<int> v(s.begin(), s.end());
      s.clear();
      // Store times.
      for (int i = 0; i < v.size(); ++i) t[pabi[q[v[i]].top()].get_i()] = a_max;
      // Remove from queues.
      a_max = 0;
      for (int i = 0; i < v.size(); ++i) {
        q[v[i]].pop();
        if (!q[v[i]].empty()) {
          s.insert(v[i]);
          a_max = max(a_max, pabi[q[v[i]].top()].get_a());
        }
      }
    }
  }

  // Output the answer.
  printf("%d\n", hours);
  for (int i = 0; i < t.size(); ++i) printf("%d\n", t[i]);

  return 0;
}