Thinking:
About this question, we have to think reversely. We first get all the input and arrange the sequence from the last guy who enters the queue. Because the last guy will not be effected by previous ones. For example, if the N th guy jumped to position[N], then he should be the position[N]+1 guy at last. What about N-1 th guy? Suppose all the N positions are empty at the beginning and no one takes them, he will find the position[N--1]+1 th empty place to stand.(You can draw a picture.) It is the same to N-2,N-3...
Thus we can use segment tree to store how many empty space there are in the interval.
AC code:
#include<iostream>
#include<cstring>
#define maxn 200005
using namespace std;
int space[maxn * 3];
int N;
int final_arrange[maxn];
int posi[maxn], vale[maxn];
void build(int rt, int l,int r)
{
space[rt] = r - l + 1;
if (l == r)
return;
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
}
void fix(int rt, int l,int r, int pos, int val)
{
space[rt]--;
if (l == r)
{
final_arrange[l] = val;
return;
}
int mid = (l + r )>> 1;
if (pos <= space[rt<<1])
{
fix(rt << 1, l, mid, pos, val);
}
else fix(rt << 1 | 1, mid+1, r, pos - space[rt<<1], val);
}
int main()
{
while (~scanf("%d", &N))
{
build(1, 1, N);
for (int i = 1; i <= N; i++)
scanf("%d%d", &posi[i], &vale[i]);
for (int i = N; i >= 1; i--)
{
fix(1,1,N, posi[i]+1, vale[i]);
}
for (int i = 1; i <= N-1; i++)
{
printf("%d ", final_arrange[i]);
}
printf("%d\n", final_arrange[N]);
}
return 0;
}