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

思路简单说一下吧

从做到右遍历每一条矩阵的边(左右边),看该边对右边的面积贡献是正还是负(矩阵左边为正,右边为负),在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. Percona-Tookit工具包之pt-summary

      Preface       As a dba,We are obliged to master several basic tools(such as vmstat,top,netstat,ios ...

  2. Djano之写api使用django_rest_framework【海瑞博客】

    使用django rest framework 可以更快速和友好的编写api,当然网上有很多教程,对于高手来说相对很简单,对于新手来说,根本搞不明白.那是你没有搞明白你自己的职责,做为后端,我们只要提 ...

  3. Action参数和View、Json、重定向

    一.Action 1.Action参数: 普通参数.Model类.FormCollection (1).普通参数 Index(string name,int age)   框架会自动把用户请求的Que ...

  4. (原)Unreal渲染相关的缓冲区 及其 自定义代码几种抓取

    @authot: 白袍小道   转载说明那啥即可. (图片和本文无关,嘿嘿,坑一下)         以下为Unreal4.18版本中对GPUBuffer部分的分析结果 (插入:比之够着,知至目的) ...

  5. initialization of 'zf' is skipped by 'case' label原因及解决方法

    原因:switch 的 case 中不能定义变量,不然就会报错.可能是变量的初始化会因为有时候case条件不被执行而跳过. 解决方法: 1:在case中用{}将代码括起来,这样在{}中就能定义变量了; ...

  6. 一步步精通NodeJs的简单实例

    一步一步完成nodejs+express+mysql的简单实例开发 1.使用express创建简单web服务器 项目文件下安装express,这个不多说,很简单,直接在项目目录下运行 npm inst ...

  7. PAT 甲级 1037 Magic Coupon

    https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472 The magic shop in Mars ...

  8. Hexo安装配置详解

    原文 http://blog.csdn.net/tonydandelion2014/article/details/61615898 http://www.joryhe.com/2016-06-06- ...

  9. P3456 [POI2007]GRZ-Ridges and Valleys

    题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是相邻的格子.(所以与(i,j)相邻的格子有(i ...

  10. div样式

    DIV样式汇总 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height:200px ...