[POJ1765]November Rain

试题描述

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.

输入

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.

输出

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.

输入示例

  1.  

输出示例

  1.  

数据规模及约定

见“输入

题解

注意到每个竖直线上的交点不会超过 25 个,我们可以用扫描线后暴力乱搞。

每遇到一个屋檐的左端点就插入,遇到右端点就删除,在每次删除后处理天降雨的情况,即把最上面的屋檐集水量增加。

然后对于斜率为正的线段的左端点向下找到最靠近的一个屋檐向其连边,对于斜率为负的线段右端点同理。这样我们就可以得到一个 DAG,按拓扑序依次往后累加就好了(可以理解成 dp)。

这题有点卡常,需要离散一下。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cctype>
  6. #include <algorithm>
  7. #include <cmath>
  8. using namespace std;
  9.  
  10. int read() {
  11. int x = 0, f = 1; char c = getchar();
  12. while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
  13. while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
  14. return x * f;
  15. }
  16.  
  17. #define maxn 40010
  18. #define maxx 1000010
  19. #define maxm 40010
  20. #define eps 1e-6
  21. int n, ca, cm, f[maxn];
  22. struct Line {
  23. int x1, y1, x2, y2;
  24. double slop;
  25. Line() {}
  26. Line(int _1, int _2, int _3, int _4, double _5): x1(_1), y1(_2), x2(_3), y2(_4), slop(_5) {}
  27. } ls[maxn];
  28. struct Point {
  29. double x, h, slop;
  30. int id;
  31. Point() {}
  32. Point(double _1, double _2, double _3, int _4): x(_1), h(_2), slop(_3), id(_4) {}
  33. bool operator < (const Point& t) const { return fabs(h - t.h) <= eps ? 0 : h < t.h; }
  34. } ad[maxn], mi[maxn], tmp[30], ae[30];
  35. bool cmp(Point a, Point b) { return a.x < b.x; }
  36.  
  37. int m, head[maxn], to[maxm], next[maxm], ind[maxn], lst[maxn], cl;
  38. void AddEdge(int a, int b) {
  39. // printf("edge: %d -> %d\n", a, b);
  40. to[++m] = b; next[m] = head[a]; head[a] = m;
  41. ind[b]++;
  42. return ;
  43. }
  44.  
  45. int vis[maxn], num[maxn<<1], cnt, A[maxn<<1];
  46. int main() {
  47. n = read();
  48. for(int i = 1; i <= n; i++) {
  49. int x1 = read(), y1 = read(), x2 = read(), y2 = read();
  50. double slop = (double)(y2 - y1) / (x2 - x1);
  51. ls[i] = Line(x1, y1, x2, y2, slop);
  52. num[++cnt] = x1; num[++cnt] = x2;
  53. }
  54. sort(num + 1, num + cnt + 1);
  55. cnt = unique(num + 1, num + cnt + 1) - num - 1;
  56. for(int i = 1; i < cnt; i++) A[i] = num[i+1] - num[i];
  57. for(int i = 1; i <= n; i++) {
  58. ls[i].x1 = lower_bound(num + 1, num + cnt + 1, ls[i].x1) - num;
  59. ls[i].x2 = lower_bound(num + 1, num + cnt + 1, ls[i].x2) - num;
  60. ad[++ca] = Point(ls[i].x1, ls[i].y1, ls[i].slop, i);
  61. mi[++cm] = Point(ls[i].x2, ls[i].y2, ls[i].slop, i);
  62. }
  63. sort(ad + 1, ad + ca + 1, cmp);
  64. sort(mi + 1, mi + cm + 1, cmp);
  65.  
  66. memset(vis, -1, sizeof(vis));
  67. int ka = 1, km = 1, kt = 0;
  68. for(int i = 1; i <= cnt; i++) {
  69. for(int j = 1; j <= kt; j++) tmp[j].h += tmp[j].slop * A[i-1];
  70. int ke = 0;
  71. while(ad[ka].x == i) {
  72. if(ad[ka].slop > 0.0) ae[++ke] = ad[ka];
  73. tmp[++kt] = ad[ka];
  74. ka++;
  75. }
  76. sort(tmp + 1, tmp + kt + 1);
  77. for(int j = 1; j <= ke; j++) {
  78. int k = lower_bound(tmp + 1, tmp + kt + 1, Point(-1, ae[j].h, -1, -1)) - tmp;
  79. if(k == 1) continue; k--;
  80. AddEdge(ae[j].id, tmp[k].id);
  81. }
  82. // for(int j = 1; j <= kt; j++) printf("%d ", tmp[kt].id); putchar('\n');
  83. ke = 0;
  84. while(mi[km].x == i) {
  85. if(mi[km].slop < 0.0) ae[++ke] = mi[km];
  86. vis[mi[km].id] = i;
  87. km++;
  88. }
  89. // for(int j = 1; j <= kt; j++) printf("%d: %.2lf\n", tmp[j].id, tmp[j].h);
  90. for(int j = 1; j <= ke; j++) {
  91. int k = lower_bound(tmp + 1, tmp + kt + 1, Point(-1, ae[j].h, -1, -1)) - tmp;
  92. // if(ae[j].id == 5) printf("here!!! %d %.2lf\n", k, ae[j].h);
  93. if(k == 1) continue; k--;
  94. AddEdge(ae[j].id, tmp[k].id);
  95. }
  96. for(int k = 1; k <= kt; k++)
  97. if(vis[tmp[k].id] == i) {
  98. swap(tmp[k], tmp[kt]);
  99. kt--; k--;
  100. }
  101. sort(tmp + 1, tmp + kt + 1);
  102. f[tmp[kt].id] += A[i];
  103. // printf("add_id: %d %d\n", tmp[kt].id, A[i]);
  104. }
  105.  
  106. for(int i = 1; i <= n; i++) if(!ind[i]) lst[++cl] = i;
  107. int i = 1;
  108. for(; i <= cl; i++)
  109. for(int e = head[lst[i]]; e; e = next[e]) {
  110. ind[to[e]]--;
  111. if(!ind[to[e]]) lst[++cl] = to[e];
  112. }
  113. for(int i = 1; i <= n; i++) {
  114. int u = lst[i];
  115. for(int e = head[u]; e; e = next[e])
  116. f[to[e]] += f[u];
  117. }
  118.  
  119. for(int i = 1; i <= n; i++) printf("%d\n", f[i]);
  120.  
  121. return 0;
  122. }

