#include <bits/stdc++.h> using namespace std; int n; int k; pair <int,int> tab[1000007]; int num[1000007]; vector <int> wek[1000007]; int stan[1000007]; int ilew[1000007]; vector <int> dosk; vector <int> ost; int kied[1000007]; int wyn; int naj; int v; priority_queue < pair <int,int> > kol; pair <int,int> dos[2000007]; set < pair <int, pair <int,int> > > secik; vector <int> ter; set < pair <int, pair <int,int> > >::iterator it; bool mniej(int a, int b) { return tab[a].second<tab[b].second; } bool mniej2(pair <int,int> a, pair <int,int> b) { int repa; if (a.first==1) repa=tab[a.second].first; else repa=tab[a.second].second; int repb; if (b.first==1) repb=tab[b.second].first; else repb=tab[b.second].second; if (repa==repb) return a<b; return repa<repb; } int main() { scanf("%d%d", &n, &k); for (int i=1; i<=n; i++) { scanf("%d%d%d", &tab[i].first, &tab[i].second, &num[i]); dosk.push_back(num[i]); } sort(dosk.begin(), dosk.end()); for (int i=0; i<n; i++) { if (!i || dosk[i]!=dosk[i-1]) { ost.push_back(dosk[i]); } } for (int i=1; i<=n; i++) { num[i]=lower_bound(ost.begin(), ost.end(), num[i])-ost.begin()+1; wek[num[i]].push_back(i); } for (int i=1; i<=n; i++) { if (wek[i].empty()) continue; sort(wek[i].begin(), wek[i].end(), mniej); naj=1000000007; for (int j=wek[i].size(); j; j--) { while(kol.empty() || (!wek[i].empty() && tab[wek[i].back()].second>=naj)) { kol.push(make_pair(tab[wek[i].back()].first, wek[i].back())); wek[i].pop_back(); naj=min(naj, tab[kol.top().second].second); } tab[kol.top().second].second=naj; kol.pop(); naj--; } } for (int i=1; i<=n; i++) { if (tab[i].second<tab[i].first) { printf("NIE\n"); return 0; } } for (int i=1; i<=n; i++) { dos[2*i-1]=make_pair(1, i); dos[2*i]=make_pair(2, i); } sort(dos+1, dos+1+2*n, mniej2); for (int i=1; i<=2*n; i++) { if (dos[i].first==1) { v=dos[i].second; if (!ilew[num[v]]) { ter.push_back(num[v]); } ilew[num[v]]++; stan[v]=1; secik.insert( make_pair( num[v] , make_pair(tab[v].second, v) ) ); continue; } if (stan[dos[i].second]==2) continue; naj=tab[dos[i].second].second; wyn++; for (int j=0; j<ter.size(); j++) { v=ter[j]; ilew[v]--; if (!ilew[v]) { swap(ter[j], ter.back()); ter.pop_back(); j--; } it=secik.lower_bound( make_pair( v, make_pair(0,0) ) ); v=(*it).second.second; stan[v]=2; kied[v]=naj; secik.erase(it); } } printf("%d\n", wyn); for (int i=1; i<=n; i++) printf("%d\n", kied[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 | #include <bits/stdc++.h> using namespace std; int n; int k; pair <int,int> tab[1000007]; int num[1000007]; vector <int> wek[1000007]; int stan[1000007]; int ilew[1000007]; vector <int> dosk; vector <int> ost; int kied[1000007]; int wyn; int naj; int v; priority_queue < pair <int,int> > kol; pair <int,int> dos[2000007]; set < pair <int, pair <int,int> > > secik; vector <int> ter; set < pair <int, pair <int,int> > >::iterator it; bool mniej(int a, int b) { return tab[a].second<tab[b].second; } bool mniej2(pair <int,int> a, pair <int,int> b) { int repa; if (a.first==1) repa=tab[a.second].first; else repa=tab[a.second].second; int repb; if (b.first==1) repb=tab[b.second].first; else repb=tab[b.second].second; if (repa==repb) return a<b; return repa<repb; } int main() { scanf("%d%d", &n, &k); for (int i=1; i<=n; i++) { scanf("%d%d%d", &tab[i].first, &tab[i].second, &num[i]); dosk.push_back(num[i]); } sort(dosk.begin(), dosk.end()); for (int i=0; i<n; i++) { if (!i || dosk[i]!=dosk[i-1]) { ost.push_back(dosk[i]); } } for (int i=1; i<=n; i++) { num[i]=lower_bound(ost.begin(), ost.end(), num[i])-ost.begin()+1; wek[num[i]].push_back(i); } for (int i=1; i<=n; i++) { if (wek[i].empty()) continue; sort(wek[i].begin(), wek[i].end(), mniej); naj=1000000007; for (int j=wek[i].size(); j; j--) { while(kol.empty() || (!wek[i].empty() && tab[wek[i].back()].second>=naj)) { kol.push(make_pair(tab[wek[i].back()].first, wek[i].back())); wek[i].pop_back(); naj=min(naj, tab[kol.top().second].second); } tab[kol.top().second].second=naj; kol.pop(); naj--; } } for (int i=1; i<=n; i++) { if (tab[i].second<tab[i].first) { printf("NIE\n"); return 0; } } for (int i=1; i<=n; i++) { dos[2*i-1]=make_pair(1, i); dos[2*i]=make_pair(2, i); } sort(dos+1, dos+1+2*n, mniej2); for (int i=1; i<=2*n; i++) { if (dos[i].first==1) { v=dos[i].second; if (!ilew[num[v]]) { ter.push_back(num[v]); } ilew[num[v]]++; stan[v]=1; secik.insert( make_pair( num[v] , make_pair(tab[v].second, v) ) ); continue; } if (stan[dos[i].second]==2) continue; naj=tab[dos[i].second].second; wyn++; for (int j=0; j<ter.size(); j++) { v=ter[j]; ilew[v]--; if (!ilew[v]) { swap(ter[j], ter.back()); ter.pop_back(); j--; } it=secik.lower_bound( make_pair( v, make_pair(0,0) ) ); v=(*it).second.second; stan[v]=2; kied[v]=naj; secik.erase(it); } } printf("%d\n", wyn); for (int i=1; i<=n; i++) printf("%d\n", kied[i]); return 0; } |