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 ...
随机推荐
- ACM题目————还是畅通工程
Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...
- Tautology 分类: POJ 2015-06-28 18:40 10人阅读 评论(0) 收藏
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10428 Accepted: 3959 Descri ...
- 第十二届浙江省大学生程序设计大赛-Beauty of Array 分类: 比赛 2015-06-26 14:27 12人阅读 评论(0) 收藏
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...
- Cimg代码初探
Cimg代码初探 程序设计最为激动人心的地方,在于丰富的并且容易被查阅到资料.比如对于图像处理,固然有Opencv等较为丰富.被广泛知晓的类库:也有其他很多具有一定特色的类库.在这段时间里面, ...
- windows下wordpress环境快速搭建
所需要软件下载网址:https://bitnami.com/ 安装使用说明网址:http://www.websoft9.com/wp-content/plugins/documente/documen ...
- ThinkPHP eq neq if 标签
内置标签的使用方法 在action文件输出一个变量 $title="hello"; $this->assign('title',$title); 如果title变量的值等于& ...
- HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 2016年12月3日 星期六 --出埃及记 Exodus 20:24
2016年12月3日 星期六 --出埃及记 Exodus 20:24 "`Make an altar of earth for me and sacrifice on it your bur ...
- c#存储过程
1. 只返回单一记录集的存储过程 SqlConnection sqlconn = new SqlConnection(conn); SqlCommand cmd = new SqlCo ...
- PowerDesigner连接SqlServer数据库