HDU1542矩形面积并
取出纵向边按x坐标排序,在y方向上建立线段树。
每次查询当前有效长度len,ans += len*(x[i]-x[i-1]); 其中len为T[rt].len;
查询完毕后更新y方向上线段树,入边+1, 出边-1。
#include<bits/stdc++.h>
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef long long ll;
struct L{
double x, y1, y2;
int d;
L(){}
L(double x, double y1, double y2, int d): x(x), y1(y1), y2(y2), d(d){}
};
L line[];
bool cmp(L A, L B){
return A.x < B.x;
}
double y[]; struct Node{
int d;
double len;
};
Node T[<<];
void init(){
memset(T, , sizeof(T));
}
void pushup(int rt, int l, int r){
if(T[rt].d)
T[rt].len = y[r]-y[l-];
else
T[rt].len = l == r? : T[rt<<].len+T[rt<<|].len;
}
void update(int L, int R, int d, int l, int r, int rt){
if(L <= l&&r <= R){
T[rt].d += d;
pushup(rt, l , r);
return ;
}
int m = (l+r)>>;
if(L <= m) update(L, R, d, lson);
if(R > m) update(L, R, d, rson);
pushup(rt, l, r);
} int main(){
int n, ca = ;
double x1, y1, x2, y2;
while(scanf("%d", &n), n){
for(int i = ; i < n; i++){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[i*] = L(x1, y1, y2, );
line[i*+] = L(x2, y1, y2, -);
y[i*] = y1, y[i*+] = y2;
}
sort(line, line+*n, cmp);
sort(y, y+*n); init();
double ans = ;
for(int i = ; i < *n; i++){
if(i&&line[i].x != line[i-].x)
ans += (line[i].x-line[i-].x)*T[].len;
int l = lower_bound(y, y+*n, line[i].y1)-y+, r = lower_bound(y, y+*n, line[i].y2)-y;
if(l <= r)
update(l, r, line[i].d, , *n, );
}
printf("Test case #%d\n", ca++);
printf("Total explored area: %.2f\n\n", ans);
}
return ;
}
无pushdown()函数,每条线段只存一次。d表示被覆盖的次数,len表示至少被覆盖一次的合法长度。详见pushup()函数。
ans += T[1].len*(x[i]-x[i-1]);
求面积交:方法同求面积并,外加len2表示至少被覆盖两次的合法长度,ans += T[1].len2*(x[i]-x[i-1]);
求周长并:方法同面积并,扫描两次,分别沿x方向和y方向,每次加上 更新前后T[1].len的差值的绝对值。
HDU1542矩形面积并的更多相关文章
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
- HDU1542 扫描线(矩形面积并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 【hdu1542】线段树求矩形面积并
分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...
- HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)
传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有
package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...
- 扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage
Area Coverage Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit user ...
- 【HDU 1542】Atlantis(线段树+离散化,矩形面积并)
求矩形面积并,离散化加线段树. 扫描线法: 用平行x轴的直线扫,每次ans+=(下一个高度-当前高度)*当前覆盖的宽度. #include<algorithm> #include<c ...
随机推荐
- js实现鼠标的滑动
js实现鼠标的滑动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- C#:WPF绘制问题
1.问题描述:切换画笔后,鼠标呈现画笔,但绘制界面需要点击后才能绘制,体验比较差 注:如果将切换为画笔或橡皮擦的功能放在二级菜单中则无次问题 解决方法(大体如此): 1)在第三方中,先创建完绘制画面和 ...
- CSS 学习质料
1.学习CSS布局 http://zh.learnlayout.com/display.html
- CalParcess.php.
<?php require_once "OperSerVice.class.php"; //接受三个数 //isset if(!isset($_REQUEST['NUM1'] ...
- [转]ios 开发file's owner以及outlet与连线的理解
转载地址:http://www.cocoachina.com/bbs/simple/?t108822.html xib文件本身可以看做是一个xml,app启动的时候会根据xml构造xib对应的界面及其 ...
- Oracle关闭session锁
select session_id from v$locked_object;//找到锁的sessionId SELECT * FROM v$session //查看该sessionId以及对应的 ...
- Android 读取Assets中资源
//读取文件 private static String getFromAssets(Context context, String fileName) { String result = " ...
- 20150604_Andriod 窗体PopupWindow
package com.example.test1; import android.support.v7.app.ActionBarActivity;import android.os.Bundle; ...
- Unity脚本在层级面板中的执行顺序测试1
第二篇测试循环时和动态创建时的调用顺序:LINK 测试版本Unity4.6.因为新版本对Transform的排序做了改变,所以不排除旧版本的测试结果不一样.测试时,使用Awake中添加Debug.lo ...
- 链表——PowerShell版
链表是由一系列节点串连起来组成的,每一个节点包括数值部分和指针部分,上一节点的指针部分指向下一节点的数值部分所在的位置. 在C语言中我们有两种方式来定义链表—— 1.定义结构体:来表示链表中的节点,节 ...