线段树单点更新的维护

所谓单点更新是指线段树中插入或删除某叶节点,从根节点出发,通过二分查找确定叶节点序号,单点更新的维护是从下而上,即从对应的叶节点出发,调整父节点路径上的每个节点的状态。

Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 26868   Accepted: 12846

Description

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

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 ≤ i ≤ N). 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

Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

线段树单点更新的维护_第1张图片

Source

POJ Monthly--2006.05.28, Zhu, Zeyuan

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

题意:插队问题,有人插进x位置,其他人往后移。

题解:线段树+逆序

 首先不要将题理解错了,题意是插队,而不是站位,所以有一组样例是不存在的,然后线段树单点维护就不会有错。

/*错误样例
4
3 1
2 2
1 3
0 1
*/

#include 
using namespace std;
int n;
int sum[800005]; 	//空位数量 
int ans[800005];	//真实存放位置	(线段树开4*n大小 ) 
struct node{
	int i,v;
}a[200005];
void change_sum(int i)
{
   sum[i] = sum[i*2] + sum[i*2+1];
}
void build_tree(int l,int r,int i)	//从节点i出发,构建区间[l,r]线段树 
{
  if(l==r)			//线段树叶节点 
  {
     sum[i]=1;  	//空位为 1 
     return;
  }
  int mid = (l + r) / 2;
  build_tree(l,mid,i*2);		//递归左子区间 
  build_tree(mid+1,r,i*2+1);	//递归右子区间 
  change_sum(i);				//以当前节点为根节点,统计节点所代表的区间[l,r]的空位数 
}
void updata(int l,int r,int i,int pos,int v)	//区间[l,r],从第i个节点出发,将第pos位置的数v存入 
{
   if(l==r)
   {
      sum[i] = 0;	//到达叶节点,将v插入,当前叶节点空位数从1变0 
      ans[r] = v;
      return ;		 
   }
   int mid = (l + r) / 2;
   if(pos<=sum[i*2])	//如果当前节点i的左子树空位数量>pos位置,左区间递归 
   {
   updata(l, mid,i*2,pos,v);	
   } 
   else
   updata(mid+1,r,i*2+1,pos-sum[i*2],v);	//否则右区间递归 ,并且pos-左子树空位数量,在右子树定位 
   change_sum(i);		//递归返回,修改空位数 
}
void solve()
{
	build_tree(1, n, 1);	//建树 
    for(int i=n;i>0;i--)	//逆向插点 ,在某位置第一次插入的点,位置不在改变 
  	{
    	updata(1,n,1,a[i].i+1,a[i].v);// 区间[1,n] 从节点根节点1出发 
  	}
  	for(int i=1;i<=n;i++)
    {
        printf("%d ",ans[i]);
    }
    printf("\n");
}
int main()
{
   	while(~scanf("%d", &n))
   	{
      	for(int i=1;i<=n;i++)
      	scanf("%d %d", &a[i].i,&a[i].v);
      	solve();
   	}
   return 0;
}

 

你可能感兴趣的:(ACM算法)