Buy Tickets
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:
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. ![]() Source
POJ Monthly--2006.05.28, Zhu, Zeyuan
|
提示
题意:
要在中国的新年里买到一张火车票是很困难的,因此我们不得不排起了长长的队伍。。。
农历新年快到了,但不幸的是一只小猫还计划要乘火车到四川省绵阳的冬季信息学奥林匹克竞赛国家队。
现在是凌晨一点,从西北吹来的寒风并没有吓跑排队的人。寒冷的夜晚让小猫打哆嗦。为什么不找个问题想一想呢?这比冻死更好!
人们不停地插队,由于周围太黑,这样不会被发现。小猫想:“如果队列中的n个人(1<=n<=200000)都被分配一个值v(0<=v<=32767),并且给出第i个人插队后前方有pos个人(0<=pos<=i-1),我能找出队列中的人的最后顺序吗?“
思路:
线段树单点更新,图解戳这。
我们需要先记录每个人的信息,之后反向用线段树存,就由后来的在前面变成了先来的在前面了,方便我们做记录。
时间复杂度好高啊,不知怎么优化。
示例程序
Source Code
Problem: 2828 Code Length: 1171B
Memory: 8892K Time: 3625MS
Language: GCC Result: Accepted
#include <stdio.h>
struct
{
int l,r,num;
}line[800000];
struct
{
int x,val;
}a[200000];
int id[200000],pos;
void create(int t,int l,int r)
{
int mid;
line[t-1].l=l;
line[t-1].r=r;
line[t-1].num=r-l+1; //当前区间所剩的位置
if(l==r)
{
return;
}
mid=(l+r)/2;
create(t*2,l,mid);
create(t*2+1,mid+1,r);
}
void update(int t,int l,int r,int p)
{
int mid;
line[t-1].num--;
if(l==r) //找到位置
{
pos=l-1;
return;
}
mid=(l+r)/2;
if(line[t*2-1].num>=p)
{
update(t*2,l,mid,p);
}
else
{
update(t*2+1,mid+1,r,p-line[t*2-1].num);
}
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF)
{
create(1,1,n);
for(i=0;n>i;i++) //存信息
{
scanf("%d %d",&a[i].x,&a[i].val);
}
for(i=n-1;i>=0;i--) //反向录入
{
update(1,1,n,a[i].x+1);
id[pos]=a[i].val; //记录每个人的值
}
for(i=0;n>i;i++)
{
if(i==0)
{
printf("%d",id[i]);
}
else
{
printf(" %d",id[i]);
}
}
printf("\n");
}
return 0;
}