zoukankan      html  css  js  c++  java
  • 【HDU】1255 覆盖的面积

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cmath>
      5 #include<algorithm>
      6 #define MAXN 5010
      7 #define EPS 1e-8
      8 using namespace std;
      9 struct Line
     10 {
     11     double left,right,high;
     12     int flag;
     13 };
     14 struct node
     15 {
     16     int cover;
     17     double one,more;
     18 };
     19 node tree[MAXN<<2];
     20 Line p[MAXN];
     21 double x[MAXN];
     22 int cmp1(const void *a,const void *b)
     23 {
     24     return (*(Line *)a).high>(*(Line *)b).high?1:-1;
     25 }
     26 int cmp2(const void *a,const void *b)
     27 {
     28     return *(double *)a>*(double *)b?1:-1;
     29 }
     30 inline int dbcmp(double s,double t)
     31 {
     32     if(fabs(s-t)<EPS)
     33         return 0;
     34     return s>t?1:-1;
     35 }
     36 int Bin(double val,int low,int high)
     37 {
     38     int mid,temp;
     39     while(low<high)
     40     {
     41         mid=(low+high)>>1;
     42         temp=dbcmp(x[mid],val);
     43         if(temp==0)
     44             return mid;
     45         if(temp>0)
     46             high=mid;
     47         else
     48             low=mid+1;
     49     }
     50 }
     51 inline void PushUp(int L,int R,int rt)
     52 {
     53     if(tree[rt].cover>1)
     54         tree[rt].one=tree[rt].more=x[R+1]-x[L];
     55     else if(tree[rt].cover==1)
     56     {
     57         tree[rt].one=x[R+1]-x[L];
     58         if(L==R)
     59             tree[rt].more=0;
     60         else
     61             tree[rt].more=tree[rt<<1].one+tree[rt<<1|1].one;
     62     }
     63     else
     64     {
     65         if(L==R)
     66             tree[rt].more=tree[rt].one=0;
     67         else
     68         {
     69             tree[rt].one=tree[rt<<1].one+tree[rt<<1|1].one;
     70             tree[rt].more=tree[rt<<1].more+tree[rt<<1|1].more;
     71         }
     72     }
     73 }
     74 void Update(int s,int t,int val,int L,int R,int rt)
     75 {
     76     if(s<=L&&R<=t)
     77     {
     78         tree[rt].cover+=val;
     79         PushUp(L,R,rt);
     80     }
     81     else
     82     {
     83         int mid=(L+R)>>1;
     84         if(mid>=s)
     85             Update(s,t,val,L,mid,rt<<1);
     86         if(t>mid)
     87             Update(s,t,val,mid+1,R,rt<<1|1);
     88         PushUp(L,R,rt);
     89     }
     90 }
     91 int main()
     92 {
     93     double ans,x1,y1,x2,y2;
     94     int n,i,c,cnt,m,s,t;
     95     scanf("%d",&c);
     96     while(c--)
     97     {
     98         scanf("%d",&n);
     99         for(i=cnt=0;i<n;i++)
    100         {
    101             scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
    102             if(dbcmp(x1,x2)==0||dbcmp(y1,y2)==0)
    103                 continue;
    104             if(x1>x2)
    105                 swap(x1,x2);
    106             if(y1>y2)
    107                 swap(y1,y2);
    108             p[cnt].flag=1;
    109             p[cnt].left=x1;
    110             p[cnt].right=x2;
    111             x[cnt]=x1;
    112             p[cnt++].high=y1;
    113             p[cnt].flag=-1;
    114             p[cnt].left=x1;
    115             p[cnt].right=x2;
    116             x[cnt]=x2;
    117             p[cnt++].high=y2;
    118         }
    119         qsort(p,cnt,sizeof(p[0]),cmp1);
    120         qsort(x,cnt,sizeof(x[0]),cmp2);
    121         for(i=m=0;i<cnt;i++)
    122         {
    123             if(dbcmp(x[m],x[i]))
    124                 x[++m]=x[i];
    125         }
    126         m++;
    127         memset(tree,0,sizeof(tree));
    128         for(ans=i=0;i<cnt-1;i++)
    129         {
    130             s=Bin(p[i].left,0,m);
    131             t=Bin(p[i].right,0,m);
    132             Update(s,t-1,p[i].flag,0,m-2,1);
    133             ans+=(p[i+1].high-p[i].high)*tree[1].more;
    134         }
    135         printf("%.2lf\n",ans);
    136     }
    137     return 0;
    138 }
    新博客:www.zhixiangli.com
  • 相关阅读:
    统计英文文章单词出现的频率
    验证码程序
    随机出题程序
    Toast提示和Menu菜单
    Failed to resolve:com.android.support:appcompat-v7第一次运行安卓程序报错
    Android stdio下载与安装教学
    简单的三级联动
    河北省科技创新平台涉众分析(交流讨论)
    项目目标文档
    系统利益相关者描述案例
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2543502.html
Copyright © 2011-2022 走看看