扫面线+线段树(hdu1542)
之前写过这个算法,时间长了就忘掉了,,现在不看书自己努力回想起来,对算法的理解,对线段树的理解感觉也更深了一点(可能心理作用,哈哈哈)
思路简单说一下吧
从做到右遍历每一条矩阵的边(左右边),看该边对右边的面积贡献是正还是负(矩阵左边为正,右边为负),在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)的更多相关文章
- POJ1151-扫面线+线段树+离散化//入门题
比较水的入门题 记录矩形竖边的x坐标,离散化排序.以被标记的边建树. 扫描线段树,查询线段树内被标记的边.遇到矩形的右边就删除此边 每一段的面积是查询结果乘边的横坐标之差,求和就是答案 #includ ...
- 矩阵重叠面积计算 线段树hdu1542
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU3333 Turing Tree(线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=3333 Description After inventing Turing Tree, 3x ...
- 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 ...
- 【hdu1542】线段树求矩形面积并
分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 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 ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
随机推荐
- python学习笔记四:lambda表达式和switch
一.定义 lambda arg1,arg2... : returnValue 二.示例 #!/usr/bin/python def f(x,y): return x*y print f(2,3) g ...
- js valueOf和toString方法
JavaScript原生提供一个Object对象,所有其他对象都继承自这个对象,Object对象有valueOf和valueOf方法,所以所有JS数据类型都继承了这两种方法. valueOf:返回 ...
- swift中的正则表达式
swift中的t正则表达式 正则表达式是对字符串操作的一种逻辑公式,用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规则字符串" ...
- Python学习-KindEditor-富文本编辑框
1.进入官网 2.下载 官网下载:http://kindeditor.net/down.php 本地下载:http://files.cnblogs.com/files/wupeiqi/kindedit ...
- 微信小程序--获取form表单初始值提交数据
<form bindsubmit="formSubmit"> <view class="txt"> <view class=&qu ...
- 华为手机怎么安装Google
华为手机怎么安装google 新买了个华为荣耀九,结果安装Google Play提示gms core 步骤一 gms 安装器.应用市场已经下架了 地址:链接: 点击打开链接 密码: m63j 步骤二 ...
- VB.NET视频总结——基础篇
VB.NET视频是台湾讲师曹祖胜和林煌章共同带来的经典视频,视频中老师的台湾腔特别重,听起来有些别扭.而且对于计算机方面的术语翻译的与大陆有很大差异,所以刚开始看视频的时候总是进入不了状态,一头雾水的 ...
- 【bzoj4636】蒟蒻的数列 离散化+线段树
原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...
- java 图形化界面笔记(1)
目录 JFrame窗体......................................................................................... ...
- 【CZY选讲·扩展LCS】
题目描述 给出两个仅有小写字母组成的字符串str1 和str2,试求出两个串的最长公共子序列. 数据范围 |str1| ⩽ 1000; |str2| ⩽ 10^6 题解: ①直接进行LCS( ...