poj 2828

线段树点更新。

最直观的想法就是用链表模拟,发现数据量太大,不可能。然后看能否直接推出每个人在最终序列的位置。很显然,最后一个插入的人插入的位置p[n]+1就是他在最终序列中的位置,先确定了最后一个人的位置。然后看倒数第二个,先不考虑最后一个人的插入,在他前面所排的人的个数有且仅为他插入的位置,那在最终的序列中不包括最后一个人,他前面的人数肯定有且仅为 pos[i]。也就是在长度为n的序列中,不包括最后一个人的位置中的第pos[i]+1个位置。之前插入的人的道理也是如此,i在最终长度为n序列中,除去他后面插入的人已经确定的位置,肯定是在poi[i]+1的位置,在这个位置,排在他之前的人的除去后面插入的人有且仅有pos[i]个人。问题就最终转化为了从后向前确定每个人早最终序列中的位置,找到所在位置为除去已经确定的位置的空位中的第p[i]+1个位置,用二分的思想确定此位置,再次用线段树优化。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=200000+10;
int re;
int pos[maxn],id[maxn],ans[maxn];
int seg[maxn*3];
int n;
void build(int no, int left,int right )
{
	if(left==right)
	{
		seg[no]=1;
		return;
	}
	int mid=left+(right-left)/2;
	seg[no]=right-left+1;
	build(no*2+1,left,mid);
	build(no*2+2,mid+1,right);
}
void update(int no,int left,int right,int needed)
{
	if(left==right) 
	{
		re=left;
		seg[no]--;
		return;
	}
	int mid=left+(right-left)/2;
	if(seg[no*2+1]>=needed) update(no*2+1,left,mid,needed);
	else update(no*2+2,mid+1,right,needed-seg[no*2+1]);
	seg[no]--;
}
int main()
{

	while(~scanf("%d",&n))
	{
		int i;
		build(0,1,n);
		for(i=1;i<=n;i++) scanf("%d%d",&pos[i],&id[i]);
		for(i=n;i>=1;i--)
		{
			update(0,1,n,pos[i]+1);
			ans[re]=id[i];
		}
		printf("%d",ans[1]);
		for(i=2;i<=n;i++) printf(" %d",ans[i]);
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值