Jack knows that there is a great underground treasury in a secret region. And he has a special device that can be used to detect treasury under the surface of the earth. One day he got outside with the device to ascertain the treasury. He chose many different locations on the surface of the earth near the secret region. And at each spot he used the device to detect treasury and got some data from it representing a region, which may contain treasury below the surface. The data from the device at each spot is six integers x1, y1, z1, x2, y2 and z2 (x1<x2, y1<y2, z1<z2). According to the instruction of the device they represent the range of x, y and z coordinates of the region. That is to say, the x coordinate of the region, which may contain treasury, ranges from x1 to x2. So do y and z coordinates. The origin of the coordinates is a fixed point under the ground.
Jack can’t get the total volume of the treasury because these regions don’t always contain treasury. Through years of experience, he discovers that if a region is detected that may have treasury at more than two different spots, the region really exist treasure. And now Jack only wants to know the minimum volume of the treasury.
Now Jack entrusts the problem to you.

题意:问矩形三次以上覆盖的面积

线段树--扫描线裸题,统计1次、2次、3次含以上的长度。

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include<math.h>
  5 using namespace std;
  6 const int maxm=2010;
  7 typedef long long ll;
  8 
  9 ll st1[maxm<<2],st2[maxm<<2],st3[maxm<<2];
 10 int cov[maxm<<2],y[maxm],z[maxm];
 11 
 12 struct rect{
 13     int x1,y1,z1,x2,y2,z2;
 14 }r[1005];
 15 
 16 struct seg{
 17     int x,y1,y2,c;
 18     bool operator < (const seg a)const{
 19         return x<a.x;
 20     }
 21 }s[maxm];
 22 
 23 void pushup(int o,int l,int r){
 24     if(cov[o]>=3)st1[o]=st2[o]=st3[o]=y[r]-y[l];
 25     else if(cov[o]==2){
 26         st1[o]=st2[o]=y[r]-y[l];
 27         if(l+1==r)st3[o]=0;
 28         else st3[o]=st1[o<<1]+st1[o<<1|1];
 29     }
 30     else if(cov[o]==1){
 31         st1[o]=y[r]-y[l];
 32         if(l+1==r)st2[o]=st3[o]=0;
 33         else{
 34             st2[o]=st1[o<<1]+st1[o<<1|1];
 35             st3[o]=st2[o<<1]+st2[o<<1|1];
 36         }
 37     }
 38     else{
 39         if(l+1==r)st1[o]=st2[o]=st3[o]=0;
 40         else{
 41             st1[o]=st1[o<<1]+st1[o<<1|1];
 42             st2[o]=st2[o<<1]+st2[o<<1|1];
 43             st3[o]=st3[o<<1]+st3[o<<1|1];
 44         }
 45     }
 46 }
 47 
 48 void update(int o,int l,int r,seg a){
 49     if(a.y1<=y[l]&&a.y2>=y[r]){
 50         cov[o]+=a.c;
 51         pushup(o,l,r);
 52         return;
 53     }
 54     if(l+1==r)return;
 55     int m=l+((r-l)>>1);
 56     if(a.y1<y[m])update(o<<1,l,m,a);
 57     if(a.y2>y[m])update(o<<1|1,m,r,a);
 58     pushup(o,l,r);
 59 }
 60 
 61 int main(){
 62     int T;
 63     scanf("%d",&T);
 64     for(int q=1;q<=T;++q){
 65         int n;
 66         scanf("%d",&n);
 67         for(int i=1;i<=n;++i){
 68             scanf("%d%d%d%d%d%d",&r[i].x1,&r[i].y1,&r[i].z1,&r[i].x2,&r[i].y2,&r[i].z2);
 69             y[2*i-1]=r[i].y1;
 70             y[2*i]=r[i].y2;
 71             z[2*i-1]=r[i].z1;
 72             z[2*i]=r[i].z2;
 73         }
 74         sort(y+1,y+2*n+1);
 75         sort(z+1,z+2*n+1);
 76         int cntz=1,cnty=1;
 77         ll ans=0;
 78         for(int i=2;i<=2*n;++i){
 79             if(y[i]!=y[i-1])y[++cnty]=y[i];
 80             if(z[i]!=z[i-1])z[++cntz]=z[i];
 81         }
 82         for(int i=1;i<cntz;++i){
 83             int cnt=0;
 84             for(int j=1;j<=n;++j){
 85                 if(r[j].z1<=z[i]&&r[j].z2>z[i]){
 86                     ++cnt;
 87                     s[cnt].x=r[j].x1;s[cnt].y1=r[j].y1;s[cnt].y2=r[j].y2;
 88                     s[cnt].c=1;
 89                     ++cnt;
 90                     s[cnt].x=r[j].x2;s[cnt].y1=r[j].y1;s[cnt].y2=r[j].y2;
 91                     s[cnt].c=-1;
 92                 }
 93             }
 94             sort(s+1,s+cnt+1);
 95             ll sum=0;
 96             for(int j=1;j<cnt;++j){
 97                 update(1,1,cnty,s[j]);
 98                 sum+=st3[1]*(s[j+1].x-s[j].x);
 99             }
100             update(1,1,cnty,s[cnt]);
101             ans+=sum*(z[i+1]-z[i]);
102         }
103         printf("Case %d: %lld\n",q,ans);
104     }
105     return 0;
106 }
View Code