#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// const int MAXN = 1000100 ; struct order { int a, b, k ; order(int aa=0, int bb=0, int kk=0) : a(aa), b(bb), k(kk) {} } ; // dwa typy obiektow: // - zakonczenie: wtedy indeksOrType = typ // - poczatek: wtedy indeksOrType = indeks struct event { int time, indexOrType ; bool isEnd ; event(int _t, int _i, int _is) : time(_t), indexOrType(_i), isEnd(_is) {} } ; bool operator<(const event &E1, const event &E2) { if(E1.time != E2.time) return E1.time < E2.time ; else return E1.isEnd < E2.isEnd ; } ostream &operator<<(ostream &stream, const event &e) { stream << "(" << e.time << "," << e.indexOrType << ", " << e.isEnd << ") " ; return stream ; } order o[MAXN] ; int ret[MAXN] ; int alreadyTaken[MAXN] ; typedef priority_queue<PII, vector<PII>, greater<PII> > QUEUE ; // (koniec, indeks) QUEUE Q[MAXN] ; vector<event> t ; void check(int n) { int i ; REP(i,n) { if(!(o[i].a <= ret[i] && ret[i] <= o[i].b)) { cout << MP(o[i].a,o[i].b) << " " << ret[i] << " type = " << o[i].k << endl ; assert(false) ; } } } struct mutableInt { mutable int val ; mutableInt(int _v=0) : val(_v) {} ; } ; bool operator<(const mutableInt &a, const mutableInt &b) { return a.val > b.val ; } VI indexOfTypeK[MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, k, i, j ; VI allK ; cin >> n >> k ; REP(i,n) { cin >> o[i].a >> o[i].b >> o[i].k ; allK.PB(o[i].k) ; } sort(ALL(allK)) ; allK.resize(unique(ALL(allK))-allK.begin()) ; REP(i,n) { o[i].k = lower_bound(ALL(allK), o[i].k)-allK.begin() +1 ; indexOfTypeK[o[i].k].PB(i) ; } k = allK.size() ; // jak przyciac te cholerne przedzialy ? typedef set<PII, greater<PII> > SET ; typedef map<mutableInt, SET*> MAPA ; MAPA M ; for(i=1 ; i<=k ; i++) { assert(!indexOfTypeK[i].empty()) ; /* cout << "type = " << i << endl ; FOREACH(it, indexOfTypeK[i]) cout << "(" << o[*it].a << "," << o[*it].b << "," << o[*it].k << ") " ; cout << endl ;*/ M.clear() ; FOREACH(it, indexOfTypeK[i]) { SET* &S = M[o[*it].b] ; if(S==NULL) S = new SET() ; S->insert(MP(o[*it].a, *it)) ; } for(MAPA::iterator it = M.begin() ; it!=M.end() ; /*it++*/) { //FOREACH(it, M) { int end = it->FI.val ; SET* list = it->SE ; assert(!list->empty()) ; int index = list->begin()->SE ; /* cout << "sciagam end=" << end << " index = " << index << endl ; cout << "lista = " ; FOREACH(q, (*list)) cout << *q << " " ; cout << endl ;*/ o[index].b = end ; t.PB(event(end, i, true)) ; list->erase(list->begin()) ; if(list->empty()) { delete list ; it++ ; } else { MAPA::iterator nextIt = it ; nextIt++ ; if(nextIt==M.end() || nextIt->FI.val != end-1) { //M[end-1] = list ; it->FI.val = end-1 ; // i zostajemy w miejscu } else { if(nextIt->SE->size() < it->SE->size()) swap(nextIt->SE, it->SE) ; assert(nextIt->SE->size() >= it->SE->size()) ; nextIt->SE->insert(ALL((*(it->SE)))) ; delete it->SE ; it = nextIt ; } } } /* FOREACH(it, indexOfTypeK[i]) cout << "(" << o[*it].a << "," << o[*it].b << "," << o[*it].k << ") " ; cout << endl ;*/ } REP(i, n) t.PB(event(o[i].a, i, false)) ; sort(ALL(t)) ; int optHours=0 ; set<int> activeQ ; FOREACH(it, t) { if(!it->isEnd) { // kladziemy int index = it->indexOrType ; int type = o[index].k ; Q[type].push(MP(o[index].b, index)) ; activeQ.insert(type) ; // cout << "type = " << type << " activeQ = " << activeQ << endl ; } else { // sciagamy int time = it->time ; int type = it->indexOrType ; if(alreadyTaken[type]>0 && (Q[type].empty() || Q[type].top().FI!=time) ) alreadyTaken[type]-- ; // juz zdjete else if(!Q[type].empty()) { optHours++ ; FOREACH(it, activeQ) { int index = Q[*it].top().SE ; ret[index] = time ; Q[*it].pop() ; if(*it != type) alreadyTaken[*it]++ ; } for(set<int>::iterator it=activeQ.begin() ; it!=activeQ.end() ; ) { if(Q[*it].empty()) activeQ.erase(it++) ; else it++ ; } // FOREACH(it, activeQ) // assert(!Q[*it].empty()) ; } else { // nie da sie cout << "NIE" << endl ; return 0 ; } } } cout << optHours << endl ; //check(n) ; REP(i,n) cout << ret[i] << endl ; }
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// const int MAXN = 1000100 ; struct order { int a, b, k ; order(int aa=0, int bb=0, int kk=0) : a(aa), b(bb), k(kk) {} } ; // dwa typy obiektow: // - zakonczenie: wtedy indeksOrType = typ // - poczatek: wtedy indeksOrType = indeks struct event { int time, indexOrType ; bool isEnd ; event(int _t, int _i, int _is) : time(_t), indexOrType(_i), isEnd(_is) {} } ; bool operator<(const event &E1, const event &E2) { if(E1.time != E2.time) return E1.time < E2.time ; else return E1.isEnd < E2.isEnd ; } ostream &operator<<(ostream &stream, const event &e) { stream << "(" << e.time << "," << e.indexOrType << ", " << e.isEnd << ") " ; return stream ; } order o[MAXN] ; int ret[MAXN] ; int alreadyTaken[MAXN] ; typedef priority_queue<PII, vector<PII>, greater<PII> > QUEUE ; // (koniec, indeks) QUEUE Q[MAXN] ; vector<event> t ; void check(int n) { int i ; REP(i,n) { if(!(o[i].a <= ret[i] && ret[i] <= o[i].b)) { cout << MP(o[i].a,o[i].b) << " " << ret[i] << " type = " << o[i].k << endl ; assert(false) ; } } } struct mutableInt { mutable int val ; mutableInt(int _v=0) : val(_v) {} ; } ; bool operator<(const mutableInt &a, const mutableInt &b) { return a.val > b.val ; } VI indexOfTypeK[MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, k, i, j ; VI allK ; cin >> n >> k ; REP(i,n) { cin >> o[i].a >> o[i].b >> o[i].k ; allK.PB(o[i].k) ; } sort(ALL(allK)) ; allK.resize(unique(ALL(allK))-allK.begin()) ; REP(i,n) { o[i].k = lower_bound(ALL(allK), o[i].k)-allK.begin() +1 ; indexOfTypeK[o[i].k].PB(i) ; } k = allK.size() ; // jak przyciac te cholerne przedzialy ? typedef set<PII, greater<PII> > SET ; typedef map<mutableInt, SET*> MAPA ; MAPA M ; for(i=1 ; i<=k ; i++) { assert(!indexOfTypeK[i].empty()) ; /* cout << "type = " << i << endl ; FOREACH(it, indexOfTypeK[i]) cout << "(" << o[*it].a << "," << o[*it].b << "," << o[*it].k << ") " ; cout << endl ;*/ M.clear() ; FOREACH(it, indexOfTypeK[i]) { SET* &S = M[o[*it].b] ; if(S==NULL) S = new SET() ; S->insert(MP(o[*it].a, *it)) ; } for(MAPA::iterator it = M.begin() ; it!=M.end() ; /*it++*/) { //FOREACH(it, M) { int end = it->FI.val ; SET* list = it->SE ; assert(!list->empty()) ; int index = list->begin()->SE ; /* cout << "sciagam end=" << end << " index = " << index << endl ; cout << "lista = " ; FOREACH(q, (*list)) cout << *q << " " ; cout << endl ;*/ o[index].b = end ; t.PB(event(end, i, true)) ; list->erase(list->begin()) ; if(list->empty()) { delete list ; it++ ; } else { MAPA::iterator nextIt = it ; nextIt++ ; if(nextIt==M.end() || nextIt->FI.val != end-1) { //M[end-1] = list ; it->FI.val = end-1 ; // i zostajemy w miejscu } else { if(nextIt->SE->size() < it->SE->size()) swap(nextIt->SE, it->SE) ; assert(nextIt->SE->size() >= it->SE->size()) ; nextIt->SE->insert(ALL((*(it->SE)))) ; delete it->SE ; it = nextIt ; } } } /* FOREACH(it, indexOfTypeK[i]) cout << "(" << o[*it].a << "," << o[*it].b << "," << o[*it].k << ") " ; cout << endl ;*/ } REP(i, n) t.PB(event(o[i].a, i, false)) ; sort(ALL(t)) ; int optHours=0 ; set<int> activeQ ; FOREACH(it, t) { if(!it->isEnd) { // kladziemy int index = it->indexOrType ; int type = o[index].k ; Q[type].push(MP(o[index].b, index)) ; activeQ.insert(type) ; // cout << "type = " << type << " activeQ = " << activeQ << endl ; } else { // sciagamy int time = it->time ; int type = it->indexOrType ; if(alreadyTaken[type]>0 && (Q[type].empty() || Q[type].top().FI!=time) ) alreadyTaken[type]-- ; // juz zdjete else if(!Q[type].empty()) { optHours++ ; FOREACH(it, activeQ) { int index = Q[*it].top().SE ; ret[index] = time ; Q[*it].pop() ; if(*it != type) alreadyTaken[*it]++ ; } for(set<int>::iterator it=activeQ.begin() ; it!=activeQ.end() ; ) { if(Q[*it].empty()) activeQ.erase(it++) ; else it++ ; } // FOREACH(it, activeQ) // assert(!Q[*it].empty()) ; } else { // nie da sie cout << "NIE" << endl ; return 0 ; } } } cout << optHours << endl ; //check(n) ; REP(i,n) cout << ret[i] << endl ; } |