http://poj.org/problem?id=1765
http://www.voidcn.com/article/p-zgpuhifb-qy.html
Contemporary buildings can have very complicated roofs. If we take a vertical section of such a roof it results in a number of sloping segments. When it is raining the drops are falling down on the roof straight from the sky above. Some segments are completely exposed to the rain but there may be some segments partially or even completely shielded by other segments. All the water falling onto a segment as a stream straight down from the lower end of the segment on the ground or possibly onto some other segment. In particular, if a stream of water is falling on an end of a segment then we consider it to be collected by this segment.
For the purpose of designing a piping system it is desired to compute how much water is down from each segment of the roof. To be prepared for a heavy November rain you should count one liter of rain water falling on a meter of the horizontal plane during one second.
Task
Write a program that:
reads the description of a roof,
computes the amount of water down in one second from each segment of the roof,
writes the results.
For the purpose of designing a piping system it is desired to compute how much water is down from each segment of the roof. To be prepared for a heavy November rain you should count one liter of rain water falling on a meter of the horizontal plane during one second.
Task
Write a program that:
reads the description of a roof,
computes the amount of water down in one second from each segment of the roof,
writes the results.
Input
The first line of the input contains one integer n (1 <= n < = 40000) being the number of segments of the roof. Each of the next n lines describes one segment of the roof and contains four integers x1, y1, x2, y2 (0 <= x1, y1, x2, y2 < = 1000000, x1 < x2, y1<>y2) separated by single spaces. Integers x1, y1 are respectively the horizontal position and the height of the left end of the segment. Integers x2, y2 are respectively the horizontal position and the height of the right end of the segment. The segments don't have common points and there are no horizontal segments. You can also assume that there are at most 25 segments placed above any point on the ground level.
Output
The output consists of n lines. The i-th line should contain the amount of water (in liters) down from the i-th segment of the roof in one second.
Sample Input
6 13 7 15 6 3 8 7 7 1 7 5 6 5 5 9 3 6 3 8 2 9 6 12 8
Sample Output
2 4 2 11 0 3
题意:有很多倾斜的屋檐,这些屋檐不会相交,每个屋檐会接收雨水,并从较低短流到另一个在它下面的屋檐,要求每秒钟每个屋檐流下的水量,最多有25个屋檐在同一垂直线上
分析:这题直接假想有一条垂直的扫描线从最左端开始,向右扫描,每个事件点为屋檐的端点,每次如果为屋檐的左端点,就加入队列,如果是右端点,就从队列中删掉,队列里的屋檐,按低到高排序,通过判断队列中屋檐的相对位置,是否有水流下,是否被覆盖。。。等等就可以算出每个屋檐自己接到的雨水,和每个屋檐的关系,然后根据拓扑排序,把在高处的雨水流到较低的屋檐就行。。。
struct point
{
int x,y,id,fl;
}g[mm];
double a[mm],b[mm],c[mm];
int head[mm],ver[mn],next[mn];
int sum[mm],cov[mm],q[mm],h[mm],s[mm],d[mm],flag[mm],down[mm];
int i,j,k,l,r,x1,x2,y1,y2,n,m,edge;
bool cmp(point a,point b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
void addedge(int u,int v)
{
ver[edge]=v,++d[v],next[edge]=head[u],head[u]=edge++;
}
void add(point p)
{
down[p.id]=p.fl;
if(!flag[p.id])
{
flag[p.id]=1;
int i=r++;
while(i>0&&(-a[q[i]]*p.x-c[q[i]])/b[q[i]]>p.y)q[i+1]=q[i],--i;
q[++i]=p.id;
}
else flag[p.id]=2;
}
int main()
{
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
while(scanf("%d",&n)!=-1)
{
for(m=0,i=1;i<=n;++i)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
a[i]=y1-y2,b[i]=x2-x1,c[i]=x1*y2-x2*y1;
s[i]=min(x1,x2);
h[i]=min(y1,y2);
j=y1<y2;
g[m].x=x1,g[m].y=y1,g[m].fl=j,g[m].id=i,m++;
g[m].x=x2,g[m].y=y2,g[m].fl=!j,g[m].id=i,m++;
d[i]=cov[i]=flag[i]=sum[i]=0;
head[i]=-1;
}
sort(g,g+m,cmp);
r=edge=i=0;
while(i<m)
{
x1=g[i].x;
add(g[i++]);
while(i<m&&g[i].x==x1)add(g[i++]);
for(j=1;j<=r;++j)
{
if(!cov[q[j]])sum[q[j]]+=x1-s[q[j]];
s[q[j]]=x1;
}
for(j=1;j<r;++j)
if(down[q[j+1]])addedge(q[j+1],q[j]);
for(k=0,j=1;j<=r;++j)
if(flag[q[j]]<2)q[++k]=q[j];
for(r=k,j=1;j<=r;++j)down[q[j]]=0,cov[q[j]]=1;
cov[q[r]]=0;
}
for(r=0,i=1;i<=n;++i)
if(!d[i])q[r++]=i;
for(l=0;l<r;++l)
for(i=head[q[l]];i>=0;i=next[i])
{
sum[ver[i]]+=sum[q[l]];
if(--d[ver[i]]==0)q[r++]=ver[i];
}
for(i=1;i<=n;++i)printf("%d\n",sum[i]);
}
return 0;
}