标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2190 Accepted Submission(s): 669
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 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4627781.html