——深深感谢你——|

_yolanda

园龄:3年7个月粉丝:11关注:15

2022-02-15 14:40阅读: 54评论: 0推荐: 0

线段树-扫描线

这其实是计算几何的一部分。一般被用来解决图形面积、周长等问题。

Atlantis 问题(板题)

题意

求给定的 n 个矩形的面积并

解法

如图:

  • 每一块颜色不同的部分都是一个矩形,这 n 个矩形的面积并就是重新划分后各个颜色的矩形的面积和

  • 扫过的距离 乘 当前的宽度就是当前矩形的面积


(从左到右扫)

若一个矩形的左下顶点坐标为 (x1,y1) ,右上顶点坐标为 (x2,y2)

  • 将它的左沿和右沿两条边存成一个四元组;左:(x1,y1,y2,1),右:(x2,y1,y2) 。最后的标记:1 表示加入这条边;1 表示减去这条边

  • n 个矩形都这样做处理,就得到 2n 条边,把这 2n 条边按照 x 的大小从小到大排序

  • 因为数据可能很大而且不一定是整数,所以要对 y 进行 离散化 处理


模板代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int n;
double ls[N<<1];
struct line{
int x,y1,y2,mrk;
}a[N<<4];
struct node{
int l,r,sum,len;//sum:被完全覆盖的次数 ; len:被选取的长度
}tr[N<<4];
bool cmp(line x,line y){
return x.x<y.x;
}
void build(int node,int l,int r){
tr[node].l=l,tr[node].r=r;
tr[node].len=0;
tr[node].sum=0;
if(l==r)
return;
int mid=(l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
return;
}
void pushup(int node){
int l=tr[node].l,r=tr[node].r;
if(tr[node].sum)
tr[node].len=ls[r+1]-ls[l];
else
tr[node].len=tr[node<<1].len+tr[node<<1|1].len;
}
void change(int node,int l,int r,int x){
int L=tr[node].l,R=tr[node].r;//L,R与l,r 意义完全不同
if(ls[R+1]<=l || ls[L]>=r)//R+1才能覆盖完区间
return;
if(l<=ls[L] && r>=ls[R+1]){
tr[node].sum+=x;
pushup(node);
return;
}
change(node<<1,l,r,x);
change(node<<1|1,l,r,x);
pushup(node);
}
signed main(){
cin>>n;
for(int i=1;i<=n;++i){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
a[i*2-1].x=x1,a[i*2-1].y1=y1,a[i*2-1].y2=y2;
a[i*2].x=x2,a[i*2].y1=y1,a[i*2].y2=y2;
a[i*2-1].mrk=1,a[i*2].mrk=-1;
ls[i*2-1]=y1,ls[i*2]=y2;
}
n<<=1;//后面用起来方便
sort(a+1,a+n+1,cmp);
sort(ls+1,ls+n+1);//离散化
int tot=unique(ls+1,ls+n+1)-ls-1;//去重
ls[tot+1]=0x3f3f3f3f;
build(1,1,tot);//建树
int ans=0;
for(int i=1;i<n;++i){
change(1,a[i].y1,a[i].y2,a[i].mrk);
ans+=tr[1].len*(a[i+1].x-a[i].x);
}
cout<<ans;
return 0;
}

拓展题目

求周长

本文作者:_yolanda

本文链接:https://www.cnblogs.com/yolanda-yxr/p/15896440.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   _yolanda  阅读(54)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 化身孤岛的鲸 周深
  2. 2 像鸟儿一样(Live) 周深
  3. 3 Rubia 周深
  4. 4 痕迹 周深
Rubia - 周深
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Rubia (崩坏3《渡尘》动画短片印象曲) - 周深

词:TetraCalyx

曲:蔡近翰Zoe(HOYO-MiX)

编曲:宫奇Gon(HOYO-MiX)/杨启翔Frex(HOYO-MiX)

Life blooms like a flower

Far away or by the road

Waiting for the one

To find the way back home

Rain falls a thousand times

No footprints of come-and-go

You who once went by

Where will you belong

I feel your sigh and breath

In the last blow of wind

Not yet for the story on the last page

It's not the end

Life blooms like a flower

Far away or by the road

Waiting for the one

To find the way back home

Time flows across the world

There is always a longer way to go

Till I reach your arms

A Madder there for you

Up against the stream

Waterways will join as one

Tracing to the source

No more strayed or lost

You will see petals fly

When lament becomes carol

Could you please hear my voice

That hungers for a duo

Life blooms like a flower

Far away or by the road

Waiting for the one

To find the way back home

Time flows across the world

There is always a longer way to go

Till I reach your arms

A Madder there for you

Life blooms like a flower

Far away or by the road

Waiting for the one

To find the way back home

Time flows across the world

There is always a longer way to go

Till I reach your arms

A Madder there for you

人声录音 Recording:徐威Aaron Xu

混音/母带 Mixing&Mastering Engineer:宫奇Gon(HOYO-MiX)

制作人 Producer:蔡近翰Zoe(HOYO-MiX)

特别鸣谢 Special Thanks:周深工作室

出品 Produced by:HOYO-MiX