您的位置:首页 > 大数据 > 人工智能

【POJ1765】November Rain——扫描线+线段树

2016-04-02 16:59 447 查看
November Rain

Time Limit: 5000MSMemory Limit: 65536K
Case Time Limit: 2000MS

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

Source

Central Europe 2003

题意:给你房子的屋顶的纵界面的两点的坐标,判断每一个房子所能接到的水的体积

分析:由于数据比较的大,但是房子的个数相对比较的小,需要将数据离散化一下,用两个线段树,一个表示从空中接到的水的体积,另外一个表示从其他的屋顶接到的水的体积。难点是如何对所给的屋顶进行排序使的被覆盖的在后面,由于不同的屋顶保证不交叉,所以对于x坐标有交叉的判断是不是有覆盖。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

const int Max = 100000;

typedef long long LL;

typedef struct node
{
LL x1,y1;

LL x2,y2;

int Id;

bool operator < (const node &b)const
{
if(b.x1>=x1&&b.x1<=x2)//判断x坐标有交叉的屋顶之间的覆盖关系。
{
double y = (double)((y2-y1)*(b.x1- x1))/(x2-x1)+y1;

return y>(double)b.y1;
}

if(b.x2>=x1&&b.x2<=x2)
{
double y = (double)((y2-y1)*(b.x2-x1))/(x2-x1)+y1;

return y>(double)b.y2;
}
return min(y1,y2)==min(b.y1,b.y2)?x2<b.x1:min(y1,y2)>min(b.y1,b.y2);
}

}Point ;

Point P[Max];

typedef struct Node//将两颗线段树放在了一起。
{
LL sum1;

LL sum2;

bool flag1,flag2;
}Tree;

Tree Tr[Max*8];

int n;

LL a[Max*3];

int Hash[Max*3];

int num ;

int Bound(int l,int r,int d)//二分
{
while(l<=r)
{

int mid = (l+r)>>1;

if(Hash[mid]==d)
{
return mid;
}

if(Hash[mid]>d)
{
r = mid-1;
}
else
{
l = mid+1;
}
}
return 0;
}

void PushUp(int st)
{
Tr[st].sum1 = Tr[st<<1].sum1+Tr[st<<1|1].sum1;

Tr[st].sum2 = Tr[st<<1].sum2+Tr[st<<1|1].sum2;
}

void PushDown(int st)
{
if(Tr[st].flag1)
{
Tr[st<<1].sum1 = Tr[st<<1|1].sum1 = Tr[st].sum1;

Tr[st<<1].flag1 = Tr[st<<1|1].flag1 = true;

Tr[st].flag1 = false;
}

if(Tr[st].flag2)
{
Tr[st<<1].sum2 = Tr[st<<1|1].sum2 =Tr[st].sum2;

Tr[st<<1].flag2 = Tr[st<<1|1].flag2 = true;

Tr[st].flag2= false;
}
}

void BuildTree(int L,int R,int st)//初始化建树。
{
Tr[st].flag1 = false;

Tr[st].flag2 = false;

if(L == R)
{
Tr[st].sum1 = Hash[L]-Hash[L-1];

Tr[st].sum2 = 0;

return ;
}
int mid = (L+R)>>1;

BuildTree(L,mid,st<<1);

BuildTree(mid+1,R,st<<1|1);

PushUp(st);
}

void Cover1(int L,int R,int st,int l,int r)
{

if(l==L&&r==R)
{
Tr[st].flag1= true;

Tr[st].sum1 = 0 ;

return ;
}

PushDown(st);

int mid = (L+R) >> 1;

if(r<=mid) Cover1(L,mid,st<<1,l,r);

else if(l>mid) Cover1(mid+1,R,st<<1|1,l,r);

else
{
Cover1(L,mid,st<<1,l,mid);

Cover1(mid+1,R,st<<1|1,mid+1,r);
}
PushUp(st);
}
void Cover2(int L,int R,int st,int l,int r)
{

if(l==L&&r==R)
{
Tr[st].flag2 = true;

Tr[st].sum2 = 0 ;

return ;
}

PushDown(st);

int mid = (L+R) >> 1;

if(r<=mid) Cover2(L,mid,st<<1,l,r);

else if(l>mid) Cover2(mid+1,R,st<<1|1,l,r);

else
{
Cover2(L,mid,st<<1,l,mid);

Cover2(mid+1,R,st<<1|1,mid+1,r);
}
PushUp(st);
}

void Update(int L,int R,int st,int x,int d)
{
if(L == R)
{
Tr[st].sum2 = d;

return ;
}

PushDown(st);

int mid = (L+R)>>1;

if(x<=mid) Update(L,mid,st<<1,x,d);

else Update(mid+1,R,st<<1|1,x,d);

PushUp(st);
}

LL Query1(int L,int R,int st,int l,int r)
{
if(l==L&&r==R) return Tr[st].sum1;

PushDown(st);

int mid = (L+R)>>1;

if(r<=mid) return Query1(L,mid,st<<1,l,r);

else if(l>mid) return Query1(mid+1,R,st<<1|1,l,r);

else return Query1(L,mid,st<<1,l,mid)+Query1(mid+1,R,st<<1|1,mid+1,r);
}

LL Query2(int L,int R,int st,int l,int r)
{
if(l==L&&r==R) return Tr[st].sum2;

PushDown(st);

int mid = (L+R)>>1;

if(r<=mid) return Query2(L,mid,st<<1,l,r);

else if(l>mid) return Query2(mid+1,R,st<<1|1,l,r);

else return Query2(L,mid,st<<1,l,mid)+Query2(mid+1,R,st<<1|1,mid+1,r);
}
int main()
{
while(~scanf("%d",&n))
{
num = 0;

for(int i = 0; i<n;i++)
{
scanf("%lld %lld %lld %lld",&P[i].x1,&P[i].y1,&P[i].x2,&P[i].y2);

a[num++] = P[i].x1; a[num++] = P[i].x2;

P[i].Id = i;
}

sort(P,P+n);

sort(a,a+num);

int ant = 1;

Hash[1]=a[0];

Hash[0]=a[0];

for(int i = 1;i<num;i++)//数据离散化
{
if(a[i]!=a[i-1])
{
Hash[++ant] = a[i];
}
}

BuildTree(1,ant,1);

for(int i = 0;i<n;i++)
{
int L = Bound(1,ant,P[i].x1);

int R = Bound(1,ant,P[i].x2);

a[P[i].Id] = Query1(1,ant,1,L+1,R)+Query2(1,ant,1,L,R);

Cover1(1,ant,1,L+1,R);

Cover2(1,ant,1,L,R);

if(P[i].y1>P[i].y2)
{
Update(1,ant,1,R,a[P[i].Id]);
}
else
{
Update(1,ant,1,L,a[P[i].Id]);
}
}
for(int i = 0;i<n;i++)
{
printf("%lld\n",a[i]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: