#include<cstdio> #include<algorithm> #include<ctime> #include<cstring> #include<iostream> #include<cstdlib> #include<cmath> #include<vector> #include<map> #include<set> #include<queue> #include<bitset> using namespace std; typedef long long ll; typedef double db; const db pi=acos(-1); void gn(int &x){ int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c=='-')sg=-1,x=0;else x=c-'0'; while((c=getchar())>='0'&&c<='9')x=x*10+c-'0'; x*=sg; } void gn(ll &x){ int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c=='-')sg=-1,x=0;else x=c-'0'; while((c=getchar())>='0'&&c<='9')x=x*10+c-'0'; x*=sg; } int gcd(int a,int b){return b?gcd(b,a%b):a;} struct node{ int l,r,p,id; }a[1000005],tmp[1000005]; int nu[1111111]; int n,k; int cmpp(const node&a,const node&b){ return a.p<b.p; } int cmpr(const node&a,const node&b){ return a.r<b.r; } int operator<(const node&a,const node&b){return a.l<b.l;} priority_queue<node>qu; int ma[2222222];int tot=0; struct edge{ int v,next; }e[2222222];int build[2222222],intv[2222222];int etot=0; void ae(int *g,int u,int v){ e[etot].v=v;e[etot].next=g[u];g[u]=etot++; } int fir[2222222]; int s[8888888],tag[8888888]; int l1,r1; int an,I,v; inline void pd(int x){ s[x<<1]+=tag[x]; s[x<<1|1]+=tag[x]; tag[x<<1]+=tag[x]; tag[x<<1|1]+=tag[x]; tag[x]=0; } void upd(int l,int r,int x){ if(l==r)s[x]=v; else{ if(tag[x])pd(x); int mid=l+r>>1; if(I<=mid)upd(l,mid,x<<1); else upd(mid+1,r,x<<1|1); s[x]=max(s[x<<1],s[x<<1|1]); } } void cov(int l,int r,int x){ if(l1<=l && r<=r1){ s[x]++;tag[x]++; an=max(an,s[x]); }else{ if(tag[x])pd(x); int mid=l+r>>1; if(l1<=mid)cov(l,mid,x<<1); if(r1>mid)cov(mid+1,r,x<<1|1); s[x]=max(s[x<<1],s[x<<1|1]); } } int pcnt[1111111]={0},plas[1111111]={0}; int f[2222222]; int bit[2222222]; inline void bitupd(int x,int v){for (;x<=tot;x+=x&-x)bit[x]=max(bit[x],v);} inline int bitque(int x){int ans=0;for(;x;x-=x&-x)ans=max(ans,bit[x]);return ans;} set<int> se; int ans[1111111]; int main() { //int cl=clock(); //freopen("in","r",stdin); //freopen("jc.out","w",stdout); memset(build,-1,sizeof(build)); memset(intv,-1,sizeof(intv)); gn(n);gn(k); for (int i=1;i<=n;i++){ gn(a[i].l); gn(a[i].r); gn(a[i].p); a[i].id=i; } /*freopen("jc.out","r",stdin); gn(k); for (int i=1;i<=n;i++){ int x;gn(x); if(x<a[i].l ||x>a[i].r){ printf("no\n"); }else printf("yes"); } return 0;*/ sort(a+1,a+1+n,cmpp); k=0;int val=-1; for (int i=1;i<=n;i++){ if(val!=a[i].p){ val=a[i].p; a[i].p=++k; }else{ a[i].p=k; } } for (int i=1;i<=n;i++)if(i==1 || a[i].p!=a[i-1].p){ int j; for (j=i;j<=n && a[j].p==a[i].p;j++); sort(a+i,a+j,cmpr); int curv=a[j-1].r,cur=j-1; int tmptot=0; while(1){ while(cur>=i && a[cur].r>=curv){ qu.push(a[cur]); cur--; } if(qu.empty()){ if(cur==i-1)break; else { curv=a[cur].r; continue; } } node s=qu.top();qu.pop(); s.r=curv; curv--; tmp[++tmptot]=s; } for (int t=1;t<=tmptot;t++)a[j-t]=tmp[t]; } for (int i=1;i<=n;i++){ swap(a[i].l,a[i].r); a[i].l*=-1;a[i].r*=-1; } for (int i=1;i<=n;i++)if(i==1 || a[i].p!=a[i-1].p){ int j; for (j=i;j<=n && a[j].p==a[i].p;j++); sort(a+i,a+j,cmpr); int curv=a[j-1].r,cur=j-1; int tmptot=0; while(1){ while(cur>=i && a[cur].r>=curv){ qu.push(a[cur]); cur--; } if(qu.empty()){ if(cur==i-1)break; else { curv=a[cur].r; continue; } } node s=qu.top();qu.pop(); s.r=curv; curv--; tmp[++tmptot]=s; } for (int t=1;t<=tmptot;t++)a[j-t]=tmp[t]; } for (int i=1;i<=n;i++){ swap(a[i].l,a[i].r); a[i].l*=-1;a[i].r*=-1; } for (int i=1;i<=n;i++) fir[a[i].p]++; for (int i=2;i<=k;i++)fir[i]+=fir[i-1]; for (int i=1;i<=n;i++){ if(a[i].l>a[i].r){ printf("NIE\n"); return 0; } a[i].l--; ma[++tot]=a[i].l; ma[++tot]=a[i].r; } sort(ma+1,ma+1+tot); tot=unique(ma+1,ma+1+tot)-ma-1; for (int i=1;i<=n;i++){ a[i].l=lower_bound(ma+1,ma+1+tot,a[i].l)-ma; a[i].r=lower_bound(ma+1,ma+1+tot,a[i].r)-ma; ae(intv,a[i].r,i); ae(build,a[i].l,i); } //printf("tm1:%d\n",cl-clock());cl=clock(); f[0]=0; for (int i=1;i<=tot;i++){ f[i]=f[i-1]; for (int j=intv[i];~j;j=e[j].next){ int x=e[j].v; int t=nu[x];int p=a[x].p; l1=fir[p-1]+1,r1=fir[p-1]+t;an=0; cov(1,n,1); f[i]=max(f[i],an); } bitupd(tot-i+1,f[i]); for (int j=build[i];~j;j=e[j].next){ int x=e[j].v; int p=a[x].p; int t=++pcnt[p];nu[x]=t; an=bitque(tot-plas[p]); plas[p]=i; v=an; I=fir[p-1]+t; upd(1,n,1); } } printf("%d\n",f[tot]); //printf("tm2:%d\n",cl-clock());cl=clock(); // for (int i=2;i<=tot;i++)if(f[i]!=f[i-1])se.insert(i); for (int i=1;i<=n;i++)if(i==1 || a[i].p!=a[i-1].p){ int j; for (j=i;j<=n && a[j].p==a[i].p;j++); sort(a+i,a+j,cmpr); for (int k=i;k<j;k++){ int t=*se.lower_bound(a[k].l+1); ans[a[k].id]=t; se.erase(t); } for (int k=i;k<j;k++){ se.insert(ans[a[k].id]); } } //printf("tm2:%d\n",cl-clock());cl=clock(); for (int i=1;i<=n;i++)printf("%d\n",ma[ans[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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | #include<cstdio> #include<algorithm> #include<ctime> #include<cstring> #include<iostream> #include<cstdlib> #include<cmath> #include<vector> #include<map> #include<set> #include<queue> #include<bitset> using namespace std; typedef long long ll; typedef double db; const db pi=acos(-1); void gn(int &x){ int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c=='-')sg=-1,x=0;else x=c-'0'; while((c=getchar())>='0'&&c<='9')x=x*10+c-'0'; x*=sg; } void gn(ll &x){ int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c=='-')sg=-1,x=0;else x=c-'0'; while((c=getchar())>='0'&&c<='9')x=x*10+c-'0'; x*=sg; } int gcd(int a,int b){return b?gcd(b,a%b):a;} struct node{ int l,r,p,id; }a[1000005],tmp[1000005]; int nu[1111111]; int n,k; int cmpp(const node&a,const node&b){ return a.p<b.p; } int cmpr(const node&a,const node&b){ return a.r<b.r; } int operator<(const node&a,const node&b){return a.l<b.l;} priority_queue<node>qu; int ma[2222222];int tot=0; struct edge{ int v,next; }e[2222222];int build[2222222],intv[2222222];int etot=0; void ae(int *g,int u,int v){ e[etot].v=v;e[etot].next=g[u];g[u]=etot++; } int fir[2222222]; int s[8888888],tag[8888888]; int l1,r1; int an,I,v; inline void pd(int x){ s[x<<1]+=tag[x]; s[x<<1|1]+=tag[x]; tag[x<<1]+=tag[x]; tag[x<<1|1]+=tag[x]; tag[x]=0; } void upd(int l,int r,int x){ if(l==r)s[x]=v; else{ if(tag[x])pd(x); int mid=l+r>>1; if(I<=mid)upd(l,mid,x<<1); else upd(mid+1,r,x<<1|1); s[x]=max(s[x<<1],s[x<<1|1]); } } void cov(int l,int r,int x){ if(l1<=l && r<=r1){ s[x]++;tag[x]++; an=max(an,s[x]); }else{ if(tag[x])pd(x); int mid=l+r>>1; if(l1<=mid)cov(l,mid,x<<1); if(r1>mid)cov(mid+1,r,x<<1|1); s[x]=max(s[x<<1],s[x<<1|1]); } } int pcnt[1111111]={0},plas[1111111]={0}; int f[2222222]; int bit[2222222]; inline void bitupd(int x,int v){for (;x<=tot;x+=x&-x)bit[x]=max(bit[x],v);} inline int bitque(int x){int ans=0;for(;x;x-=x&-x)ans=max(ans,bit[x]);return ans;} set<int> se; int ans[1111111]; int main() { //int cl=clock(); //freopen("in","r",stdin); //freopen("jc.out","w",stdout); memset(build,-1,sizeof(build)); memset(intv,-1,sizeof(intv)); gn(n);gn(k); for (int i=1;i<=n;i++){ gn(a[i].l); gn(a[i].r); gn(a[i].p); a[i].id=i; } /*freopen("jc.out","r",stdin); gn(k); for (int i=1;i<=n;i++){ int x;gn(x); if(x<a[i].l ||x>a[i].r){ printf("no\n"); }else printf("yes"); } return 0;*/ sort(a+1,a+1+n,cmpp); k=0;int val=-1; for (int i=1;i<=n;i++){ if(val!=a[i].p){ val=a[i].p; a[i].p=++k; }else{ a[i].p=k; } } for (int i=1;i<=n;i++)if(i==1 || a[i].p!=a[i-1].p){ int j; for (j=i;j<=n && a[j].p==a[i].p;j++); sort(a+i,a+j,cmpr); int curv=a[j-1].r,cur=j-1; int tmptot=0; while(1){ while(cur>=i && a[cur].r>=curv){ qu.push(a[cur]); cur--; } if(qu.empty()){ if(cur==i-1)break; else { curv=a[cur].r; continue; } } node s=qu.top();qu.pop(); s.r=curv; curv--; tmp[++tmptot]=s; } for (int t=1;t<=tmptot;t++)a[j-t]=tmp[t]; } for (int i=1;i<=n;i++){ swap(a[i].l,a[i].r); a[i].l*=-1;a[i].r*=-1; } for (int i=1;i<=n;i++)if(i==1 || a[i].p!=a[i-1].p){ int j; for (j=i;j<=n && a[j].p==a[i].p;j++); sort(a+i,a+j,cmpr); int curv=a[j-1].r,cur=j-1; int tmptot=0; while(1){ while(cur>=i && a[cur].r>=curv){ qu.push(a[cur]); cur--; } if(qu.empty()){ if(cur==i-1)break; else { curv=a[cur].r; continue; } } node s=qu.top();qu.pop(); s.r=curv; curv--; tmp[++tmptot]=s; } for (int t=1;t<=tmptot;t++)a[j-t]=tmp[t]; } for (int i=1;i<=n;i++){ swap(a[i].l,a[i].r); a[i].l*=-1;a[i].r*=-1; } for (int i=1;i<=n;i++) fir[a[i].p]++; for (int i=2;i<=k;i++)fir[i]+=fir[i-1]; for (int i=1;i<=n;i++){ if(a[i].l>a[i].r){ printf("NIE\n"); return 0; } a[i].l--; ma[++tot]=a[i].l; ma[++tot]=a[i].r; } sort(ma+1,ma+1+tot); tot=unique(ma+1,ma+1+tot)-ma-1; for (int i=1;i<=n;i++){ a[i].l=lower_bound(ma+1,ma+1+tot,a[i].l)-ma; a[i].r=lower_bound(ma+1,ma+1+tot,a[i].r)-ma; ae(intv,a[i].r,i); ae(build,a[i].l,i); } //printf("tm1:%d\n",cl-clock());cl=clock(); f[0]=0; for (int i=1;i<=tot;i++){ f[i]=f[i-1]; for (int j=intv[i];~j;j=e[j].next){ int x=e[j].v; int t=nu[x];int p=a[x].p; l1=fir[p-1]+1,r1=fir[p-1]+t;an=0; cov(1,n,1); f[i]=max(f[i],an); } bitupd(tot-i+1,f[i]); for (int j=build[i];~j;j=e[j].next){ int x=e[j].v; int p=a[x].p; int t=++pcnt[p];nu[x]=t; an=bitque(tot-plas[p]); plas[p]=i; v=an; I=fir[p-1]+t; upd(1,n,1); } } printf("%d\n",f[tot]); //printf("tm2:%d\n",cl-clock());cl=clock(); // for (int i=2;i<=tot;i++)if(f[i]!=f[i-1])se.insert(i); for (int i=1;i<=n;i++)if(i==1 || a[i].p!=a[i-1].p){ int j; for (j=i;j<=n && a[j].p==a[i].p;j++); sort(a+i,a+j,cmpr); for (int k=i;k<j;k++){ int t=*se.lower_bound(a[k].l+1); ans[a[k].id]=t; se.erase(t); } for (int k=i;k<j;k++){ se.insert(ans[a[k].id]); } } //printf("tm2:%d\n",cl-clock());cl=clock(); for (int i=1;i<=n;i++)printf("%d\n",ma[ans[i]]); return 0; } |