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

using namespace std;

class scaler{
    public:
    vector<int> V;
    void add( int a ) {
        V.push_back( a );
    }
    void process() {
        sort( V.begin(), V.end() );
        int wsk = 0, a=-1;
        for ( int i=0; i<V.size(); i++ ) {
            if ( a!=V[i] )
                a=V[wsk++]=V[i];
        }
        V.resize(wsk);
        V.reserve(wsk);
    }
    int scale( int a ) {
        return lower_bound( V.begin(), V.end(), a )-V.begin();
    }
    int descale( int a ) {
        return V[a];
    }
};

class przedz {
    public:
        int b;
        int e;
        int num;
        przedz(int _b,int _e,int _num) {e=_e;b=_b;num=_num;} 
};

inline bool operator<( const przedz &a, const przedz &b ) {
     if ( a.e != b.e ) return a.e<b.e;
     return a.b<b.b;
}

void failed() {
    printf("NIE\n");
    exit( 0 );
}

int A[1000006][3];
int W[1000006];
vector<przedz> TB[2000006];
vector<przedz> TE[2000006];
vector<przedz> V[ 1000006 ];
set<przedz> S[1000006];

scaler tm, dev;

int main() {
    int n,syf;
    //n=1000000; uint eee=84738747;
    scanf("%d%d",&n,&syf);
    for ( int i=0; i<n; i++ ) {
    //for ( int i=n-1; i>=0; i-- ) {
        int a,b,p;
        /*a=eee%10000000; b=a+eee%100000;
        p=eee%3;
        eee*=8732677;//*/
        scanf("%d%d%d",&a,&b,&p);
        dev.add(p);
        A[i][0] = a; A[i][1] = b;
        A[i][2] = p;
    }
    dev.process();
    for ( int i=0; i<n; i++ ) {
        int a,b,p;
        a=A[i][0]; b=A[i][1];
        p=A[i][2]=dev.scale(A[i][2]);
        V[p].push_back( przedz( a, b, i ) );
    }
    for ( int i=0; i<n; i++ ) if (V[i].size()) {
        vector<przedz> &v = V[i];
        sort( v.begin(), v.end() ); 
        int wsk = 1000000009;
        for ( int j=v.size()-1; j>=0; j-- ) {
            if ( v[j].e<wsk ) wsk=v[j].e;
               else {
                   v[j].e=wsk;
                   if ( v[j].b>v[j].e )
                       failed();
               }
            tm.add( v[j].b ); tm.add( v[j].e );
            wsk --;
        }
    }
    tm.process();

    for ( int i=0; i<n; i++ ) if ( V[i].size() ) {
        for ( auto &k : V[i] ) {
            TB[tm.scale(k.b)].push_back(k);
            TE[tm.scale(k.e)].push_back(k);
        }
    }

    list<int> active;
    int w=0;
    for ( int i=0; i<2*n; i++ ) {
        if (TB[i].size()) {
           sort( TB[i].begin(), TB[i].end() );
           for ( auto &j : TB[i] ) {
               int p = A[j.num][2];
               auto &l = S[ p ];
               if ( l.empty() ) active.push_back( p );
               l.insert( j );
           }
        }
        if (TE[i].size()) {
           sort( TE[i].begin(), TE[i].end() );
           for ( auto &j : TE[i] ) if (!W[j.num]) {
               int pos = tm.descale( i );
               w++;
               for ( auto it = active.begin(); it != active.end();  ) {
                   auto &l = S[*it];
                   int pp;
                   auto s_it = l.begin();
                   if ( s_it != l.end() ) {
                       pp = s_it->num;
                       s_it = l.erase( s_it );
                       if ( W[pp] )
                          while ( s_it != l.end() ) {
                              pp = s_it->num;
                              s_it = l.erase( s_it );
                              if ( !W[pp] ) {
                                  W[ pp ] = pos;
                                  break;
                              }
                          } else 
                            W[ pp ] = pos;
                       //printf("  %d %d\n",pp,pos);
                   }
                   if ( l.empty() ) it = active.erase( it );
                      else it++;
               }
           }
        }
    }
    printf("%d\n",w);
    for ( int i=0; i<n; i++ )
        printf("%d\n",W[i]);
    return 0;
}