hdu 1255(线段树 扫描线) 覆盖的面积

http://acm.hdu.edu.cn/showproblem.php?pid=1255

典型线段树辅助扫描线,顾名思义扫描线就是相当于yy出一条直线从左到右(也可以从上到下)扫描过去,此时先将所有的横坐标和纵坐标排序

因为是从左到右扫描,那么横坐标应该离散化一下

当扫描线依次扫描的时候,依次扫描到的纵区间在线段树中查找,依据是上边还是下边记录,上边就是-1,下边就是+1,

如果某区间记录值为0的时候,代表没有被覆盖,为1的时候代表覆盖一次,为2代表覆盖两次(不会出现为负数的情况)

最后将依次扫描的覆盖两次的纵区间长度乘以以此的横坐标的和就行

所以http://acm.hdu.edu.cn/search.php?action=listproblem hdu1542 就在稍微改改这题的代码就行了

code

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cstring>
  4 using namespace std;
  5 struct node{
  6     double x,y1,y2;
  7     double flag;
  8 };
  9 node num[2001];
 10 double a[2001];
 11 bool cmp(node q,node w){return q.x<w.x;}
 12 struct point {
 13      int l,r,mark;
 14      double lf,rf;
 15      double once; //覆盖一次
 16      double twice;//覆盖两次
 17 };
 18 point tree[2001*4];
 19 void build(int i,int left,int right)
 20 {
 21     tree[i].l=left,tree[i].r=right;
 22     tree[i].once=tree[i].twice=0;
 23     tree[i].lf=a[left];
 24     tree[i].rf=a[right];
 25     if (left+1==right) return ;
 26     int mid=(left+right)/2;
 27     build(i*2,left,mid);
 28     build(i*2+1,mid,right);
 29 }
 30 void find(int i)
 31 {
 32     if (tree[i].mark>=2)
 33     {
 34         tree[i].twice=tree[i].once=tree[i].rf-tree[i].lf;
 35         return;
 36     }
 37     else if (tree[i].mark==1)
 38     {
 39         tree[i].once=tree[i].rf-tree[i].lf;
 40         if (tree[i].l+1==tree[i].r) tree[i].twice=0;
 41         else tree[i].twice=tree[i*2].once+tree[i*2+1].once;
 42     }
 43     else
 44     {
 45         if(tree[i].l+1==tree[i].r)
 46             tree[i].once=tree[i].twice=0;
 47         else
 48         {
 49             tree[i].once=tree[i*2].once+tree[i*2+1].once;
 50             tree[i].twice=tree[i*2].twice+tree[i*2+1].twice;
 51         }
 52     }
 53 }
 54 void update(int i,node g)
 55 {
 56     //printf("***%d***
",i);
 57     if (g.y1==tree[i].lf&&g.y2==tree[i].rf)
 58     {
 59         tree[i].mark+=g.flag;
 60         find(i); //更新区间
 61         return ;
 62     }
 63     if (g.y2<=tree[i*2].rf) update(i*2,g);
 64     else if (g.y1>=tree[i*2+1].lf) update(i*2+1,g);
 65     else
 66     {
 67         node temp=g;
 68         temp.y2=tree[i*2].rf;
 69         update(i*2,temp);
 70         temp=g;
 71         temp.y1=tree[i*2+1].lf;
 72         update(i*2+1,temp);
 73     }
 74     find(i);
 75 }
 76 int main()
 77 {
 78     int t,n,i,k;
 79     double x1,x2,y1,y2,ans;
 80     while (~scanf("%d",&k))
 81     {
 82         while (k--)
 83         {
 84             scanf("%d",&n);
 85             t=1;
 86             while (n--)
 87             {
 88                 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
 89                 num[t].x=x1,num[t].y1=y1,num[t].y2=y2;
 90                 num[t].flag=1;a[t]=y1;
 91                 t++;
 92                 num[t].x=x2,num[t].y1=y1,num[t].y2=y2;
 93                 num[t].flag=-1;a[t]=y2;
 94                 t++;
 95             }
 96             sort(num+1,num+t,cmp);
 97             sort(a+1,a+t);
 98             ans=0;
 99             build(1,1,t-1);
100             //printf("-1
");
101             update(1,num[1]);
102             for (i=2;i<t;i++)
103             {
104                 ans+=tree[1].twice*(num[i].x-num[i-1].x);
105                 update(1,num[i]);
106             }
107             printf("%.2lf
",ans);
108         }
109     }
110     return 0;
111 }
原文地址:https://www.cnblogs.com/JJCHEHEDA/p/4732691.html