取出纵向边按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矩形面积并的更多相关文章

  1. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  2. HDU1542 扫描线(矩形面积并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. 【hdu1542】线段树求矩形面积并

    分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...

  4. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  5. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  6. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

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

  8. 扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage

    Area Coverage Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit user ...

  9. 【HDU 1542】Atlantis(线段树+离散化,矩形面积并)

    求矩形面积并,离散化加线段树. 扫描线法: 用平行x轴的直线扫,每次ans+=(下一个高度-当前高度)*当前覆盖的宽度. #include<algorithm> #include<c ...

随机推荐

  1. wordpress网站被挂马以及防御方法

    wordpress本身的安全性是非常的高的,一般不会被轻易的破解,被挂马,但是我们也不能够过度迷信wordpress的安全性,凡是连接上互联网的服务器和电脑,都存在被破解的风险性.所以我们在日常维护自 ...

  2. Codeforces 733C:Epidemic in Monstropolis(暴力贪心)

    http://codeforces.com/problemset/problem/733/C 题意:给出一个序列的怪兽体积 ai,怪兽只能吃相邻的怪兽,并且只有体积严格大于相邻的怪兽才能吃,吃完之后, ...

  3. java JPEGImageEncoder;图像处理

    在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JP ...

  4. $q服务的API详解

    下面我们通过讲解$q的API让你更多的了解promise异步编程模式.$q是做为angularjs的一个服务而存在的,只是对promise异步编程模式的一个简化实现版,源码中剔除注释实现代码也就二百多 ...

  5. HDU 1715 大菲波数

    大菲波数 问题描述 : Fibonacci数列,定义如下: f(1)=f(2)=1 f(n)=f(n-1)+f(n-2)  n>=3. 计算第n项Fibonacci数值. 输入: 输入第一行为一 ...

  6. hdoj4906 Our happy ending(2014 Multi-University Training Contest 4)

    对于一个定长(size = n)的数列a, 若其存在“位置相关”的子集(含空集)使得该子集所有元素之和为k,那么将数列a计数. 其中数列a中任一元素a[i]在[0, l]内自由取值. 数据条件0≤n, ...

  7. 验证码识别--type7

    验证码识别--type7 一.干扰分析 有黑色边框,然后点干扰,线干扰 去边框 去点干扰 变成这样的结果,方便运算吗?也可以多种方式联合起来运算的.我相信在很多情况下,都可能会遇到类似的结果.我们人类 ...

  8. linux内核2.4.x网络接口分析层次图

    http://blog.csdn.net/wswifth/article/details/5108744 今天大概分写了下<Linux内核2.4.x的网络接口源码的结构[转]>中的结构层次 ...

  9. CentOS 5/6.X 使用 EPEL YUM源

    参考:http://www.linuxidc.com/Linux/2013-08/88523.htm 大纲 一.什么是EPEL? 二.与163 YUM源比较 三.CentOS 5.X 安装使用EPEL ...

  10. 2016年12月7日 星期三 --出埃及记 Exodus 21:2

    2016年12月7日 星期三 --出埃及记 Exodus 21:2 "If you buy a Hebrew servant, he is to serve you for six year ...