http://poj.org/problem?id=2828
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 ≤ 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
一个长队,人们都来插队【好没素质的说
有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;
}