poj2828线段树

http://poj.org/problem?id=2828

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 13222 Accepted: 6560

Description

Input

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ iN). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

Sample Input

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492

Sample Output

77 33 69 51
31492 20523 3890 19243

一个长队,人们都来插队【好没素质的说


有n个人,每一组n行,每行输入p,v,意思是来了一个人,给他一个值设为v【两个人可以有相同的值】,这个人插在第p号位置【p从0开始】

然后输出队伍最后的形态。


建树的时候 末端分支初始化为1,然后向上更新的时候做和累加。


void build(int l,int r,int rt)
{
	if(l==r) {
		sum[rt]=1;
		return;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	PushUp(rt);
}
倒序插入,

然后只要保证1.该位置为空白位置。2.前面有pos个空白位置。

得到的位置就是最终位置。
方法是更新点的时候,查找第pos+1个空白位置

设为p

如果p小于等于左子树搜左子树

如果大于左子树,p-左子树的和,再搜右子树。

终止条件是找到末端分支,并且末端分支为空百分支。

void update(int p,int v,int l,int r,int rt){
	if(l==r&&sum[rt]==p&&p==1){
		sum[rt]=0;
		que[l]=v;
		return;
	}
	int m=(l+r)>>1;
	if(p<=sum[rt<<1]) update(p,v,lson);
	else {
        p-=sum[rt<<1];
        update(p,v,rson);
	}
	PushUp(rt);
}


完整代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 200100;
int sum[maxn<<2];
int que[maxn];
int pos[maxn],val[maxn];
void PushUp(int rt)
{
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
	if(l==r) {
		sum[rt]=1;
		return;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	PushUp(rt);
}
void update(int p,int v,int l,int r,int rt){
	if(l==r&&sum[rt]==p&&p==1){
		sum[rt]=0;
		que[l]=v;
		return;
	}
	int m=(l+r)>>1;
	if(p<=sum[rt<<1]) update(p,v,lson);
	else {
        p-=sum[rt<<1];
        update(p,v,rson);
	}
	PushUp(rt);
}

 int main()
 {
 	int n;
 	while(~scanf("%d",&n))
 	{

 		build(1,n,1);
 		char op[10];
 		for(int i=n;i>=1;i--)
            scanf("%d%d",&pos[i],&val[i]);
        for(int i=1;i<=n;i++)
        {
            update(pos[i]+1,val[i],1,n,1);
        }
 		for(int i=1;i<=n;i++)
        {
            if(i==1) printf("%d",que[i]);
            else printf(" %d",que[i]);
        }
        printf("\n");
 	}
 	return 0;
 }




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值