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

来源:互联网 发布:网络构架 编辑:程序博客网 时间:2024/05/17 20:32

传送门


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;}
原创粉丝点击