TLE on #2 #9 #10
#include<bits/stdc++.h>
using namespace std;
const int maxN=1000010;
struct node{
int bg,ed,id;
} a[maxN];
struct node2{
int id,last;
} tmp[maxN];
bool cmp(node n1,node n2){
return n1.bg<n2.bg;
}
bool operator<(node2 n1,node2 n2){
return n1.last>n2.last;
}
priority_queue<node2> pq;
int b[maxN];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].bg,&a[i].ed);
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++) pq.push(node2{i,0});
for(int i=1;i<=n;i++){
node2 nd;
int cnt=0,p=1e9;
while(!pq.empty() && pq.top().last<a[i].bg){
tmp[++cnt]=pq.top();
p=p<pq.top().id?p:pq.top().id;
pq.pop();
}
b[a[i].id]=p;
pq.push(node2{p,a[i].ed});
for(int j=cnt;j;j--){
if(tmp[j].id!=p) pq.push(tmp[j]);
tmp[j]=node2{0,0}; //
}
}
int ans=0;
for(int i=1;i<=n;i++) ans=ans>b[i]?ans:b[i];
cout<<ans<<endl;
for(int i=1;i<=n;i++) printf("%d\n",b[i]);
return 0;
}