[POJ1765]November Rain的更多相关文章

  1. POJ 1765 November Rain

    题目大意: 有一些屋顶,相当于一些线段(不想交). 问每一条线段能够接到多少水,相对较低的屋顶能够接到高屋顶留下的水(如题图所看到的).因为y1!=y2,所以保证屋顶是斜的. 解题思路: 扫描线,由于 ...

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

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

  3. Go语言interface详解

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

  4. GoLang之方法与接口

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

  5. Go 接口(interface)

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

  6. Ext JS 4 的类系统

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

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

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

  8. bzoj AC倒序

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

  9. GO方法与接口

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

随机推荐

  1. 数组Arrays

    1.toString 方法 Arrays的toString方法可以方便的输出一个数组的字符串形式,方便查看,它有九个重载的方法,包括八种基本类型数组和一个对象类型数组,这里列举两个: public s ...

  2. C#读写文本文件

    static public string Read(string path) { StreamReader sr = new StreamReader(path,Encoding.Default); ...

  3. [转载]将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,解决办法

    eclipse 代码中文注释乱码 求解决 将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclips ...

  4. NGINX 配置404错误页面转向

    什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...

  5. 股市非常态,CCI指标买卖点实例图解

    CCI指标即顺势指标,是唐纳德·蓝伯特于上世纪80年代提出的,是一种比较新颖的技术指标.CCI指标是专门用来衡量股价是否超出常态分布范围,是一种 超买超卖类指标,但它与其他超买超卖型指标又有自己比较独 ...

  6. AJAX笔试面试题汇总

    AJAX笔试面试题汇总 Javascript 1. 什么是ajax,为什么要使用Ajax(请谈一下你对Ajax的认识) 什么是ajax: AJAX是“Asynchronous JavaScript a ...

  7. PasswordHasher

    namespace Microsoft.AspNet.Identity { public class PasswordHasher : IPasswordHasher { public virtual ...

  8. mysql SELECT FOR UPDATE语句使用示例

    以MySQL 的InnoDB 为例,预设的Tansaction isolation level 为REPEATABLE READ,在SELECT 的读取锁定主要分为两种方式:SELECT ... LO ...

  9. 自定义列表dl的使用原因和场合

    为什么要使用自定义列表? dl和ol, ul的区别? 要正确理解dl的意图, 理解 dl的 "语义" ! 才能知道为什么要使用dl, 以及在什么时候/ 什么情况下使用 dl? dl ...

  10. centos 安装php7.0.2

    PHP7.0正式版已经在2015年11月份左右发布,目前是PHP7.0.2版本,本人最早是从2015年8月php7的第一个测试版跟起,现在正式版发布. linux版本:64位CentOS 6.6 Ng ...