这个题目初看没想法,估计要用到三维线段树,果断和队友先放下了。过了两天,看了前人的思想,简直吊爆了。居然转化为一维的来考虑,主要还是自己不细心,不然根据数据特征也可以推出来的,有思路就好办了,敲出来有个小错误,改了就过了,总体来说还是蛮顺利的。。。。
ACcode:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int nsize=2222;
int Z[nsize],X[nsize];
int cov[nsize<<2],len1[nsize<<2],len2[nsize<<2],len3[nsize<<2];
LL ans;
struct Rectange
{
int x1,y1,z1;
int x2,y2,z2;
}rec[nsize/2];
struct Line
{
int lx,rx,y;
int f;
Line(){}
Line(int a,int b,int c,int d) : lx(a),rx(b),y(c),f(d){}
bool operator < (const Line &cmp) const {
return y<cmp.y;
}
}line[nsize];
int Fin(int key,int len)
{
int l=0,r=len;
while (l<=r)
{
int m=(l+r)>>1;
if (X[m]==key) return m;
else if (X[m]>key) r=m-1;
else l=m+1;
}
return -1;
}
void PushUp(int rt,int l,int r)
{
int t1=rt<<1,t2=t1|1;
if (cov[rt]>=3)
{
len3[rt]=X[r+1]-X[l];
len2[rt]=len1[rt]=0;
}
else if (cov[rt]>=2)
{
len3[rt]=len1[t1]+len1[t2]+len2[t1]+len2[t2]+len3[t1]+len3[t2];
len2[rt]=X[r+1]-X[l]-len3[rt];
len1[rt]=0;
}
else if (cov[rt]>=1)
{
len3[rt]=len2[t1]+len2[t2]+len3[t1]+len3[t2];
len2[rt]=len1[t1]+len1[t2];
len1[rt]=X[r+1]-X[l]-len3[rt]-len2[rt];
}
else
{
len1[rt]=len1[t1]+len1[t2];
len2[rt]=len2[t1]+len2[t2];
len3[rt]=len3[t1]+len3[t2];
}
}
void update(int rt,int l,int r,int L,int R,int v)
{
if (L<=l&&r<=R)
{
cov[rt]+=v;
PushUp(rt,l,r);
return ;
}
int m=(l+r)>>1;
if (L<=m) update(rt<<1,l,m,L,R,v);
if (R>m) update(rt<<1|1,m+1,r,L,R,v);
PushUp(rt,l,r);
}
int main()
{
int n,i,j,k,nx,ny,p,T,cas=0;
int x1,x2,y1,y2,z1,z2;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d%d%d%d%d%d",&rec[i].x1,&rec[i].y1,&rec[i].z1,&rec[i].x2,&rec[i].y2,&rec[i].z2);
Z[i<<1]=rec[i].z1; Z[i<<1|1]=rec[i].z2;
}
sort(Z,Z+n*2);
for (k=0,i=1;i<n*2;i++)
if (Z[i]!=Z[i-1]) Z[++k]=Z[i];
for (ans=i=0;i<k;i++)
{
memset(cov,0,n<<5);
memset(len1,0,n<<5);
memset(len2,0,n<<5);
memset(len3,0,n<<5);
for (ny=j=0;j<n;j++)
{
if (rec[j].z1<=Z[i]&&rec[j].z2>=Z[i+1])
{
X[ny]=rec[j].x1; line[ny++]=Line(rec[j].x1,rec[j].x2,rec[j].y1,1);
X[ny]=rec[j].x2; line[ny++]=Line(rec[j].x1,rec[j].x2,rec[j].y2,-1);
}
}
sort(X,X+ny);
sort(line,line+ny);
for (nx=0,j=1;j<ny;j++)
if (X[j]!=X[j-1]) X[++nx]=X[j];
for (j=0;j<ny-1;j++)
{
int L=Fin(line[j].lx,nx);
int R=Fin(line[j].rx,nx)-1;
if (L<=R) update(1,0,nx,L,R,line[j].f);
ans+=1LL*len3[1]*(line[j+1].y-line[j].y)*(Z[i+1]-Z[i]);
}
}
printf("Case %d: %I64d\n",++cas,ans);
}
return 0;
}