矩形面积并模板,今年不会了,哭死

#include<iostream>//poj 1151
#include<cstdio>
#include<cmath>
#include<string.h>
#include<algorithm>
using namespace std;
//#define see(x) cout<<#x<<":"<<x<<endl;
#define e 3000
struct no{
        double x,stay,endy;
        int sta,end;
        int out;
        int cover;
        bool operator <(const no &a)const{
            return x < a.x;
            }
}num[e];
double yy[e];
struct node{
        int l,r;
        double sum;
        double len;
        int cover;
        int state;
}tree[e*4];
/************矩形面积并模板**************************/
void build(int nod,int l,int r)
{
        tree[nod].l=l;
        tree[nod].r=r;
        tree[nod].len=yy[r]-yy[l];
        tree[nod].sum=0;
        tree[nod].cover=0;
        if(r-l<=1) return ;
        int mid = (l+r)>>1;
        build(nod<<1,l,mid);
        build(nod<<1|1,mid,r);
}
void insert(int nod,int l,int r,int state)
{
        if(l<= tree[nod].l && tree[nod].r<=r)
        {

                tree[nod].cover+=state;
        }
        else
        {
            int mid=(tree[nod].r+ tree[nod].l)>>1;
            if(l< mid)
            insert(nod<<1,l,r,state);
            if(mid<r)
            insert(nod<<1|1,l,r,state);
        }
        if(tree[nod].cover>0)
        {
                tree[nod].sum=tree[nod].len;
        }
        else
        {
                if(tree[nod].r-tree[nod].l>1)
                    tree[nod].sum= tree[nod<<1].sum+tree[nod<<1|1].sum;
                else
                    tree[nod].sum=0;
        }
}
/************矩形面积并模板**************************/
int main()
{
    #ifndef ONLINE_JUDGE
     freopen("//home//mabodx//桌面//in.txt","r",stdin);
 #endif
    int n,m,i,j,k,t;
    k=0;
    while(scanf("%d", &n))
    {
        k++;
        memset(yy,0,sizeof(yy));
        memset(num,0,sizeof(num));
        memset(tree,0,sizeof(tree));
        if(n==0) break;
        if(k!=1)
            printf("\n");
        double x1,x2,y1,y2;
        int cnt=0;
        for(i=0; i<n ;i++)
        {
                scanf("%lf%lf%lf%lf", &x1,&y1,&x2,&y2);
                yy[++cnt]=y1;
                yy[++cnt]=y2;

                num[i*2].x=x1;
                num[i*2].stay=y1;
                num[i*2].endy=y2;
                num[i*2].out=0;

                num[i*2+1].x=x2;
                num[i*2+1].stay=y1;
                num[i*2+1].endy=y2;
                num[i*2+1].out=1;
        }


        sort(yy+1,yy+cnt+1);
        sort(num,num+(2*n));


        int cn=0;
        for(i=2; i<=cnt;i++)
        {
            if(yy[i]!= yy[i-1])
                yy[++cn]=yy[i-1];
        }
        yy[++cn]=yy[cnt];



        for(i=0; i<=2*n-1; i++)
        {
            num[i].sta=lower_bound(yy+1,yy+cn+1,num[i].stay)-yy;
            num[i].end=lower_bound(yy+1,yy+cn+1,num[i].endy)-yy;
        }
        build(1,1,cn);
        double area=0;
        /************矩形面积并模板**************************/
        for(i=0; i<2*n-1; i++)
        {
            if(num[i].out==0)
                insert(1,num[i].sta,num[i].end,1);
            else
                insert(1,num[i].sta,num[i].end,-1);
            area+=(tree[1].sum* (num[i+1].x- num[i].x) );
        }
        /************矩形面积并模板**************************/
        printf("Test case #%d\n",k);
        printf("Total explored area: %.2lf\n",area);
    }
}

你可能感兴趣的:(矩形面积并模板,今年不会了,哭死)