面积并+扫描线 覆盖的面积 HDU

题目链接:https://cn.vjudge.net/problem/HDU-1255

题目大意:中文题目

具体思路:和上一篇的博客思路差不多,上一个题求的是面积,然后我们这个地方求的是啊覆盖两次及两次以上的面积,我们可以在原来的基础上进行改进,原来的tree1储存的是覆盖一次的合理的面积,我们再加一个tree2求得是覆盖两次及以上的面积,具体的判断过程:

1,如果lazy[rt]>1,就代表这块区域完全的被覆盖了两次,那么这块区域的面积就是hash【r+1】-hash【l】。

2,如果是根节点,覆盖两次的面积是0,我们判断是l+1==r。

3,如果是lazy[rt]==1的话,我们就取原来覆盖一次的面积上已经被覆盖的,这样就相当于求的是覆盖两次的面积。

4,其余的话,就是两个根节点覆盖两次的面积了。

AC代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <string>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <cmath>
  7 #include <vector>
  8 #include <queue>
  9 #include <stack>
 10 #include <ctime>
 11 #define ll long long
 12 # define lson l,m,rt<<1
 13 # define rson m+1,r,rt<<1|1
 14 using namespace std;
 15 const int maxn = 1000+100;
 16 # define inf 0x3f3f3f3f
 17 struct node
 18 {
 19     double l,r,h;
 20     int d;
 21     node() {}
 22     node(double xx,double yy,double zz,int tt)
 23     {
 24         l=xx;
 25         r=yy;
 26         h=zz;
 27         d=tt;
 28     }
 29     bool friend operator < (node t1,node t2)
 30     {
 31         return t1.h<t2.h;
 32     }
 33 } q[maxn<<2];
 34 double Hash[maxn<<2],tree1[maxn<<2],tree2[maxn<<2],lazy[maxn<<2];
 35 void up(int l,int r,int rt)
 36 {
 37     if(lazy[rt])
 38         tree1[rt]=Hash[r+1]-Hash[l];
 39     else if(l==r)
 40         tree1[rt]=0;
 41     else
 42         tree1[rt]=tree1[rt<<1]+tree1[rt<<1|1];
 43     if(lazy[rt]>1)
 44         tree2[rt]=Hash[r+1]-Hash[l];
 45     else if(l==r)
 46         tree2[rt]=0;
 47         else if(lazy[rt]==1)
 48             tree2[rt]=tree1[rt<<1]+tree1[rt<<1|1];
 49     else
 50         tree2[rt]=tree2[rt<<1]+tree2[rt<<1|1];
 51 }
 52 
 53 void update(int l,int r,int rt,int L,int R,int p)
 54 {
 55     if(L<=l&&R>=r)
 56     {
 57         lazy[rt]+=p;
 58         up(l,r,rt);
 59         return ;
 60     }
 61     int m=(l+r)>>1;
 62     if(L<=m)
 63         update(lson,L,R,p);
 64     if(R>m)
 65         update(rson,L,R,p);
 66     up(l,r,rt);
 67 }
 68 int main()
 69 {
 70     int T;
 71     scanf("%d",&T);
 72     while(T--)
 73     {
 74         int n,num=0;
 75         scanf("%d",&n);
 76         double x1,y1,x2,y2;
 77         for(int i=0; i<n; i++)
 78         {
 79              scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
 80             q[num]= {x1,x2,y1,1};
 81             Hash[num++]=x1;
 82             q[num]= {x1,x2,y2,-1};
 83             Hash[num++]=x2;
 84         }
 85         sort(Hash,Hash+num);
 86         sort(q,q+num);
 87         int k=1;
 88         for(int i=1; i<num; i++)
 89         {
 90             if(Hash[i]==Hash[i-1])
 91                 continue;
 92             Hash[k++]=Hash[i];
 93         }
 94         memset(tree1,0,sizeof(tree1));
 95         memset(tree2,0,sizeof(tree2));
 96         memset(lazy,0,sizeof(lazy));
 97         double ans=0;
 98     //    cout<<1<<endl;
 99         for(int i=0; i<num; i++)
100         {
101         //    cout<<i<<endl;
102             int l=lower_bound(Hash,Hash+k,q[i].l)-Hash;
103             int r=lower_bound(Hash,Hash+k,q[i].r)-Hash-1;
104             update(0,k-1,1,l,r,q[i].d);
105             ans+=tree2[1]*(q[i+1].h-q[i].h);
106         }
107         printf("%.2lf
",ans);
108     }
109     return 0;
110 }
原文地址:https://www.cnblogs.com/letlifestop/p/10335397.html