poj1765 November Rain 扫描线

Description

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. 

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


题意:求每个房顶接到的水量

分析:一根垂直的扫描线从左到右扫一遍,求出每个房顶最初接到的水,同时建立房顶之间的多对一的关系,因为任意x坐标被覆盖不超过25,暴力排序即可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<ostream>
#include<istream>
#include<algorithm>
#include<queue>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define inf (1<<30)
#define eps 1e-8
#define pb push_back
#define debug puts("========")
using namespace std;
const int maxn=40010;
struct Node
{
    int lr,ud;
    int id;
    int x;
    double h;
    bool operator<(const Node& u)const
    {
        return h>u.h;
    }
}node[50];
int e;
int n;
struct Line
{
    int x1,y1,x2,y2;
    int id;
}line[maxn];
int x[maxn*2];
vector<int>vec[maxn*2];
bool vis[maxn];
int nxt[maxn];
int deg[maxn];
int w[maxn];
void solve()
{
    queue<int>que;
    for(int i=1;i<=n;i++)
        if(deg[i]==0)
            que.push(i);
    while(!que.empty()) {
        int u=que.front();
        que.pop();
        if(nxt[u]==-1) continue;
        w[nxt[u]]+=w[u];
        deg[nxt[u]]--;
        if(deg[nxt[u]]==0)
            que.push(nxt[u]);
    }
}
int main()
{
    while(~scanf("%d",&n)) {
        int cnt=0;
        for(int i=1;i<=n;i++) {
            scanf("%d%d%d%d",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2);
            line[i].id=i;
            x[cnt++]=line[i].x1;
            x[cnt++]=line[i].x2;
        }
        sort(x,x+cnt);
        cnt=unique(x,x+cnt)-x;
        for(int i=0;i<cnt;i++)
            vec[i].clear();
        for(int i=1;i<=n;i++) {
            int k=lower_bound(x,x+cnt,line[i].x1)-x;
            vec[k].push_back(i);
            k=lower_bound(x,x+cnt,line[i].x2)-x;
            vec[k].push_back(i);
        }
        memset(vis,0,sizeof(vis));
        memset(w,0,sizeof(w));
        memset(nxt,-1,sizeof(nxt));
        memset(deg,0,sizeof(deg));
        e=0;
        Node tmp[50];
        for(int i=0;i<cnt;i++) {
            for(int j=0;j<vec[i].size();j++) {
                if(vis[vec[i][j]]) continue;
                vis[vec[i][j]]=1;
                node[e++].id=vec[i][j];
            }
            for(int j=0;j<e;j++) {
                int di=node[j].id;
                if(line[di].x1==x[i]) {
                    node[j].lr=1;
                    if(line[di].y1>line[di].y2)
                        node[j].ud=1;
                    else
                        node[j].ud=0;
                }
                else if(line[di].x2==x[i]) {
                    node[j].lr=0;
                    if(line[di].y1>line[di].y2)
                        node[j].ud=0;
                    else
                        node[j].ud=1;
                }
                else {
                    node[j].lr=node[j].ud=-1;
                }
                if(node[j].lr==1)
                    node[j].h=line[di].y1*1.0;
                else {
                    node[j].h=(line[di].y2-line[di].y1)*1.0/(line[di].x2-line[di].x1)*(x[i]-line[di].x1)+line[di].y1;
                }
            }
            sort(node,node+e);
            for(int j=0;j<e-1;j++) {
                if(node[j].ud==0) {
                    nxt[node[j].id]=node[j+1].id;
                    deg[node[j+1].id]++;
                }
            }
            int e1=0;
            for(int j=0;j<e;j++) {
                if(node[j].lr==0) {
                    vis[node[j].id]=0;
                    continue;
                }
                tmp[e1++]=node[j];
            }
            if(e1>0) {
                w[tmp[0].id]+=x[i+1]-x[i];
            }
            e=0;
            for(int j=0;j<e1;j++)
                node[e++]=tmp[j];
        }
        solve();
        for(int i=1;i<=n;i++)
            printf("%d\n",w[i]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值