矩形周长并 poj 1177Picture/hdu 1828

本文探讨了如何通过代码实现解决复杂问题,并优化数据结构提高效率。具体包括使用线段树进行快速查询与更新,以及如何正确处理边界条件避免常见bug。

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

同上篇一样,具体的去看论文吧

我没看过论文,不知道正解,但是我的代码能够A提,这就足够了

分两个方向分别扫一边

bug:

1.先处理入边,在处理出边

2.线段树query函数注意一下(代码种有标记)


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define mid (L+R>>1)
#define lson L,mid,P<<1
#define rson mid,R,P<<1|1
const int M = 10010;
class segtree{
public:
    int left[M<<2],right[M<<2];
    int sum[M<<2],num[M<<2],s[M],n;
    
    void init(int ss[],int k){
        n=0;
        for(int i=0;i<k;i++){
            if(i==0||s[n]!=ss[i]){
                s[++n]=ss[i];
            }
        }
        __build(1,n,1);
    }
    void __build(int L,int R,int P){
        left[P]=s[L],right[P]=s[R];
        num[P]=0,sum[P]=0;
        if(L+1==R)return ;
        __build(lson);
        __build(rson);
    }
    void pushup(int x,int L,int R){
        if(num[x]){
            sum[x]=right[x]-left[x];
        }else{
            if(L+1!=R){
                sum[x]=sum[x<<1]+sum[x<<1|1];
            }else{
                sum[x]=0;
            }
        }
    }
    void __update(int L,int R,int P,int l,int r,int f){
        if(l==left[P]&&r==right[P]){
            num[P]+=f;
            pushup(P,L,R);
            return ;
        }
        if(l<right[P<<1]){
            __update(lson,l,min(right[P<<1],r),f);
        }
        if(r>right[P<<1]){
            __update(rson,max(l,right[P<<1]),r,f);
        }
        pushup(P,L,R);
    }
    int __query(int L,int R,int P,int l,int r){
        if(sum[P]==right[P]-left[P]){//注意细节
            return r-l;
        }
        if(l==left[P]&&r==right[P]){
            return sum[P];
        }
        int ret=0;
        if(l<right[P<<1]){
            ret+=__query(lson,l,min(r,right[P<<1]));
        }
        if(r>right[P<<1]){
            ret+=__query(rson,max(l,right[P<<1]),r);
        }
        return ret;
    }
    void update(int l,int r,int f){__update(1,n,1,l,r,f);}
    int query(int l,int r){return __query(1,n,1,l,r);}
};
struct Line{
    int x,y1,y2,f;
    void set(int _x,int _y1,int _y2,int _f){
        x=_x,y1=_y1,y2=_y2,f=_f;
    }
}ll[M<<1],rr[M<<1];
int xx[M<<1],yy[M<<1],n;


bool cmp(Line a,Line b){
    if(a.x!=b.x){
        return a.x<b.x;
    }else{
        return a.f>b.f;
    }
}
int main(){
    while(cin>>n){
        segtree *tree;
        tree = new segtree;
        for(int i=0,x1,y1,x2,y2;i<n;i++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            ll[i<<1].set(x1,y1,y2,1);
            ll[i<<1|1].set(x2,y1,y2,-1);
            rr[i<<1].set(y1,x1,x2,1);
            rr[i<<1|1].set(y2,x1,x2,-1);
            yy[i<<1]=y1,yy[i<<1|1]=y2;
            xx[i<<1]=x1,xx[i<<1|1]=x2;
        }
        
        sort(ll,ll+n*2,cmp);
        sort(yy,yy+n*2);
        tree->init(yy,n*2);
        int sum=ll[0].y2-ll[0].y1;
        tree->update(ll[0].y1,ll[0].y2,ll[0].f);
        for(int i=1;i<n*2;i++){
            if(ll[i].f==1){
                sum+=(ll[i].y2-ll[i].y1-tree->query(ll[i].y1,ll[i].y2));
                tree->update(ll[i].y1,ll[i].y2,ll[i].f);
            }else{
                tree->update(ll[i].y1,ll[i].y2,ll[i].f);
                sum+=(ll[i].y2-ll[i].y1-tree->query(ll[i].y1,ll[i].y2));
            }
        }
        delete tree;
        tree = new segtree;


        sort(rr,rr+n*2,cmp);
        sort(xx,xx+n*2);
        tree->init(xx,n*2);
        sum+=rr[0].y2-rr[0].y1;
        tree->update(rr[0].y1,rr[0].y2,rr[0].f);
        for(int i=1;i<n*2;i++){
            if(rr[i].f==1){
                sum+=(rr[i].y2-rr[i].y1-tree->query(rr[i].y1,rr[i].y2));
                tree->update(rr[i].y1,rr[i].y2,rr[i].f);
            }else{
                tree->update(rr[i].y1,rr[i].y2,rr[i].f);
                sum+=(rr[i].y2-rr[i].y1-tree->query(rr[i].y1,rr[i].y2));
            }
        }
        cout<<sum<<endl;
        delete tree;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值