之前写过这个算法,时间长了就忘掉了,,现在不看书自己努力回想起来,对算法的理解,对线段树的理解感觉也更深了一点(可能心理作用,哈哈哈)

思路简单说一下吧

从做到右遍历每一条矩阵的边(左右边),看该边对右边的面积贡献是正还是负(矩阵左边为正,右边为负),在y轴上用线段树维护在y轴的贡献值,与x轴上该边与下一条边的差值相乘即可

 // acm1.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
typedef long long lint;
const ll mod=1e9+;
const int maxn=1e2+;
const int maxm=1e5+;
const double eps=1e-;
int ar1[maxn],ar2[maxn],m,n;
bool bo[maxn];
struct line{
double y1,y2,x;
int ma;
}li[maxn*]; struct node{
int l,r;
int mark;
double length;
}no[maxn*];
double xl[maxn],yl[maxn];
bool cmp(line l1,line l2){
return l1.x<l2.x;
}
void build(int x,int y,int o){
no[o].l=x;
no[o].r=y;
no[o].length=;
no[o].mark=;
if(y-x==)return ;
int mid=(x+y)>>;
if(mid>x)build(x,mid,o*);
if(mid<y)build(mid,y,o*+);
}
void down(int x){
if(no[x].mark>)no[x].length=yl[no[x].r]-yl[no[x].l];
else if(no[x].r==no[x].l+){
no[x].length=;
}
else{
no[x].length=no[x*].length+no[x*+].length;
}
}
void update(int o,line & liMid){
double func1=yl[no[o].l],func2=yl[no[o].r];
if(liMid.y1<=func1&&liMid.y2>=func2){
no[o].mark+=liMid.ma;
}
else{
double funcMid=yl[(no[o].l+no[o].r)>>];
if(liMid.y1<funcMid)update(o*,liMid);
if(liMid.y2>funcMid)update(o*+,liMid);
}
down(o);
}
int main()
{
int fir=;
while(~scanf("%d",&n)&&n){
for(int i=;i<n;i++){
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
li[i*].x=x1;
li[i*].y1=y1;
li[i*].y2=y2;
li[i*].ma=;
li[i*+].x=x2;
li[i*+].y1=y1;
li[i*+].y2=y2;
li[i*+].ma=-;
yl[i*]=y1;
yl[i*+]=y2;
}
sort(li,li+*n,cmp);
sort(yl,yl+*n);
int len=unique(yl,yl+*n)-yl;
build(,len-,);
double ans=;
for(int i=;i<*n-;i++){
update(,li[i]);
ans+=no[].length*(li[i+].x-li[i].x);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",++fir,ans);
}
return ;
}

扫面线+线段树(hdu1542)的更多相关文章

  1. POJ1151-扫面线+线段树+离散化//入门题

    比较水的入门题 记录矩形竖边的x坐标,离散化排序.以被标记的边建树. 扫描线段树,查询线段树内被标记的边.遇到矩形的右边就删除此边 每一段的面积是查询结果乘边的横坐标之差,求和就是答案 #includ ...

  2. 矩阵重叠面积计算 线段树hdu1542

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

  3. HDU3333 Turing Tree(线段树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=3333 Description After inventing Turing Tree, 3x ...

  4. POJ 2482 Stars in Your Window 线段树扫描线

    Stars in Your Window   Description Fleeting time does not blur my memory of you. Can it really be 4 ...

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

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

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

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

  7. hdu1542 Atlantis (线段树+扫描线+离散化)

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

  8. Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army [线段树+线性扫一遍]

    传送门 D. R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...

随机推荐

  1. USACO Section1.2 Milking Cows 解题报告

    milk2解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  2. 抓包工具 - Fiddler - (一)

    <转载于 miantest> Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888 ...

  3. Linux常用命令及工具记录(持续更新)

    一.命令 convmv   作用:文件名的编码转换   安装:sudo apt-get install convmv   使用:convmv * -f gbk -t utf8 --notest   c ...

  4. 通过设计表快速了解sql语句中字段的含义

    打开Navicat-------> 选择数据库 ------->右键设计表------>查看下方注释

  5. J2EE的十三种技术——JNDI

    背景: 上一篇博客中介绍了J2EE的十三种技术之一--JDBC,主要用于提供了统一访问多种数据库的方式.这篇文章我们继续介绍J2EE的技术--JNDI. JNDI: Java Naming and D ...

  6. KVC 开发详情

    目录 概述 KVC基本技术 KVC访问函数 KVC搜索顺序 KVC集合操作 一.概述 kvc全名是Key-value coding,kvc是一种通过字符串间接的访问oc对象的属性的一种技术. 一个oc ...

  7. MPLAB设置路径

    大家都知道在MPLAB环境下编译程序,c文件.h文件.编译器生成的文件等等,都会被编译器无情的放在一个项目文件夹下. 稍微有些讲究的程序员可能就会觉得用MPLAB项目组织的一团糟.于是大家想到了一种方 ...

  8. BZOJ3990 排序(sort)

    排序(sort) 题目描述 小A有一个1~2N的排列A[1..2N],他希望将数组A从小到大排序.小A可以执行的操作有N种,每种操作最多可以执行一次.对于所有的i(1<=i<=N),第i种 ...

  9. makefile语法

    makefile很重要 什么是makefile? 或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional 的程 ...

  10. bootstrap3基本了解

    使用 BootCDN 提供的免费 CDN 加速服务(同时支持 http 和 https 协议) Bootstrap 中文网 为 Bootstrap 专门构建了免费的 CDN 加速服务,访问速度更快.加 ...