取出纵向边按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. count(*)、count(val)和count(1)的解释

    一.关于count的一些谣言: 1.count(*)比count(val)更慢!项目组必须用count(val),不准用count(*),谁用扣谁钱! 2.count(*)用不到索引,count(va ...

  2. 2015 AlBaath Collegiate Programming Contest B

    Description Yaaaay, Haven't you heard the news? Bakaloria results are out! And Reem had very good gr ...

  3. sudo详解

    一. sudo的特点 sudo扮演的角色注定了它要在安全方面格外谨慎,否则就会导致非法用户攫取root权限.同时,它还要兼顾易用性,让系统管理员能够更有效,更方便地使用它.sudo设计者的宗旨是:给用 ...

  4. ContentProvider官方教程(9)定义一个provider完整示例:实现方法,定义权限等

    Creating a Content Provider In this document Designing Data Storage Designing Content URIs Implement ...

  5. itoa

    功能:把int转为字符数组 eg: int a=100: char ch[3]; itoa(a,ch,10)://十进制 ---->ch[0]==1;...

  6. H5网站借鉴

    http://www.rdinfo.com.cn/index.shtml http://www.winployee.com/ http://www.lkkdesign.com/ http://www. ...

  7. 看看,这就是微软的“万物互联”系统 window10 IOT

    今天在深圳 WinHEC2015 大会上,微软正式发布了其基于 Windows 10 开发的,专门用于一系列物联网设备的操作系统:Windows 10 IoT for Smart Devices(是的 ...

  8. request请求对象实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoRequest.as ...

  9. C#读写本地ini

    //读写INI public class GF_INI { [DllImport("kernel32")] private static extern long WritePriv ...

  10. nohup不输出日志信息的方法,及linux重定向学习

    起因 最近使用nohup创建了一个后台进程,默认日志输出到了nohup.out文件中,程序跑起来也就没再管,过了大约一周,发现硬盘空间不够了,于是查找原因,发现这个nohup.out文件已经到了70G ...