Luogu_5490 【模板】扫描线
主要讲一个关于计算几何的扫描线算法。。
基础算法:动态开点 + 离散化
你考虑面积求交转化成线段求交即可。
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define db double
using namespace std;
const int Maxn=2e5+5;
int n,rt,tot,cnt,cnt2,num,b[Maxn];
struct node{
int x,y,y2,c;
bool operator <(const node &a)const {
return x<a.x;
}
}a[Maxn<<2];
struct SegmentTree{
int lc,rc,cnt,len;
}t[Maxn<<2];
int build() {
t[++tot].lc=0,t[tot].rc=0;
t[tot].cnt=0,t[tot].len=0;
return tot;
}
int getx(int x) {
return lower_bound(b+1,b+1+num,x)-b;
}
void pushup(int p,int l,int r){
if(t[p].cnt) t[p].len=b[r]-b[l];
else if(l+1==r) t[p].len=0;
else t[p].len=t[t[p].lc].len+t[t[p].rc].len;
}
void upd(int &p,int l,int r,int ql,int qr,int k) {
if(!p) p=build();
if(ql<=l&&r<=qr) {
t[p].cnt+=k;
pushup(p,l,r);
return;
}
if(l+1==r) return;
int mid=l+r>>1;
if(ql<=mid) {
upd(t[p].lc,l,mid,ql,qr,k);
}
if(mid<=qr) {
upd(t[p].rc,mid,r,ql,qr,k);
}
pushup(p,l,r);
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
int x,y,x2,y2;
scanf("%d%d%d%d",&x,&y,&x2,&y2);
a[++cnt]={x,y,y2,1};
a[++cnt]={x2,y,y2,-1};
b[++cnt2]=y,b[++cnt2]=y2;
}
sort(b+1,b+1+cnt2);
num=unique(b+1,b+1+cnt2)-b-1;
for(int i=1;i<=cnt;i++) {
a[i].y=getx(a[i].y);
a[i].y2=getx(a[i].y2);
}
sort(a+1,a+1+cnt);
ll res=0;
for(int i=1;i<cnt;i++) {
upd(rt,1,num,a[i].y,a[i].y2,a[i].c);
res+=1ll*(a[i+1].x-a[i].x)*t[1].len;
}
printf("%lld",res);
}
__EOF__
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」