社区讨论
18pts求diao
P5490【模板】扫描线 & 矩形面积并参与者 1已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mhjo59vr
- 此快照首次捕获于
- 2025/11/04 05:45 4 个月前
- 此快照最后确认于
- 2025/11/04 05:45 4 个月前
CPP
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5, M=N<<3;
ll n,x,x2,y,y2,cnt=0,X[N<<1];
struct Side{
ll l,r,q,n;
}s[N<<1];
struct Node{
ll l,r,sum,len;
}t[M];
bool cmp(Side a,Side b){
return a.n<=b.n;
}
inline int ls(int p){return p<<1;}
inline int rs(int p){return p<<1|1;}
void push_up(int p){//Change the length of the part of tree
if(t[p].sum)
t[p].len=X[t[p].r+1]-X[t[p].l];
else
t[p].len=t[ls(p)].len+t[rs(p)].len;
}
void build(int p,int l,int r){//Easy to build a tree
t[p]={l,r,0,0};
if(l==r)
return;
int mid=(l+r)>>1;
build(ls(p),l,mid);
build(rs(p),mid+1,r);
}
void query(int p,int l,int r,int q){
if(X[t[p].r+1]<=l||X[t[p].l]>=r)//The limit mods aren't in the part of tree
return;
if(l<=X[t[p].l]&&r>=X[t[p].r+1]){//If the limit mods are in the part of tree
t[p].sum+=q;//Change the sum if the mods are covered
push_up(p);//Then change the length of the part of tree
return;//As long as get the length,stop the step
}
query(ls(p),l,r,q);
query(rs(p),l,r,q);
push_up(p);
}
int main(){
cin>>n;
for(int i=1;i<=n;++i){
cin>>x>>y>>x2>>y2;
s[++cnt].l=x;
s[cnt].r=x2;
s[cnt].n=y;
s[cnt].q=1;
X[cnt]=x;
s[++cnt].l=x;
s[cnt].r=x2;
s[cnt].n=y2;
s[cnt].q=-1;
X[cnt]=x2;
}
sort(s+1,s+1+cnt,cmp);
sort(X+1,X+1+cnt);
unique(X+1,X+1+cnt);
build(1,1,cnt-1);
ll ans=0;
for(int i=1;i<cnt;++i){
query(1,s[i].l,s[i].r,s[i].q);
ans+=t[1].len*(s[i+1].n-s[i].n);
}
cout<<ans;
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...