首页 技术 正文
技术 2022年11月19日
0 收藏 436 点赞 2,195 浏览 1468 个字

【POJ1151】Atlantis(线段树,扫描线)

题面

Vjudge

题解

学一学扫描线

其实很简单啦

这道题目要求的就是若干矩形的面积和

把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫)

这样只需要求出每次和\(x\)轴覆盖的长度

就可以两两相乘,求出面积

最后累计和就行啦

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 500
#define lson (now<<1)
#define rson (now<<1|1)
struct Node{double x1,x2,y,w;}p[MAX];
bool operator<(Node a,Node b){return a.y<b.y;}
double S[MAX];
int top;
int n,tot;
struct SegmentTreeNode
{
double ss;
int ly;
}t[MAX<<3];
void pushup(int now,int l,int r)
{
if(t[now].ly)t[now].ss=S[r+1]-S[l];
else if(l==r)t[now].ss=0;
else t[now].ss=t[lson].ss+t[rson].ss;
}
void Modify(int now,int l,int r,int L,int R,int w)
{
if(L<=l&&r<=R)
{
t[now].ly+=w;
pushup(now,l,r);
return;
}
int mid=(l+r)>>1;
if(L<=mid)Modify(lson,l,mid,L,R,w);
if(R>mid)Modify(rson,mid+1,r,L,R,w);
pushup(now,l,r);
}
int main()
{
int TT=0;
while(scanf("%d",&n))
{
++TT;
if(!n)break;
tot=top=0;
double x1,x2,y1,y2;
for(int i=1;i<=n;++i)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
p[++tot]=(Node){x1,x2,y1,1};
p[++tot]=(Node){x1,x2,y2,-1};
S[++top]=x1,S[++top]=x2;
}
sort(&S[1],&S[top+1]);
sort(&p[1],&p[tot+1]);
top=unique(&S[1],&S[top+1])-S-1;
double ans=0;
for(int i=1;i<tot;++i)
{
int l=lower_bound(&S[1],&S[top+1],p[i].x1)-S;
int r=lower_bound(&S[1],&S[top+1],p[i].x2)-S-1;
if(l<=r)Modify(1,1,top,l,r,p[i].w);
if(i!=tot)ans+=(p[i+1].y-p[i].y)*t[1].ss;
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",TT,ans);
memset(t,0,sizeof(t));
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918