[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.

输入示例

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

输出示例

2
4
2
11
0
3

数据规模及约定

见“输入

题解

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

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

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

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

#include <iostream>
#include <cstdio>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = getchar(); }
	return x * f;
}

#define maxn 40010
#define maxx 1000010
#define maxm 40010
#define eps 1e-6
int n, ca, cm, f[maxn];
struct Line {
	int x1, y1, x2, y2;
	double slop;
	Line() {}
	Line(int _1, int _2, int _3, int _4, double _5): x1(_1), y1(_2), x2(_3), y2(_4), slop(_5) {}
} ls[maxn];
struct Point {
	double x, h, slop;
	int id;
	Point() {}
	Point(double _1, double _2, double _3, int _4): x(_1), h(_2), slop(_3), id(_4) {}
	bool operator < (const Point& t) const { return fabs(h - t.h) <= eps ? 0 : h < t.h; }
} ad[maxn], mi[maxn], tmp[30], ae[30];
bool cmp(Point a, Point b) { return a.x < b.x; }

int m, head[maxn], to[maxm], next[maxm], ind[maxn], lst[maxn], cl;
void AddEdge(int a, int b) {
//	printf("edge: %d -> %d\n", a, b);
	to[++m] = b; next[m] = head[a]; head[a] = m;
	ind[b]++;
	return ;
}

int vis[maxn], num[maxn<<1], cnt, A[maxn<<1];
int main() {
	n = read();
	for(int i = 1; i <= n; i++) {
		int x1 = read(), y1 = read(), x2 = read(), y2 = read();
		double slop = (double)(y2 - y1) / (x2 - x1);
		ls[i] = Line(x1, y1, x2, y2, slop);
		num[++cnt] = x1; num[++cnt] = x2;
	}
	sort(num + 1, num + cnt + 1);
	cnt = unique(num + 1, num + cnt + 1) - num - 1;
	for(int i = 1; i < cnt; i++) A[i] = num[i+1] - num[i];
	for(int i = 1; i <= n; i++) {
		ls[i].x1 = lower_bound(num + 1, num + cnt + 1, ls[i].x1) - num;
		ls[i].x2 = lower_bound(num + 1, num + cnt + 1, ls[i].x2) - num;
		ad[++ca] = Point(ls[i].x1, ls[i].y1, ls[i].slop, i);
		mi[++cm] = Point(ls[i].x2, ls[i].y2, ls[i].slop, i);
	}
	sort(ad + 1, ad + ca + 1, cmp);
	sort(mi + 1, mi + cm + 1, cmp);

	memset(vis, -1, sizeof(vis));
	int ka = 1, km = 1, kt = 0;
	for(int i = 1; i <= cnt; i++) {
		for(int j = 1; j <= kt; j++) tmp[j].h += tmp[j].slop * A[i-1];
		int ke = 0;
		while(ad[ka].x == i) {
			if(ad[ka].slop > 0.0) ae[++ke] = ad[ka];
			tmp[++kt] = ad[ka];
			ka++;
		}
		sort(tmp + 1, tmp + kt + 1);
		for(int j = 1; j <= ke; j++) {
			int k = lower_bound(tmp + 1, tmp + kt + 1, Point(-1, ae[j].h, -1, -1)) - tmp;
			if(k == 1) continue; k--;
			AddEdge(ae[j].id, tmp[k].id);
		}
//		for(int j = 1; j <= kt; j++) printf("%d ", tmp[kt].id); putchar(‘\n‘);
		ke = 0;
		while(mi[km].x == i) {
			if(mi[km].slop < 0.0) ae[++ke] = mi[km];
			vis[mi[km].id] = i;
			km++;
		}
//		for(int j = 1; j <= kt; j++) printf("%d: %.2lf\n", tmp[j].id, tmp[j].h);
		for(int j = 1; j <= ke; j++) {
			int k = lower_bound(tmp + 1, tmp + kt + 1, Point(-1, ae[j].h, -1, -1)) - tmp;
//			if(ae[j].id == 5) printf("here!!! %d %.2lf\n", k, ae[j].h);
			if(k == 1) continue; k--;
			AddEdge(ae[j].id, tmp[k].id);
		}
		for(int k = 1; k <= kt; k++)
			if(vis[tmp[k].id] == i) {
				swap(tmp[k], tmp[kt]);
				kt--; k--;
			}
		sort(tmp + 1, tmp + kt + 1);
		f[tmp[kt].id] += A[i];
//		printf("add_id: %d %d\n", tmp[kt].id, A[i]);
	}

	for(int i = 1; i <= n; i++) if(!ind[i]) lst[++cl] = i;
	int i = 1;
	for(; i <= cl; i++)
		for(int e = head[lst[i]]; e; e = next[e]) {
			ind[to[e]]--;
			if(!ind[to[e]]) lst[++cl] = to[e];
		}
	for(int i = 1; i <= n; i++) {
		int u = lst[i];
		for(int e = head[u]; e; e = next[e])
			f[to[e]] += f[u];
	}

	for(int i = 1; i <= n; i++) printf("%d\n", f[i]);

	return 0;
}
时间: 2024-08-29 02:01:31

