capacity 2182

1-n has a sequence number of an i know in front of it and it is smaller than the counted number, the seek sequence.
Segment tree, point to modify, point query.
More detailed point is the weight tree line, check the section k-th largest

#include <cstdio>

const int N = 8000 + 10;

int n,sumv[N<<2],a[N],ans[N];
void build(int rt,int L,int R){
	if(L == R) {
		sumv[rt] = 1;
		return ;
	}
	int mid = (L+R)/2;
	build(rt<<1,L,mid);
	build(rt<<1|1,mid+1,R);
	sumv[rt] = sumv[rt<<1] + sumv[rt<<1|1];
}
void update(int rt,int L,int R,int idx,int pos){
	if(L == R) {
		sumv[rt]--;
		ans[pos] = L;
		return ;
	}
	int mid = (L + R) /2;
	if(sumv[rt<<1] > idx) update(rt<<1,L,mid,idx,pos);
	else update(rt<<1|1,mid+1,R,idx-sumv[rt<<1],pos);
	sumv[rt] = sumv[rt<<1] + sumv[rt<<1|1]; 
}
int main(){
	while(~scanf("%d",&n)){
		a[0] = 0;
		for(int i = 1;i<n;i++) scanf("%d",&a[i]);
		build(1,1,n);
		for(int i = n-1;~i;i--){
			update(1,1,n,a[i],i);
		}
		for(int i = 0;i<n;i++) printf("%d\n",ans[i]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/winhcc/article/details/89480400