【模板】扫描线

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() { // freopen("data.in","r",stdin); 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__

本文作者仰望星空的蚂蚁
本文链接https://www.cnblogs.com/cqbzly/p/17530253.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   仰望星空的蚂蚁  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示