hdu1542 二维如何转化为线段树

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 struct LINE
 6 {
 7     double x,ydown,yup;
 8     int flag;
 9     bool operator<(const LINE &a) const
10     {
11         return x<a.x;
12     }
13 }line[1005];
14 int cover[1005],ff;
15 double prex[1005],downy[1005],upy[1005],xx,y1,y2,y[1005];
16 void build(int o,int l,int r)
17 {
18     prex[o]=0.0; cover[o]=0;
19     downy[o]=y[l]; upy[o]=y[r];
20     if (l+1==r) return;
21     int mid=l+(r-l)/2;
22     build(o*2,l,mid);
23     build(o*2+1,mid,r);
24 }
25 double update(int o,int l,int r)
26 {
27     int mid=l+(r-l)/2;
28     double temp;
29     if (l+1==r)
30     {
31         if (cover[o]>=1)
32         {
33             cover[o]+=ff; temp=prex[o]; prex[o]=xx;
34             return (upy[o]-downy[o])*(xx-temp);
35         }
36         else{cover[o]+=ff; prex[o]=xx; return 0.0;  }
37     }
38     if (y1>=upy[o*2]) return update(o*2+1,mid,r);
39     if (y2<=downy[o*2+1]) return update(o*2,l,mid);
40     return update(o*2,l,mid)+update(o*2+1,mid,r);
41 }
42 int main()
43 {
44     int t=0,num,i,n;
45     double x1,x2;
46     while (~scanf("%d",&n)&&n)
47     {
48         num=0;
49         for (i=1;i<=n;i++)
50         {
51             scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
52             num++;  y[num]=y1;
53             line[num].x=x1; line[num].ydown=y1; line[num].yup=y2; line[num].flag=1;
54             num++; y[num]=y2;
55             line[num].x=x2; line[num].ydown=y1; line[num].yup=y2; line[num].flag=-1;
56         }
57         sort(y+1,y+num+1);
58         sort(line+1,line+num+1);
59         build(1,1,num);
60         double ans=0.0;
61         for (i=1;i<=num;i++)
62         {
63             y1=line[i].ydown; y2=line[i].yup;
64             xx=line[i].x;  ff=line[i].flag;
65             ans+=update(1,1,num);
66         }
67         printf("Test case #%d\n",++t);
68         printf("Total explored area: %0.2lf\n\n",ans);
69     }
70 }

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

posted on 2014-07-27 01:48  xiao_xin  阅读(95)  评论(0编辑  收藏  举报

导航