#include <bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define mp make_pair #define ALL(G) (G).begin(),(G).end() typedef long long ll; typedef double D; typedef pair<int,int> pii; typedef vector<int> vi; const int inft = 1000000009; const int MAXN = 1000006; pii P[MAXN]; int K[MAXN],ind[MAXN]; bool usedK[MAXN]; vector<int> V[MAXN]; bool cmp(const int &a,const int &b){ if(P[a].y==P[b].y)return P[a].x<P[b].x; return P[a].y<P[b].y; } class cmp2{ public: bool operator() (const int &a, const int &b){ return !cmp(a,b); } }; priority_queue<int,vi, cmp2> QK[MAXN]; void solve() { int n,k; scanf("%d%d",&n,&k); vi X; fru(i,n){ scanf("%d%d%d",&P[i].x,&P[i].y,&K[i]); X.pb(K[i]); } sort(ALL(X)); X.resize(unique(ALL(X))-X.begin()); fru(i,n)K[i]=lower_bound(ALL(X),K[i])-X.begin(); fru(i,n)V[K[i]].pb(i); int Xs=X.size(); fru(i,Xs){ // printf("%d\n",X[i]); sort(ALL(V[i]),cmp); // tr(it,V[i])printf("%d %d\n",P[*it].x,P[*it].y); int prev=0; set<pii> S ; for(int j=V[i].size()-1;j>=0;j--){ int indx=V[i][j]; if(P[indx].y>=prev){ prev=P[indx].y; S.insert(pii(P[indx].x,indx)); // printf("wrzucam %d\n",indx); } else{ //jesli set niepusty, wyciagnij if(!S.empty()){ pii v=*S.rbegin(); S.erase(v); // printf("biore %d z %d\n",v.y, prev); P[v.y].y=prev; prev--; } else{ prev=P[indx].y; } j++; } } while(!S.empty()){ pii v=*S.rbegin(); S.erase(v); // printf("biore %d z %d\n",v.y, prev); P[v.y].y=prev; prev--; } sort(ALL(V[i]),cmp); } // puts("$$$$"); // fru(i,n)printf("%d %d\n",P[i].x,P[i].y); fru(i,n)if(P[i].x>P[i].y){puts("NIE");return;} vi ANS(n); tr(it,ANS)*it=-1; vector<pii> Q; fru(i,n){ Q.pb(pii(P[i].x,-(i+1))); Q.pb(pii(P[i].y,+(i+1))); } sort(ALL(Q)); vi C; int czas=0; tr(it2,Q){ pii u=*it2; // printf("event %d, %d\n",u.x,u.y); if(u.y>0){ //zdejmuje, u.y jest na gorze swojej kolejki prio, albo juz nie u.y--; int v=K[u.y]; if(QK[v].empty() || QK[v].top()!=u.y)continue; // printf("pop dla %d\n",u.y); czas++; vi CC; tr(it,C){ // printf("mam %d\n",*it); int w=QK[*it].top();QK[*it].pop(); // printf("w= %d\n",w); ANS[w]=u.x; if(!QK[*it].empty()){ // printf("dodaje %d\n",*it); CC.pb(*it); } } C.clear(); swap(C,CC); } else{ //dokladam u.y=-u.y;u.y--; // printf("put dla %d\n",u.y); if(QK[K[u.y]].empty()){ // printf("dorzucam do C\n"); C.pb(K[u.y]); } QK[K[u.y]].push(u.y); } } printf("%d\n",czas); fru(i,n)printf("%d\n",ANS[i]); } int main() { // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); int t=1; // scanf("%d",&t); fru(i,t) solve(); 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 | #include <bits/stdc++.h> using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define mp make_pair #define ALL(G) (G).begin(),(G).end() typedef long long ll; typedef double D; typedef pair<int,int> pii; typedef vector<int> vi; const int inft = 1000000009; const int MAXN = 1000006; pii P[MAXN]; int K[MAXN],ind[MAXN]; bool usedK[MAXN]; vector<int> V[MAXN]; bool cmp(const int &a,const int &b){ if(P[a].y==P[b].y)return P[a].x<P[b].x; return P[a].y<P[b].y; } class cmp2{ public: bool operator() (const int &a, const int &b){ return !cmp(a,b); } }; priority_queue<int,vi, cmp2> QK[MAXN]; void solve() { int n,k; scanf("%d%d",&n,&k); vi X; fru(i,n){ scanf("%d%d%d",&P[i].x,&P[i].y,&K[i]); X.pb(K[i]); } sort(ALL(X)); X.resize(unique(ALL(X))-X.begin()); fru(i,n)K[i]=lower_bound(ALL(X),K[i])-X.begin(); fru(i,n)V[K[i]].pb(i); int Xs=X.size(); fru(i,Xs){ // printf("%d\n",X[i]); sort(ALL(V[i]),cmp); // tr(it,V[i])printf("%d %d\n",P[*it].x,P[*it].y); int prev=0; set<pii> S ; for(int j=V[i].size()-1;j>=0;j--){ int indx=V[i][j]; if(P[indx].y>=prev){ prev=P[indx].y; S.insert(pii(P[indx].x,indx)); // printf("wrzucam %d\n",indx); } else{ //jesli set niepusty, wyciagnij if(!S.empty()){ pii v=*S.rbegin(); S.erase(v); // printf("biore %d z %d\n",v.y, prev); P[v.y].y=prev; prev--; } else{ prev=P[indx].y; } j++; } } while(!S.empty()){ pii v=*S.rbegin(); S.erase(v); // printf("biore %d z %d\n",v.y, prev); P[v.y].y=prev; prev--; } sort(ALL(V[i]),cmp); } // puts("$$$$"); // fru(i,n)printf("%d %d\n",P[i].x,P[i].y); fru(i,n)if(P[i].x>P[i].y){puts("NIE");return;} vi ANS(n); tr(it,ANS)*it=-1; vector<pii> Q; fru(i,n){ Q.pb(pii(P[i].x,-(i+1))); Q.pb(pii(P[i].y,+(i+1))); } sort(ALL(Q)); vi C; int czas=0; tr(it2,Q){ pii u=*it2; // printf("event %d, %d\n",u.x,u.y); if(u.y>0){ //zdejmuje, u.y jest na gorze swojej kolejki prio, albo juz nie u.y--; int v=K[u.y]; if(QK[v].empty() || QK[v].top()!=u.y)continue; // printf("pop dla %d\n",u.y); czas++; vi CC; tr(it,C){ // printf("mam %d\n",*it); int w=QK[*it].top();QK[*it].pop(); // printf("w= %d\n",w); ANS[w]=u.x; if(!QK[*it].empty()){ // printf("dodaje %d\n",*it); CC.pb(*it); } } C.clear(); swap(C,CC); } else{ //dokladam u.y=-u.y;u.y--; // printf("put dla %d\n",u.y); if(QK[K[u.y]].empty()){ // printf("dorzucam do C\n"); C.pb(K[u.y]); } QK[K[u.y]].push(u.y); } } printf("%d\n",czas); fru(i,n)printf("%d\n",ANS[i]); } int main() { // freopen("input.in", "r", stdin); // freopen("output.out", "w", stdout); int t=1; // scanf("%d",&t); fru(i,t) solve(); return 0; } |