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
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

namespace {
  struct Booking {
    int from;
    int to;
    int where;
  };

  template <typename T, typename C=less<T>>
  class Queue {
    private:
      vector<priority_queue<T, vector<T>, C>> queue;
      vector<int> nonempty;

    public:
      Queue(int k): queue(k), nonempty{}
      {
      }

      bool empty() const
      {
        return nonempty.empty();
      }

      void add(int k, T&& v)
      {
        if (queue[k].empty()) nonempty.push_back(k);
        queue[k].push(move(v));
      }

      template <typename F>
      void for_each(F&& f)
      {
        vector<int> new_nonempty;
        for (auto const& k: nonempty) {
          f(queue[k].top());
          queue[k].pop();
          if (!queue[k].empty()) new_nonempty.push_back(k);
        }
        nonempty = move(new_nonempty);
      }
  };

  int compress(vector<Booking>& bookings)
  {
    vector<int> p(bookings.size());
    iota(p.begin(), p.end(), 0);
    sort(p.begin(), p.end(), [&](int a, int b) { return bookings[a].where < bookings[b].where; });
    int k = 0;
    auto it = p.begin();
    while (it != p.end()) {
      auto x = bookings[*it].where;
      auto it2 = it;
      while (it2 != p.end() && bookings[*it2].where == x) {
        bookings[*it2].where = k;
        ++it2;
      }
      ++k;
      it = it2;
    }
    return k;
  }

  bool make_ends_differ(vector<Booking>& bookings, int k)
  {
    vector<int> p(bookings.size());
    iota(p.begin(), p.end(), 0);
    sort(p.begin(), p.end(), [&](int a, int b) { return bookings[a].to > bookings[b].to; });
    Queue<pair<int, int>> q(k);
    int t = -1;
    auto it = p.begin();
    while (it != p.end() || !q.empty()) {
      if (q.empty()) t = bookings[*it].to;
      while (it != p.end() && bookings[*it].to == t) {
        q.add(bookings[*it].where, {bookings[*it].from, *it});
        ++it;
      }
      bool ok = true;
      q.for_each([&](pair<int, int> x) {
        bookings[x.second].to = t;
        if (bookings[x.second].from > t) ok = false;
      });
      if (!ok) return false;
      --t;
    }
    return true;
  }

  pair<int, vector<int>> solve(vector<Booking> const& bookings, int k)
  {
    int n = bookings.size();
    vector<pair<int, int>> events;
    events.reserve(2 * n);
    for (int i = 0; i < n; ++i) {
      events.emplace_back(bookings[i].from, i);
      events.emplace_back(bookings[i].to, n+i);
    }
    sort(events.begin(), events.end());

    int used = 0;
    vector<int> when(n);

    Queue<pair<int, int>, greater<pair<int, int>>> q(k);

    for (auto const& e: events) {
      auto t = e.first;
      if (e.second < n) {
        auto i = e.second;
        q.add(bookings[i].where, {bookings[i].to, i});
      } else {
        auto i = e.second - n;
        if (when[i] > 0) continue;
        ++used;
        q.for_each([&](pair<int, int> x) {
          when[x.second] = t;
        });
      }
    }

    return {used, when};
  }
}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;

  vector<Booking> bookings(n);
  for (auto& b: bookings) {
    cin >> b.from >> b.to >> b.where;
  }

  k = compress(bookings);

  if (!make_ends_differ(bookings, k)) {
    cout << "NIE\n";
    return 0;
  }

  auto res = solve(bookings, k);
  cout << res.first << '\n';
  for (auto const& x: res.second) {
    cout << x << '\n';
  }

  return 0;
}