#include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<iostream> #include<string> using namespace std; #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> const int M=1000005; struct eve2 { int st, k, bo, ind; }; struct eve1 { int st, en, ind; }; int n, m, res, prevv; int R[M], K[M], C[M], rdyk[M], done[M], arr[M]; bool rdy[M]; PII T[M]; vector<eve1> V[M]; vector<eve2> X; priority_queue<PII> Q, Q2[M]; vector<int> W; vector<PII> SC; bool cmp1(eve1 x, eve1 y) { if(x.en==y.en) return x.st>y.st; return x.en>y.en; } bool cmp2(eve2 x, eve2 y) { if(x.st==y.st) return x.bo<y.bo; return x.st<y.st; } bool put(int a, int ind) { while(!Q.empty() && prevv>a) { if(Q.top().F>prevv) return false; eve2 x; x.st=prevv; x.k=ind; x.bo=2; x.ind=Q.top().S; // printf("lol %d %d %d\n", x.st, x.k, x.ind); arr[x.ind]=x.st; X.PB(x); Q.pop(); prevv--; } return true; } bool solve1(int ind) { prevv=1000000001; while(!Q.empty()) Q.pop(); //printf("ind: %d\n", ind); for(int i=0; i<(int)V[ind].size(); i++) { //printf("%d %d %d\n", V[ind][i].st, V[ind][i].en, V[ind][i].ind); if(!put(V[ind][i].en, ind)) return false; prevv=min(prevv, V[ind][i].en); Q.push(MP(V[ind][i].st, V[ind][i].ind)); } if(!put(0, ind)) return false; return Q.empty(); } void solve2() { for(int i=0; i<(int)X.size(); i++) { if(X[i].bo==0) { //printf("add: %d %d %d\n", X[i].st, X[i].k, X[i].ind); rdy[X[i].ind]=1; rdyk[X[i].k]++; if(rdyk[X[i].k]==1) W.PB(X[i].k); Q2[X[i].k].push(MP(-arr[X[i].ind], X[i].ind)); } //if(X[i].bo==1) // printf("bo1 %d %d ind: %d %d\n", X[i].st, X[i].k, X[i].ind, R[X[i].ind]); //if(X[i].bo==1 && R[X[i].ind]==0) // putall(X[i].st); //if(X[i].bo==2) // printf("bo2 %d %d %d done: %d\n", X[i].st, X[i].k, X[i].ind, done[X[i].k]); if(X[i].bo==2) { if(R[X[i].ind]==0) { // putall(X[i].st, X[i].ind, X[i].k); res++; // printf("qwe=%d ind=%d k=%d\n", X[i].st, X[i].ind, X[i].k); vector<int> TM; while(!W.empty()) { int v=W[W.size()-1]; W.pop_back(); rdyk[v]--; if(rdyk[v]>0) TM.PB(v); if(v!=X[i].k) { while(R[Q2[v].top().S]!=0) Q2[v].pop(); int u=Q2[v].top().S; Q2[v].pop(); //printf("lol %d %d\n", u, v); R[u]=X[i].st; //done[v]++; } else { R[X[i].ind]=X[i].st; //printf("lol %d %d\n", X[i].ind, v); } } W=TM; } } } } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); for(int i=0; i<n; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); T[i]=MP(a, b); SC.PB(MP(c, i)); } int pre=-1, tmp=0; sort(SC.begin(), SC.end()); for(int i=0; i<n; i++) { if(SC[i].F!=pre) { tmp++; pre=SC[i].F; } C[SC[i].S]=tmp; } for(int i=0; i<n; i++) { int a=T[i].F, b=T[i].S, c=C[i]; eve1 x; eve2 y; K[i]=c; x.st=a; x.en=b; x.ind=i; V[c].PB(x); y.st=a; y.k=c; y.bo=0; y.ind=i; X.PB(y); } //printf("%d\n", tmp); for(int i=1; i<=tmp; i++) { sort(V[i].begin(), V[i].end(), cmp1); if(!solve1(i)) { printf("NIE\n"); return 0; } } sort(X.begin(), X.end(), cmp2); solve2(); printf("%d\n", res); for(int i=0; i<n; i++) printf("%d\n", R[i]); return 0; }
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 | #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<iostream> #include<string> using namespace std; #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define PII pair<int, int> #define PLL pair<LL, LL> const int M=1000005; struct eve2 { int st, k, bo, ind; }; struct eve1 { int st, en, ind; }; int n, m, res, prevv; int R[M], K[M], C[M], rdyk[M], done[M], arr[M]; bool rdy[M]; PII T[M]; vector<eve1> V[M]; vector<eve2> X; priority_queue<PII> Q, Q2[M]; vector<int> W; vector<PII> SC; bool cmp1(eve1 x, eve1 y) { if(x.en==y.en) return x.st>y.st; return x.en>y.en; } bool cmp2(eve2 x, eve2 y) { if(x.st==y.st) return x.bo<y.bo; return x.st<y.st; } bool put(int a, int ind) { while(!Q.empty() && prevv>a) { if(Q.top().F>prevv) return false; eve2 x; x.st=prevv; x.k=ind; x.bo=2; x.ind=Q.top().S; // printf("lol %d %d %d\n", x.st, x.k, x.ind); arr[x.ind]=x.st; X.PB(x); Q.pop(); prevv--; } return true; } bool solve1(int ind) { prevv=1000000001; while(!Q.empty()) Q.pop(); //printf("ind: %d\n", ind); for(int i=0; i<(int)V[ind].size(); i++) { //printf("%d %d %d\n", V[ind][i].st, V[ind][i].en, V[ind][i].ind); if(!put(V[ind][i].en, ind)) return false; prevv=min(prevv, V[ind][i].en); Q.push(MP(V[ind][i].st, V[ind][i].ind)); } if(!put(0, ind)) return false; return Q.empty(); } void solve2() { for(int i=0; i<(int)X.size(); i++) { if(X[i].bo==0) { //printf("add: %d %d %d\n", X[i].st, X[i].k, X[i].ind); rdy[X[i].ind]=1; rdyk[X[i].k]++; if(rdyk[X[i].k]==1) W.PB(X[i].k); Q2[X[i].k].push(MP(-arr[X[i].ind], X[i].ind)); } //if(X[i].bo==1) // printf("bo1 %d %d ind: %d %d\n", X[i].st, X[i].k, X[i].ind, R[X[i].ind]); //if(X[i].bo==1 && R[X[i].ind]==0) // putall(X[i].st); //if(X[i].bo==2) // printf("bo2 %d %d %d done: %d\n", X[i].st, X[i].k, X[i].ind, done[X[i].k]); if(X[i].bo==2) { if(R[X[i].ind]==0) { // putall(X[i].st, X[i].ind, X[i].k); res++; // printf("qwe=%d ind=%d k=%d\n", X[i].st, X[i].ind, X[i].k); vector<int> TM; while(!W.empty()) { int v=W[W.size()-1]; W.pop_back(); rdyk[v]--; if(rdyk[v]>0) TM.PB(v); if(v!=X[i].k) { while(R[Q2[v].top().S]!=0) Q2[v].pop(); int u=Q2[v].top().S; Q2[v].pop(); //printf("lol %d %d\n", u, v); R[u]=X[i].st; //done[v]++; } else { R[X[i].ind]=X[i].st; //printf("lol %d %d\n", X[i].ind, v); } } W=TM; } } } } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); for(int i=0; i<n; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); T[i]=MP(a, b); SC.PB(MP(c, i)); } int pre=-1, tmp=0; sort(SC.begin(), SC.end()); for(int i=0; i<n; i++) { if(SC[i].F!=pre) { tmp++; pre=SC[i].F; } C[SC[i].S]=tmp; } for(int i=0; i<n; i++) { int a=T[i].F, b=T[i].S, c=C[i]; eve1 x; eve2 y; K[i]=c; x.st=a; x.en=b; x.ind=i; V[c].PB(x); y.st=a; y.k=c; y.bo=0; y.ind=i; X.PB(y); } //printf("%d\n", tmp); for(int i=1; i<=tmp; i++) { sort(V[i].begin(), V[i].end(), cmp1); if(!solve1(i)) { printf("NIE\n"); return 0; } } sort(X.begin(), X.end(), cmp2); solve2(); printf("%d\n", res); for(int i=0; i<n; i++) printf("%d\n", R[i]); return 0; } |