您的位置:首页 > 其它

HDU-1255-覆盖的面积-线段树求面积并(模板)

2015-08-03 13:49 453 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255

好吧,这题和HDU1542几乎完全一样,链接:/article/2046926.html我有详细讲;

这个题目唯一的不同就是这是个求重合的面积,而HDU1542是求并面积,如果明白了1542题目的原理,我想很轻松的就可以搞定这个题目,只要将cover>0改成cover>1就可以求出覆盖面积了,我这里就不在赘述了;代码我再贴一下

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<cmath>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
#define inf 1<<30
using namespace std;
const int N=2005;
int n;
double y
;
struct LINE
{
    double x;
    double y_down,y_up;
    int flag;
}line
;
struct node
{
    double l,r;
    double x;
    int cover;
    bool flag;
}node[N<<2];
bool cmp(LINE a,LINE b)
{
    return a.x<b.x;
}
void build(int rt,int l,int r)
{
    node[rt].l=y[l];
    node[rt].r=y[r];
    node[rt].x=-1;
    node[rt].flag=false;
    node[rt].cover=0;
    if(l+1==r){
        node[rt].flag=true;
        return;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid,r);
}
double Insert_query(int rt,double x,double l,double r,int flag)
{
    if(l>=node[rt].r||r<=node[rt].l) return 0;
    if(node[rt].flag){
        if(node[rt].cover>1){
            double pre=node[rt].x;
            double ans=(x-pre)*(node[rt].r-node[rt].l);
            node[rt].x=x;
            node[rt].cover+=flag;
            return ans;
        }else{
            node[rt].x=x;
            node[rt].cover+=flag;
            return 0;
        }
    }
    double ans1,ans2;
    ans1=Insert_query(rt<<1,x,l,r,flag);
    ans2=Insert_query(rt<<1|1,x,l,r,flag);
    return ans1+ans2;
}
int main()
{
    int t;
    double x1,x2,y1,y2;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int cnt=-1;
        for(int i=0;i<n;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            y[++cnt]=y1;
            line[cnt].x=x1;
            line[cnt].y_down=y1;
            line[cnt].y_up=y2;
            line[cnt].flag=1;
            y[++cnt]=y2;
            line[cnt].x=x2;
            line[cnt].y_down=y1;
            line[cnt].y_up=y2;
            line[cnt].flag=-1;
        }
        sort(y,y+cnt+1);
        sort(line,line+cnt+1,cmp);
        build(1,0,cnt);
        double area=0;
        for(int i=0;i<=cnt;i++){
            area+=Insert_query(1,line[i].x,line[i].y_down,line[i].y_up,line[i].flag);
        }
        printf("%.2lf\n",area);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: