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

思路简单说一下吧

从做到右遍历每一条矩阵的边(左右边),看该边对右边的面积贡献是正还是负(矩阵左边为正,右边为负),在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 Dual Palindromes 解题报告

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

  2. (原、整)BSP的江湖传说

    @author:黑袍小道 查看随缘,当苦无妨,良人可归.     引言 为什么叫江湖传说,因为实现了第一人是卡马克,就这么简单.(不接受那啥) Quake3:http://www.mralligato ...

  3. java单例模式(类只能创建唯一对象)

    //饿汉式 class Single { private static final Single s= new Single(); private Single(){} public static S ...

  4. 上手Caffe(一)

    @author:oneBite 本文记录编译使用caffe for windows 使用环境 VS2013 ultimate,win7 sp1,caffe-windows源码(从github上下载ca ...

  5. springboot13 Hikari 和Introspector

    SpringBoot Initializr Introspector(内省) class TestReflect { @Test fun testReflect() { //获取字节码对象 val c ...

  6. HDU 4107 Gangster(线段树 特殊懒惰标记)

    两种做法. 第一种:标记区间最大值和最小值,若区间最小值>=P,则本区间+2c,若区间最大值<P,则本区间+c.非常简单的区间更新. 最后发一点牢骚:最后query查一遍就行,我这个2B竟 ...

  7. Android记事本11

    昨天: Activity的启动模式. 今天: 分析了一些网上的例子的源码. 遇到问题: 无.

  8. MyBatis中使用OGNL表达式与操作符

    String与基本数据类型 _parameter 自定义类型(对象) 属性名 集合 数组 array List list Map _parameter 获取集合中的一条数据 数组 array[索引] ...

  9. Dev express 笔记

    1.设置treelist不同行的颜色 void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDr ...

  10. [AGC010E] Rearranging [拓扑排序+堆]

    题面 传送门 思路 首先,一个显然的结论是:Alice调整过后的序列中任意两个不互质的数的相对顺序无法改变 那么我们可以以这个性质为突破口 我们在两个不互质的权值的点之间连一条边(没错这是个图论题!! ...