Bzoj 3225: [Sdoi2008] Cube Overlay

Source: Internet
Author: User

"Original question"

3225: [Sdoi2008] cube cover time limit: 2 Sec Memory Limit: MB
Submit: wuyi Solved: 36
[Submit] [Status] DescriptionA June recently for the preparation of the provincial team selection. Special training of data structure was specially carried out. The classical problem of "rectangular area and" is encountered in the course of training. That is: give the n each side and the axis parallel (vertical) rectangle, to find the sum of the area covered by the rectangle.

A June according to the vertical axis to establish a line tree after the horizontal scan calculation. This problem is easily ac, and the time complexity is O (NLOGN). for intensive training. A June will extend the problem to three-dimensional space, that is: to give the N each edge and axis parallel (vertical) cube, to find the sum of the volume of the cube cover. To simplify the problem, the cube is degraded to a positive cube, with a four-tuple (x, Y, Z, R) representing a cube, where x, Y, and Z are the center point coordinates of the cube. R is the distance from the center point to each polygon of the cube (that is, half the height of the cube). This time can be stumped a June. Just ask you--the future gold medal--to help him. the first line of Input is a positive integer n. the following n lines are four integers per line x, Y, Z, R, separated by a space.

Output is a total of one number, which is the overall product of the overlay.

Sample Input3
0 0 0 3
1–1 0 1
19 3 5 6

Sample Output1944

HINT

For 100% of data. 1≤n≤100

For 100% of data, -1000≤x, Y, z≤1000. 1≤r≤200


"Procedure 1" n=100? Does violence ever work? Violence is discrete and statistical. The limit efficiency is probably: 200^3*100. and to be able to improve a little more efficiency.

"Code 1"

#include <cstdio> #include <algorithm> #define N 105#define M 210using namespace Std;int n,x,y,z,r,hx,hy,hz,  Tx,ty,tz,i,j,k,p,flag,ans;int X1[n],x2[n],y1[n],y2[n],z1[n],z2[n],xx[m],yy[m],zz[m],x[m],y[m],z[m];int Main () {  scanf ("%d", &n);    for (i=1;i<=n;i++) {scanf ("%d%d%d%d", &x,&y,&z,&r);    X1[i]=x-r;x2[i]=x+r;    Y1[i]=y-r;y2[i]=y+r;  Z1[i]=z-r;z2[i]=z+r;    } for (i=1;i<=n;i++) {xx[++hx]=x1[i];xx[++hx]=x2[i];    Yy[++hy]=y1[i];yy[++hy]=y2[i];  Zz[++hz]=z1[i];zz[++hz]=z2[i];  } sort (xx+1,xx+hx+1); xx[0]=xx[1]-1;  Sort (yy+1,yy+hy+1); yy[0]=yy[1]-1;  Sort (zz+1,zz+hz+1); zz[0]=zz[1]-1;  for (i=1;i<=hx;i++) if (xx[i]!=xx[i-1]) x[++tx]=xx[i];  for (i=1;i<=hy;i++) if (yy[i]!=yy[i-1]) y[++ty]=yy[i];  for (i=1;i<=hz;i++) if (zz[i]!=zz[i-1]) z[++tz]=zz[i];        for (i=1;i<tx;i++) for (j=1;j<ty;j++) for (k=1;k<tz;k++) {flag=1; for (p=1;p<=n&&flag;p++) if (x1[p]<=x[i]&&x[i+1]<=X2[P]&AMP;&AMP;Y1[P]&LT;=Y[J]&AMP;&AMP;Y[J+1]&LT;=Y2[P]&AMP;&AMP;Z1[P]&LT;=Z[K]&AMP;&AMP;Z[K+1]&LT;=Z2[P]) flag        = 0;      if (!flag) ans+= (X[i+1]-x[i]) * (Y[j+1]-y[j]) * (Z[k+1]-z[k]);  } printf ("%d", ans); return 0;}

"Analysis 2" then we'll just write the mark. Obviously. Direct Line segment tree set line The tree is a bit empty.

So I first enumerate the discrete h, and once again build up the x and y that satisfy the condition. This translates into a rectangular area of the classic Nlog (N).

The idea of using scan lines. Take each vertical edge out and press X from left to right. and then scan. Suppose an edge is the beginning edge, adding 1 to the Y1--y2 in the line segment tree . Otherwise the y1--y2 minus 1. Then for each different x, the number of overlay layers in the segment tree is greater than 1 * (X[i+1]-x[i]).

There is a lot of detail in this segment tree. For example, the line segment tree is built l~mid,mid~r. Then the leaf node is l~l+1.

"Code 2"

#include <cstdio> #include <algorithm> #define N 105#define A 1200#define M 210using namespace Std;struct tree{ int l,r,sum,cover;}  a[m*2*16];struct arr{int x,l,r,p;    friend BOOL operator < (const arr &a,const arr &b) {if (a.x!=b.x) return a.x<b.x;  Return!a.p&&b.p; }}e[m*2];int N,x,y,z,r,hz,tz,l,r,i,j,k,opt,h,hy,ty,p,tot,flag,temp,h,w,ans;int x1[N],x2[N],y1[N],y2[N],z1[N],z2[  n],zz[m],z[m],yy[m],y[m],pre[2405];void Build (int k,int l,int r) {a[k].l=l;a[k].r=r;a[k].sum=0;a[k].cover=0; if (l+1==r| |  L==R) Return;int mid= (l+r) >>1;  Build (K<<1,l,mid); Build (K<<1|1,mid,r);}     void Cover_add (int k) {if (l<=a[k].l&&a[k].r<=r) {if (!opt) A[K].COVER++,A[K].SUM=Y[A[K].R]-Y[A[K].L]; else if (! ( --a[k].cover)) a[k].sum= (A[K].L+1&GT;=A[K].R)?

0:a[k<<1].sum+a[(k<<1) +1].sum; Return } int mid= (A[K].L+A[K].R)/2; if (l<mid) Cover_add (k<<1); if (r>mid) Cover_add (k<<1|1); if (!a[k].cover) a[k].sum=a[k<<1].sum+a[k<<1|1].sum;} int main () {scanf ("%d", &n); for (i=1;i<=n;i++) {scanf ("%d%d%d%d", &x,&y,&z,&r); X1[i]=x-r;x2[i]=x+r; Y1[i]=y-r;y2[i]=y+r; Z1[i]=z-r;z2[i]=z+r; } for (i=1;i<=n;i++) zz[++hz]=z1[i],zz[++hz]=z2[i]; Sort (zz+1,zz+hz+1); zz[0]=zz[1]-1; for (i=1;i<=hz;i++) if (zz[i]!=zz[i-1]) z[++tz]=zz[i]; for (k=1;k<tz;k++) {h=z[k+1]-z[k];h=hy=ty=0;temp=tot=0; for (i=1;i<=n;i++) if (Z1[i]<=z[k]&&z[k+1]<=z2[i]) {e[++h]= (arr) {x1[i],y1[i],y2[i],0}; E[++h]= (arr) {x2[i],y1[i],y2[i],1}; Yy[++hy]=y1[i];yy[++hy]=y2[i]; } sort (yy+1,yy+hy+1); yy[0]=yy[1]-1; for (i=1;i<=hy;i++) if (yy[i]!=yy[i-1]) Y[++ty]=yy[i],pre[yy[i]+a]=++tot; if (!ty) continue; Sort (e+1,e+h+1);Build (1,1,ty); for (i=1;i

Bzoj 3225: [Sdoi2008] Cube Overlay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.