社区讨论
18pts bus error马蜂良好求条
P5490【模板】扫描线 & 矩形面积并参与者 3已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mhjayddm
- 此快照首次捕获于
- 2025/11/03 23:36 4 个月前
- 此快照最后确认于
- 2025/11/03 23:36 4 个月前
rt
CPP#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
typedef long long ll;
const ll N=1e5+5;
unordered_map<ll,ll> mp;
struct p{
ll l,r,h;
ll tag;
bool operator<(const p t)const {
return h<t.h;
}
};
struct node{
ll l,r,len,tag;
} tr[N<<5];
ll n,ans;
ll pos_x[N<<1],tot;
vector<p> line;
void build(int u,ll l,ll r){
tr[u].l=l,tr[u].r=r;
tr[u].len=tr[u].tag=0;
if (l==r) return;
int mid=(l+r)/2;
build(u<<1,l,mid);
build((u<<1)+1,mid+1,r);
}
void push_up(int u){
if (tr[u].tag) tr[u].len=mp[tr[u].r+1]-mp[tr[u].l];
else if (tr[u].l==tr[u].r) tr[u].len=0;
else tr[u].len=tr[u<<1].len+tr[(u<<1)+1].len;
}
void upd(int u,ll l,ll r,ll v){
if (l<=tr[u].l&&tr[u].r<=r){
tr[u].tag+=v;
push_up(u);
return;
}
if (tr[u].l==tr[u].r) return;
if (l<=tr[u<<1].r) upd(u<<1,l,r,v);
if (tr[(u<<1)+1].l<=r) upd((u<<1)+1,l,r,v);
push_up(u);
}
int main (){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1;i<=n;i++){
ll x,y,xx,yy;
cin >> x >> y >> xx >> yy;
line.push_back((p){x,xx,y,1});
line.push_back((p){x,xx,yy,-1});
}
for (auto i:line){
pos_x[++tot]=i.l;
pos_x[++tot]=i.r;
}
sort(pos_x+1,pos_x+tot+1);
tot=unique(pos_x+1,pos_x+tot+1)-pos_x-1;
for (int i=1;i<=tot;i++){
mp[i]=pos_x[i];
}
sort(line.begin(),line.end());
build(1,1,tot-1);
for (int i=0;i<line.size()-1;i++){
int l=lower_bound(pos_x+1,pos_x+tot+1,line[i].l)-pos_x;
int r=lower_bound(pos_x+1,pos_x+tot+1,line[i].r)-pos_x-1;
upd(1,l,r,line[i].tag);
ans+=tr[1].len*(line[i+1].h-line[i].h);
}
cout << ans << endl;
return 0;
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...