(codevs 3044 矩形面积求并)<可以不用线段树>

本文介绍了一种使用线段树解决区间覆盖问题的方法,并详细解释了如何通过更新线段树来实现对区间覆盖状态的维护。此外还分享了一些调试过程中遇到的问题及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门


Solution

听说这个题
n2 做法
n3 做法
竟然 n4 也能A???!!!
不管反正我考场写挂QAQ

虽然我对这个题还是理(wan)解(quan)很(bu)深(dong)的,但我还是要安利一下hzwer的博客,但是他的代码在codevs上好像wa了。。。雾
说一下细节:
线段树每个节点存的是这个点到下一个点的区间是否被覆盖
另外还有一个,这个题数据范围是100,理论上开200多的数组就可以,但事实上要600多才行。。。


Code

不要手残变量开成int

// by spli
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<ctime>
#include<queue>
#include<cstdlib>
#include<map>
#include<stack>
#include<cmath>
using namespace std;

const int N=310;
int n;
struct node{
    double x1,x2,h,f;
}p[N<<1];
double ha[N<<1];
int col[N<<1];
double s[N<<1];

bool cmp(node a,node b){
    return a.h<b.h;
}

void pushup(int pos,int L,int R){
    if(col[pos]) s[pos]=ha[R+1]-ha[L];
    else if(L==R) s[pos]=0;
    else s[pos]=s[pos<<1]+s[pos<<1|1];
}

void update(int pos,int L,int R,int ll,int rr,int f){
    if(ll>R||L>rr) return;
    if(ll<=L&&rr>=R){
        col[pos]+=f;
        pushup(pos,L,R);
        return;
    }
    int mid=(L+R)>>1;
    update(pos<<1,L,mid,ll,rr,f);
    update(pos<<1|1,mid+1,R,ll,rr,f);
    pushup(pos,L,R);
}

void work(){
    memset(s,0,sizeof(s));
    memset(ha,0,sizeof(ha));
    memset(col,0,sizeof(col));
    double x1,x2,y1,y2;
    for(int i=1;i<=n;++i){
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        p[i*2-1]=(node){x1,x2,y1,1};
        p[i*2]=(node){x1,x2,y2,-1};
        ha[i*2-1]=x1;
        ha[i*2]=x2;
    }
    sort(p+1,p+1+2*n,cmp);
    sort(ha+1,ha+1+2*n);
    int ll,rr;
    double ans=0;
    for(int i=1;i<=2*n;++i){
        ll=lower_bound(ha+1,ha+1+2*n,p[i].x1)-ha;
        rr=lower_bound(ha+1,ha+1+2*n,p[i].x2)-ha-1;
        if(ll<=rr) update(1,1,2*n,ll,rr,p[i].f);
        ans+=s[1]*(p[i+1].h-p[i].h);
    }
    printf("%.2lf\n",ans);
    return;
}

int main(){
    while(scanf("%d",&n)&&n) work();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值