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
//Przemysław Jakub Kozłowski
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <cassert>
#define FI first
#define SE second
#define MP make_pair
using namespace std;
const int N = 1000006, INF = 1000000009;

int n,k;
pair<pair<int,int>, int> tab[N];

bool cmpa(int a, int b)
{
    if(tab[a].SE != tab[b].SE) return tab[a].SE < tab[b].SE;
    if(tab[a].FI.SE != tab[b].FI.SE) return tab[a].FI.SE > tab[b].FI.SE;
    return tab[a].FI.FI > tab[b].FI.FI;
}
struct cmpb
{
    bool operator()(int a, int b) const
    {
        return tab[a].FI.SE > tab[b].FI.SE;
    }
};
struct cmpc
{
    bool operator()(int a, int b) const
    {
        return tab[a].FI.FI < tab[b].FI.FI;
    }
};

int wyng;
int wynd[N];

bool obl()
{
    for(int i = 1;i <= n;i++)
        if(tab[i].FI.FI > tab[i].FI.SE)
            return false;

    vector<pair<int, pair<char, int> > > events;
    for(int i = 1;i <= n;i++)
    {
        events.push_back(MP(tab[i].FI.FI, MP(1, i)));
        events.push_back(MP(tab[i].FI.SE, MP(2, i)));
    }
    sort(events.begin(), events.end());

    vector<int> otwarte;
    vector<priority_queue<int, vector<int>, cmpb> > KP(n+1);
    for(int i = 0;i < events.size();i++)
    {
        if(events[i].SE.FI == 1)
        {
            int kpnr = tab[events[i].SE.SE].SE;
            if(KP[kpnr].empty()) otwarte.push_back(kpnr);
            KP[kpnr].push(events[i].SE.SE);
        }
        else
        {
            int kpnr = tab[events[i].SE.SE].SE;
            if(!(KP[kpnr].empty() || KP[kpnr].top() != events[i].SE.SE))
            {
                wyng++;
                vector<int> notwarte;
                for(int j = 0;j < otwarte.size();j++)
                {
                    int kpnr = otwarte[j];
                    int zap = KP[kpnr].top();
                    KP[kpnr].pop();
                    wynd[zap] = events[i].FI;
                    if(!KP[kpnr].empty()) notwarte.push_back(kpnr);
                }
                otwarte.swap(notwarte);
            }
        }
    }

    if(otwarte.empty()) return true;
    else return false;
}
int main()
{
    scanf("%d%d", &n, &k);
    map<int,int> remap;
    for(int i = 1;i <= n;i++)
    {
        scanf("%d%d%d", &tab[i].FI.FI, &tab[i].FI.SE, &tab[i].SE);
        tab[i].SE = remap.insert(MP(tab[i].SE, remap.size()+1)).FI->SE;
    }

    vector<int> kol(n+1);
    for(int i = 1;i <= n;i++) kol[i] = i;
    sort(kol.begin()+1, kol.begin()+n+1, cmpa);

    int lastw = 0, maxpoz = INF;
    priority_queue<int, vector<int>, cmpc> PQ;
    for(int i = 1;i <= n;i++)
    {
        if(lastw != tab[kol[i]].SE)
        {
            while(!PQ.empty())
            {
                int pq = PQ.top();
                PQ.pop();
                tab[pq].FI.SE = maxpoz;
                maxpoz--;
            }
            lastw = tab[kol[i]].SE;
            maxpoz = tab[kol[i]].FI.SE;
        }
        while(!PQ.empty() && maxpoz > tab[kol[i]].FI.SE)
        {
            int pq = PQ.top();
            PQ.pop();
            tab[pq].FI.SE = maxpoz;
            maxpoz--;
        }
        PQ.push(kol[i]);
        maxpoz = tab[kol[i]].FI.SE;
    }
    while(!PQ.empty())
    {
        int pq = PQ.top();
        PQ.pop();
        tab[pq].FI.SE = maxpoz;
        maxpoz--;
    }

    bool tak = obl();
    if(tak)
    {
        printf("%d\n", wyng);
        for(int i = 1;i <= n;i++)
            printf("%d\n", wynd[i]);
    }
    else printf("NIE\n");

    return 0;
}