POJ2828 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:

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.

Source

POJ Monthly--2006.05.28, Zhu, Zeyuan


题意&思路

有n个人依次来排队,每个人有一个pos和val值,pos表示站到当前第pos个人后。如果pos=0,则占到第一个位置。n个人都到后,问最后的队伍顺序是什么?(输出每个人的val值)

  • 看到学长留下的三个字“反着来”,自己瞎想出一个算法:

倒着处理每个人的情况,用树状数组维护1~pos中的人数k,那么pos+k就是当前这个人最终站的位置。因为树状数组下标从1开始,所以读入的时候所有pos都+1即可

  • 然而这样做是错的-_-# 因为pos+k处可能有人占用,然后pos到pos+k处也可能有人……总之情况乱七八糟,直接这样做的话肯定不行。最后只好去看题解,思路也很简单

  • 还有一个细节是线段树里面getsum()的写法,非常巧妙

逆序处理的时候,直接找到第pos+1(第pos个人之后相当于pos+1)个空位即可。利用线段树维护,修改和查询可以在一个函数中完成。

Code

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define FA(i,s,t) for(int i=(s);i<=(t);i++)
#define FD(i,s,t) for(int i=(s);i>=(t);i--)
#define PRI pair<int,int>
#define LLD long long
using namespace std;

int getint()
{
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

const int INF=(int) 2e9;
const int MAXN=10000+50;

struct tnode
{
	int l,r,c;
}st[MAXN*16];
int vsb[MAXN];

inline bool t_ins(int t,int l,int r)
{
	return st[t].l>=l&&st[t].r<=r;
}
inline bool t_out(int t,int l,int r)
{
	return st[t].l>r||st[t].r<l;
}

#define ls (t<<1)
#define rs (t<<1|1)

inline void pushdown(int t)
{
	if(st[t].c)
	{
		st[ls].c=st[rs].c=st[t].c;
		st[t].c=0;
	}
}
void build(int t,int l,int r)
{
	st[t].l=l;
	st[t].r=r;
	st[t].c=0;
	
	if(l==r) return;
	
	build(ls,l,(l+r)>>1);
	build(rs,((l+r)>>1)+1,r);
}
void cover(int t,int l,int r,int x)
{
	if(t_out(t,l,r))
	{
		return;
	}
	if(t_ins(t,l,r))
	{
		st[t].c=x;
		return;
	}
	
	pushdown(t);
	
	cover(ls,l,r,x);
	cover(rs,l,r,x);
}
void query1(int t)
{
	if(st[t].l==st[t].r)
	{
		vsb[st[t].c]=1;
		return;
	}
	
	pushdown(t); 
	
	query1(ls);
	query1(rs);
}
void query2(int t)
{
	if(st[t].c||(st[t].l==st[t].r))
	{
		vsb[st[t].c]=1;
		return;
	}
	
	query2(ls);
	query2(rs);
}

int n,d[MAXN*2],ans,mp[MAXN*2],f[MAXN*2];
PRI lsh[MAXN*2];

void program()
{
	n=getint();
	
	FA(i,1,n)
	{
		d[i*2-1]=getint();
		d[i*2]=getint();
		lsh[i*2-1]=make_pair(d[i*2-1],i*2-1);
		lsh[i*2]=make_pair(d[i*2],i*2);
	}
	
	sort(lsh+1,lsh+n*2+1);
	
	int cnt=0;
	f[lsh[1].second]=++cnt;
	
	FA(i,2,n*2)
	{
		if(lsh[i].first==lsh[i-1].first)
		{
			f[lsh[i].second]=cnt; //注意!! 
			continue;
		}
		if(lsh[i].first>lsh[i-1].first+1)
		{
			++cnt;
		}
		f[lsh[i].second]=++cnt;
	}
	
	int L=f[lsh[n*2].second];
	
	build(1,1,L);
	
	FA(i,1,n)
	{
		cover(1,f[i*2-1],f[i*2],i);
	}
	
	memset(vsb,0,sizeof(vsb));
	query2(1);
	
	ans=0;
	FA(i,1,n)
	{
		if(vsb[i]) ans++;
	}
	
	printf("%d
",ans);
}

int main()
{
	int t=getint();
	
	FA(i,1,t)
	{
		program();
	}
	
	return 0;
}
----不要温顺地走进那良宵
原文地址:https://www.cnblogs.com/Hist/p/5014871.html