Get The Treasury

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2190    Accepted Submission(s): 669


Problem Description
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.

 

 

Input
The first line of the input file contains a single integer t, the number of test cases, followed by the input data for each test case.
Each test case is given in some lines. In the first line there is an integer n (1 ≤ n ≤ 1000), the number of spots on the surface of the earth that he had detected. Then n lines follow, every line contains six integers x1, y1, z1, x2, y2 and z2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 106, and that of z coordinate is no more than 500.

 

 

Output
For each test case, you should output “Case a: b” in a single line. a is the case number, and b is the minimum volume of treasury. The case number is counted from one.
 

 

Sample Input
2
1
0 0 0 5 6 4
3
0 0 0 5 5 5
3 3 3 9 10 11
3 3 3 13 20 45
 

 

Sample Output
Case 1: 0
Case 2: 8
 

 

Source
 
 
题目意思:
给n个立方体,求n个立方体相交三次或三次以上的部分的体积。
 
思路:
扫描线3维的应用,把z离散化,那么每一块z[i]--z[i+1]中相交三次或三次以上的体积就是相交三次或三次以上的面积*(z[i+1]-z[i])。
求出相交三次或三次以上的面积即可。
 
代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 #include <set>
  9 using namespace std;
 10 
 11 #define N 1005
 12 #define ll root<<1
 13 #define rr root<<1|1
 14 #define mid (a[root].l+a[root].r)/2
 15 
 16 int max(int x,int y){return x>y?x:y;}
 17 int min(int x,int y){return x<y?x:y;}
 18 int abs(int x,int y){return x<0?-x:x;}
 19 
 20 
 21 struct node{
 22     int l, r;
 23     int one, two, more;
 24     int val;
 25 }a[N*8];
 26 
 27 struct Line{
 28     int x1, x2, y, val;
 29     Line(){}
 30     Line(int a,int b,int c,int d){
 31         x1=a;
 32         x2=b;
 33         y=c;
 34         val=d;
 35     }
 36 }line[N*2];
 37 
 38 bool cmp(Line a,Line b){
 39     return a.y<b.y;
 40 }
 41 
 42 struct spot{
 43     int x1, y1, z1, x2, y2, z2;
 44 }s[N];
 45 
 46 int n, nx, nz;
 47 int xx[N*2];
 48 int zz[N*2];
 49 
 50 int b_s(int key){
 51     int l=0, r=nx-1;
 52     while(l<=r){
 53         int mm=(l+r)/2;
 54         if(xx[mm]==key) return mm;
 55         else if(xx[mm]>key) r=mm-1;
 56         else if(xx[mm]<key) l=mm+1;
 57     }
 58 }
 59 
 60 void build(int l,int r,int root){
 61     a[root].l=l;
 62     a[root].r=r;
 63     a[root].one=a[root].two=a[root].more=a[root].val=0;
 64     if(l==r) return;
 65     build(l,mid,ll);
 66     build(mid+1,r,rr);
 67 }
 68 
 69 void up(int root){
 70     if(a[root].val>2) a[root].one=a[root].two=a[root].more=xx[a[root].r+1]-xx[a[root].l];
 71     else if(a[root].val==2){
 72         a[root].one=a[root].two=xx[a[root].r+1]-xx[a[root].l];
 73         if(a[root].l==a[root].r) a[root].more=0;
 74         else a[root].more=a[ll].one+a[rr].one;
 75     }
 76     else if(a[root].val==1){
 77         a[root].one=xx[a[root].r+1]-xx[a[root].l];
 78         if(a[root].l==a[root].r) a[root].two=a[root].more=0;
 79         else{
 80             a[root].two=a[ll].one+a[rr].one;
 81             a[root].more=a[ll].two+a[rr].two;
 82         }
 83     }
 84     else{
 85         if(a[root].l==a[root].r) a[root].one=a[root].two=a[root].more=0;
 86         else{
 87             a[root].one=a[ll].one+a[rr].one;
 88             a[root].two=a[ll].two+a[rr].two;
 89             a[root].more=a[ll].more+a[rr].more;
 90         }
 91     }
 92 }
 93 
 94 void update(int l,int r,int val,int root){
 95     if(a[root].l==l&&a[root].r==r){
 96         a[root].val+=val;
 97         up(root);
 98         return;
 99     }
100     if(r<=a[ll].r) update(l,r,val,ll);
101     else if(l>=a[rr].l) update(l,r,val,rr);
102     else{
103         update(l,mid,val,ll);
104         update(mid+1,r,val,rr);
105     }
106     up(root);
107 }
108 
109 main()
110 {
111     int t, i, j, k;
112     int kase=1;
113     cin>>t;
114     while(t--){
115         scanf("%d",&n);
116         nx=nz=0;
117         for(i=0;i<n;i++){
118             scanf("%d %d %d %d %d %d",&s[i].x1,&s[i].y1,&s[i].z1,&s[i].x2,&s[i].y2,&s[i].z2);
119             xx[nx++]=s[i].x1;
120             xx[nx++]=s[i].x2;
121             zz[nz++]=s[i].z1;
122             zz[nz++]=s[i].z2;
123         }
124         sort(xx,xx+nx);
125         sort(zz,zz+nz);
126         nx=unique(xx,xx+nx)-xx;
127         nz=unique(zz,zz+nz)-zz;
128         __int64 ans=0;
129         for(i=1;i<nz;i++){
130             k=0;
131             for(j=0;j<n;j++){
132                 if(s[j].z1<=zz[i-1]&&s[j].z2>=zz[i]){
133                     line[k++]=Line(s[j].x1,s[j].x2,s[j].y1,1);
134                     line[k++]=Line(s[j].x1,s[j].x2,s[j].y2,-1);
135                 }
136             }
137             sort(line,line+k,cmp);
138             build(0,nx,1);
139             __int64 num=0;
140             for(j=0;j<k-1;j++){
141                 update(b_s(line[j].x1),b_s(line[j].x2)-1,line[j].val,1);
142                 num+=(__int64)a[1].more*(__int64)(line[j+1].y-line[j].y);
143             }
144             ans+=num*(__int64)(zz[i]-zz[i-1]);
145         }
146         printf("Case %d: %I64d\n",kase++,ans);
147     }
148 }

 

posted on 2015-07-07 18:05  MC幽夜  阅读(329)  评论(0编辑  收藏  举报