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
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=100005; struct nodgd { int h,v; bool operator < (const nodgd& b) const { return h<b.h; } } a[maxn]; struct Tree { int left,right,sum; Tree(int l=0,int r=0):left(l),right(r),sum(0) {} }; struct Segment_Tree { Tree tree[maxn*4]; #define ls index<<1 #define rs index<<1|1 void push_up(int index) { tree[index].sum=tree[ls].sum+tree[rs].sum; } void build(int index,int Left,int Right) { tree[index]=Tree(Left,Right); if(Left==Right) { tree[index].sum=1; return; } int mid=(Left+Right)>>1; build(ls,Left,mid); build(rs,mid+1,Right); push_up(index); } void modify(int index,int target) { if(tree[index].left>target||tree[index].right<target)return; if(tree[index].left==tree[index].right) { tree[index].sum=0; return; } modify(ls,target); modify(rs,target); push_up(index); } int query(int index,int k) { if(tree[index].left==tree[index].right) { if(k==1)return tree[index].left; else return -1; } if(tree[ls].sum>=k)return query(ls,k); return query(rs,k-tree[ls].sum); } } st; int n,ans[maxn]; int main() { n=Get_Int(); for(int i=1; i<=n; i++) { a[i].h=Get_Int(); a[i].v=Get_Int(); } sort(a+1,a+n+1); st.build(1,1,n); for(int i=1; i<=n; i++) { int x=st.query(1,min(a[i].v+1,n-i-a[i].v+1)); if(x==-1) { puts("impossible"); return 0; } st.modify(1,x); ans[x]=a[i].h; } for(int i=1; i<=n; i++)printf("%d ",ans[i]); return 0; }
|