标签:des style blog http java color 使用 os
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3571 Accepted Submission(s): 1753
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 #define N 2010 7 #define ll root<<1 8 #define rr root<<1|1 9 #define mid (a[root].l+a[root].r)/2 10 11 double x[N*2]; 12 13 struct Line{ 14 double l, r, h; 15 int v; 16 Line(double a=0,double b=0,double c=0,int d=0):l(a),r(b),h(c),v(d){} 17 }line[N*4]; 18 19 struct node{ 20 int l, r; 21 double sum1, sum2; 22 int v; 23 }a[N*4]; 24 25 bool cmp(Line a,Line b){ 26 return a.h<b.h; 27 } 28 29 void build(int l,int r,int root){ 30 a[root].l=l; 31 a[root].r=r; 32 a[root].v=0; 33 a[root].sum1=a[root].sum2=0.0; 34 if(l==r) return; 35 build(l,mid,ll); 36 build(mid+1,r,rr); 37 } 38 39 int find(double key,int k){ 40 int l=0, r=k; 41 while(l<=r){ 42 int mm=(l+r)/2; 43 if(x[mm]==key) return mm; 44 else if(x[mm]<key){ 45 l=mm+1; 46 } 47 else{ 48 r=mm-1; 49 } 50 } 51 } 52 53 void up(int root){ 54 if(a[root].v>1){ 55 a[root].sum1=0.0; 56 a[root].sum2=x[a[root].r+1]-x[a[root].l]; 57 } 58 else if(a[root].v==1){ 59 if(a[root].l==a[root].r) { 60 a[root].sum1=x[a[root].r+1]-x[a[root].l]; 61 a[root].sum2=0.0; 62 } 63 else{ 64 a[root].sum2=a[ll].sum1+a[ll].sum2+a[rr].sum1+a[rr].sum2; 65 a[root].sum1=x[a[root].r+1]-x[a[root].l]-a[root].sum2; 66 } 67 } 68 else{ 69 if(a[root].l==a[root].r){ 70 a[root].sum1=a[root].sum2=0.0; 71 } 72 else{ 73 a[root].sum1=a[ll].sum1+a[rr].sum1; 74 a[root].sum2=a[ll].sum2+a[rr].sum2; 75 } 76 } 77 } 78 void update(int l,int r,int v,int root){ 79 if(a[root].l>=l&&a[root].r<=r){ 80 a[root].v+=v; 81 up(root); 82 return; 83 } 84 if(l<=mid) update(l,r,v,ll); 85 if(r>mid) update(l,r,v,rr); 86 up(root); 87 } 88 89 main() 90 { 91 int t; 92 double x1, y1, x2, y2; 93 int n, m; 94 int i, j, k; 95 cin>>t; 96 while(t--){ 97 scanf("%d",&n); 98 m=0; 99 for(i=0;i<n;i++){ 100 scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); 101 x[m]=x1; 102 line[m++]=Line(x1,x2,y1,1); 103 x[m]=x2; 104 line[m++]=Line(x1,x2,y2,-1); 105 } 106 107 sort(x,x+m); 108 sort(line,line+m,cmp); 109 k=unique(x,x+m)-x; 110 build(0,k-1,1); 111 double ans=0.0; 112 for(i=0;i<m-1;i++){ 113 int l=find(line[i].l,k-1); 114 int r=find(line[i].r,k-1); 115 update(l,r-1,line[i].v,1); 116 // printf("%.2lf\n",a[1].sum2); 117 ans+=(line[i+1].h-line[i].h)*a[1].sum2; 118 } 119 printf("%.2lf\n",ans); 120 } 121 }
HDU 1255 离散化+扫描线覆盖的面积,布布扣,bubuko.com
标签:des style blog http java color 使用 os
原文地址:http://www.cnblogs.com/qq1012662902/p/3875789.html