题目大意:

有一些屋顶,相当于一些线段(不想交)。

问每一条线段能够接到多少水,相对较低的屋顶能够接到高屋顶留下的水(如题图所看到的)。因为y1!=y2,所以保证屋顶是斜的。



解题思路:

扫描线,由于对于同一个x最多有25条线段。所以不须要线段树更新。

在扫描线的过程中构造出线段与线段之间的关系。好在最后计算每一个屋顶能够接多少水。



以下是代码:

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> #define eps 1e-8
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define iabs(x) ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define min( x, y ) ( ((x) < (y)) ? (x) : (y) ) using namespace std; struct node
{
int x1,x2,y1,y2;
int num,next,w,deg;
double k;
} segment[400005]; int n,point[400000<<1],ans[400005],q[400005],a[100]; bool cmp(node a,node b)
{
return a.x1<b.x1;
} int main()
{
int last=0,now,tail=0,k,cnt,u,v,pointcnt,levelcnt;
while(scanf("%d",&n)!=EOF)
{
pointcnt=0;
for(int i=0; i<n; i++)
{
scanf("%d%d%d%d",&segment[i].x1,&segment[i].y1,&segment[i].x2,&segment[i].y2);
segment[i].num=i;
segment[i].next=-1;
segment[i].w=0;
segment[i].deg=0;
segment[i].k=(1.0*segment[i].y2-segment[i].y1)/(segment[i].x2-segment[i].x1);
point[pointcnt++]=segment[i].x1;
point[pointcnt++]=segment[i].x2;
} sort(point,point+pointcnt);
pointcnt=unique(point,point+pointcnt)-point; sort(segment,segment+n,cmp); last=0;
tail=0;
levelcnt=0; for(int i = 0; i < pointcnt ; last=now,i++)
{
now=point[i];
if(levelcnt)
{
segment[a[levelcnt-1]].w+=now-last;
}
while(segment[tail].x1==now&&tail<n)
{
k=-1;
for(int j=0; j<levelcnt; j++)
{
if( (segment[a[j]].y1 + segment[a[j]].k * (now - segment[a[j]].x1))<segment[tail].y1)k=j;
}
for(int j=levelcnt-1; j>k; j--)
{
a[j+1]=a[j];
}
a[k+1]=tail;
levelcnt++;
tail++;
}
for(int j=1; j<levelcnt; j++)
{
if((segment[a[j]].x1==now&&segment[a[j]].y1<segment[a[j]].y2)||(segment[a[j]].x2==now&&segment[a[j]].y1>segment[a[j]].y2))
{
segment[a[j]].next=a[j-1];
segment[a[j-1]].deg++;
}
}
cnt=0;
for(int j=0; j<levelcnt; j++)
{
if(segment[a[j]].x2!=now)a[cnt++]=a[j];
}
levelcnt=cnt;
}
queue <int >q;
for(int i=0; i<n; i++)
{
if(!segment[i].deg)q.push(i);
}
while(!q.empty())
{
u=q.front();
q.pop();
v=segment[u].next;
if(v==-1)continue;
segment[v].deg--;
segment[v].w+=segment[u].w;
if(!segment[v].deg)q.push(v);
}
for(int i=0; i<n; i++)
{
ans[segment[i].num]=segment[i].w;
} for(int i=0; i<n; i++)
{
printf("%d\n",ans[i]);
} }
return 0;
}



POJ 1765 November Rain的更多相关文章

  1. [POJ1765]November Rain

    [POJ1765]November Rain 试题描述 Contemporary buildings can have very complicated roofs. If we take a ver ...

  2. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...

  3. POJ题目分类推荐 (很好很有层次感)

    著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...

  6. Go语言interface详解

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...

  7. GoLang之方法与接口

    GoLang之方法与接口 Go语言没有沿袭传统面向对象编程中的诸多概念,比如继承.虚函数.构造函数和析构函数.隐藏的this指针等. 方法 Go 语言中同时有函数和方法.方法就是一个包含了接受者的函数 ...

  8. Go 接口(interface)

        文章转载地址:https://www.flysnow.org/2017/04/03/go-in-action-go-interface.html 1.什么是 interface? 简单的说,i ...

  9. Ext JS 4 的类系统

    前言 我们知道,JavaScript中没有真正的类,它是一种面向原型的语言 .这种语言一个强大的特性就是灵活,实现一个功能可以有很多不同的方式,用不同的编码风格和技巧.但随之也带来了代码的不可预测和难 ...

随机推荐

  1. Java设计模式透析之 —— 模板方法(Template Method)

    今天你还是像往常一样来上班,一如既往地開始了你的编程工作. 项目经理告诉你,今天想在server端添加一个新功能.希望写一个方法.能对Book对象进行处理.将Book对象的全部字段以XML格式进行包装 ...

  2. uiautomator中InteractionController学习笔记(8)

    4.1版本号 InteractionController将用户的键盘事件注入到android系统中,与系统进行交互(电视为什么不能设计成可组装,屏幕多大自己决定,想 多大就多大) click(int, ...

  3. Hello The World! —— 致我们无悔的IT之旅

    感谢IT,让我有了这么可爱活泼的伙伴. 有了KsCla,Coming,lhx_QAQ,tututu,AB_ever,Fat-zhang,wka,lhm这些伙伴神犇的陪伴,我的OI历程不至于那么枯燥无味 ...

  4. Common webpart properties in kentico

    https://devnet.kentico.com/docs/7_0/devguide/index.html?common_web_part_properties.htm HTML Envelope ...

  5. 英语发音规则---D字母

    英语发音规则---D字母 一.总结 一句话总结: 1.D发[d]音? doctor ['dɒktə] n. 医生:博士 bread [bred] n. 面包:生计 hand [hænd] n. 手,手 ...

  6. Docker私服仓库Harbor安装

    Harbor安装那里还是很简单,就是在Docker Login那里掉坑里去了,搞半天,写博客的时候,又重新安装了一遍 1.准备两台服务器 centos7 harbor 10.19.46.15 clie ...

  7. ROS-动态参数

    前言:在节点外部改变参数的方式有:参数服务器.服务.主题以及动态参数. 1.新建cfg文件 在chapter2_tutorials包下新建cfg文件夹,在cfg文件夹下新建chapter2.cfg文件 ...

  8. 常用相关linux命令

    查看进程netstat -tnlp | egrep "(9097)" lsof -i:9097 ps -ef | grep kafka 观察句柄变化lsof -p $pid | w ...

  9. C# 读取硬盘信息 ManagementClass类

    一.在很多情况下,你可能都需要得到微机的硬件信息.需要加上下面的这句话: using System.Management; 获取硬件信息,需先知道硬件参数信息: Win32_Processor, // ...

  10. Eclipse插件Lambok,实现自动生成Java代码

    1.下载Lombok.jar http://projectlombok.googlecode.com/files/lombok.jar 2.运行Lombok.jar: java -jar  D:\00 ...