[POJ1765]November Rain的相关文章

POJ 1765 November Rain

题目大意: 有一些屋顶,相当于一些线段(不想交).问每一条线段可以接到多少水,相对较低的屋顶可以接到高屋顶留下的水(如题图所示).由于y1!=y2,所以保证屋顶是斜的. 解题思路: 扫描线,因为对于同一个x最多有25条线段,所以不需要线段树更新. 在扫描线的过程中构造出线段与线段之间的关系,好在最后计算每个屋顶可以接多少水. 下面是代码: #include <set> #include <map> #include <queue> #include <math.h

串口通讯(DMA模式)

在高级语言中,I/O 流输入(input)操作一般都要求指定要读取的数据的最大长度(字节数).当接收到至少1字节.最多所指定的字节数时,函数返回. STM32 串口接收数据时,HAL API 要求指定数据长度.但无论轮询.中断或是DMA方式,都必须完整地接收到这么多字节,程序流程才继续.如何接收变长消息,我想不到特别好的实现方式.一种方式是,轮询加超时.另一种方式是,设计消息协议,使消息头为定长,且消息头内包含消息体的长度.但是,如果通讯异常,导致消息数据错误或丢失,那么,还是缺少"提前返回&q

build-web-application-with-golang学习笔记

build-web-application-with-golang 学习教程 这几周学习以上教程,仅记录一些重点难点部分. Go语言 Go语言基础 Go是一门类似C的编译型语言,但是它的编译速度非常快.这门语言的关键字总共也就二十五个: break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue

[ES6系列-04]再也不乱“哇”了:用 let 与 const 替代 var

[原创]码路工人 Coder-Power 大家好,这里是码路工人有力量,我是码路工人,你们是力量. github-pages 博客园cnblogs 今天的内容是,关于 JavaScript 中定义变量的变化(其实不确切,函数,常量表示被冷落). 首先,回顾下 var 定义存在的问题 1. 哇..var 好乱.. 1.1 可以重复定义 /* eg.0 * multi-definition of var-variable */ //----------------------------------

Go基础(七):接口

interface Go 语言里面设计最精妙的应该算 interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被 interface 的巧妙设计所折服. 什么是 interface 简单的说,interface 是一组 method 签名的组合,我们通过 interface 来定义对象的一组行为. 我们前面一章最后一个例子中 Student 和 Employee 都能 SayHi,虽然他们的内部实现不一样,但是那不重要,重要的是他们都能 say hi 让我们来继续做更多的

HDU 2389 Rain on your Parade

http://acm.hdu.edu.cn/showproblem.php?pid=2389 题意:给暴风雨到来的时刻,m个人的坐标和单位速度,和n个救生衣的坐标.每个救生衣只能匹配一个人,求最多有多少人可以得到救生衣. 题解:典型二分图最大匹配题型.因为点比较多,使用Hopcroft-Karp算法.讲解:http://blog.csdn.net/wall_f/article/details/8248373 http://www.cnblogs.com/-sunshine/archive/201

407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater th

某校内题 rain

Task 3. 雨中冒险 ( rain.cpp/c/pas) [ 问题描述] 有 n 个节点,标号为 1..n. m 条双向公路连接着这些节点,其中第 i 条公路 连接着 u_i 和 v_i,从一端走到另一端需要 w_i 秒.现在,小 Y 打算从学校回到 家里. 学校是节点 1,小 Y 家是节点 n,保证存在至少一条从节点 1 到节点 n 的路 径. 在第 0 秒,小 Y 身处节点 1,他的目标是尽早到达节点 n.根据天气预报, 接下来会有 k 次暴雨,第 i 次暴雨的时间为第 l_i 秒至第

November 23rd 2016 Week 48th Wednesday

I always like walking in the rain, so no one can see me crying. 我一直喜欢在雨中行走,那样就没人能看到我的眼泪. I like walking in the sunny days, because the feeling is very good. If the world seems cold to you, kindle fires to warm it. 如果你觉得这世界一片冰冷,就点燃一堆火去温暖它. Kindle a fi