码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1542 【线段树+扫描线】

时间:2014-11-04 22:36:15      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   for   sp   

bubuko.com,布布扣
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define L(k) k<<1
#define R(k) k<<1|1
const int NO=205;
struct X
{
    int l,r;//左右短点对应的线段
    int s;//覆盖次数 全相同则不为-1  负责为-1
}p[NO<<2];
struct LINE
{
    double l,r;
    double h;
    int s;//覆盖次数
}line[NO<<1];
int n,m;
double x[NO<<1]={-1};
void creat(int k,int l,int r)
{
    p[k].l=l;
    p[k].r=r;
    p[k].s=0;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    creat(L(k),l,mid);
    creat(R(k),mid+1,r);
}
double search_(int k)
{
    if(p[k].s!=-1)
        return (x[p[k].r+1]-x[p[k].l])*(p[k].s!=0);
    return search_(L(k))+search_(R(k));
}
void update(int k,double l,double r,int s)
{
    if(p[k].s!=-1&&x[p[k].l]==l&&x[p[k].r+1]==r)
    {
        p[k].s+=s;
        return;
    }
    if(p[k].s!=-1)
        p[L(k)].s=p[R(k)].s=p[k].s;
    int mid=(p[k].l+p[k].r)>>1;
    if(r<=x[mid+1])
        update(L(k),l,r,s);
    else if(x[mid+1]<=l)
        update(R(k),l,r,s);
    else
    {
        update(L(k),l,x[mid+1],s);
        update(R(k),x[mid+1],r,s);
    }
    p[k].s=p[L(k)].s==p[R(k)].s?p[L(k)].s:-1;
}
bool H(const LINE &a,const LINE &b){return a.h<b.h;}
int main()
{
    freopen("1.txt","r",stdin);
    int cas=1;
    while(scanf("%d",&m),m)
    {
        for(int i=1;i<=m;i++)
        {
            scanf("%lf%lf%lf%lf",&x[i],&line[i].h,&x[m+i],&line[m+i].h);
            line[i].l=line[m+i].l=x[i];
            line[i].r=line[m+i].r=x[m+i];
            line[i].s=1;
            line[m+i].s=-1;
        }
        sort(x+1,x+1+2*m);
        sort(line+1,line+1+2*m,H);
        n=0;HD
        for(int i=1;i<=2*m;i++)
            if(x[i]!=x[i-1])
                x[++n]=x[i];
        creat(1,1,n-1);
        double ans=0;
        line[0].h=line[1].h;
        for(int i=1;i<=2*m;i++)
        {
            ans+=(line[i].h-line[i-1].h)*search_(1);
            update(1,line[i].l,line[i].r,line[i].s);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
    }
    return 0;
}
View Code

 

HDU1542 【线段树+扫描线】

标签:style   blog   http   io   color   ar   os   for   sp   

原文地址:http://www.cnblogs.com/Lnizei/p/4075009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!