UVa12171 hdu2771 UVaLive4291 Sculpture

时间:2022-12-07 14:51:44

填坑系列(p.171)

orz rjl

代码基本和rjl的一样

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream> template<typename Q> Q read(Q& x) {
static char c, f;
for(f = ; c = getchar(), !isdigit(c); ) if(c == '-') f = ;
for(x = ; isdigit(c); c = getchar()) x = x * + c - '';
if(f) x = -x;
return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
} const int maxn = + , maxc = + ;
const int dx[] = {, -, , , , };
const int dy[] = {, , -, , , };
const int dz[] = {, , , , -, }; int n, x0[maxn], y0[maxn], z0[maxn], x1[maxn], y1[maxn], z1[maxn]; int nx, ny, nz;
int xs[maxn*], ys[maxn*], zs[maxn*];
int color[maxn*][maxn*][maxn*]; struct Cell {
int x, y, z;
Cell() {}
Cell(int x, int y, int z) : x(x), y(y), z(z) {}
bool valid() const {
return x >= && x < nx - && y >= && y < ny - && z >= && z < nz - ;
}
bool solid() const {
return color[x][y][z] == ;
}
bool getvis() const {
return color[x][y][z] == ;
}
void setvis() const {
color[x][y][z] = ;
}
int volume() const {
return (xs[x+] - xs[x]) * (ys[y+] - ys[y]) * (zs[z+] - zs[z]);
}
Cell neighbor(int dir) const {
return Cell(x + dx[dir], y + dy[dir], z + dz[dir]);
}
int area(int dir) const {
if(dx[dir]) return (ys[y+] - ys[y]) * (zs[z+] - zs[z]);
if(dy[dir]) return (xs[x+] - xs[x]) * (zs[z+] - zs[z]);
return (xs[x+] - xs[x]) * (ys[y+] - ys[y]);
}
}; void discretize(int *s, int& n) {
std::sort(s, s + n);
n = std::unique(s, s + n) - s;
} int ID(int *s, int n, int x) {
return std::lower_bound(s, s + n, x) - s;
} #include<queue>
void floodfill(int &v, int &s) {
v = , s = ;
Cell c(, , );
c.setvis();
std::queue<Cell> q;
q.push(c);
while(!q.empty()) {
Cell c = q.front(); q.pop();
v += c.volume();
for(int i = ; i < ; i++) {
Cell c2 = c.neighbor(i);
if(!c2.valid()) continue;
if(c2.solid()) s += c.area(i);
else if(!c2.getvis()) {
c2.setvis();
q.push(c2);
}
}
}
v = maxc * maxc * maxc - v;
} int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif int T; scanf("%d", &T);
while(T--) {
nx = ny = nz = ;
xs[] = ys[] = zs[] = ;
xs[] = ys[] = zs[] = maxc;
scanf("%d", &n);
for(int i = ; i < n; i++) {
scanf("%d%d%d%d%d%d", &x0[i], &y0[i], &z0[i], &x1[i], &y1[i], &z1[i]);
x1[i] += x0[i]; y1[i] += y0[i]; z1[i] += z0[i];
xs[nx++] = x0[i]; xs[nx++] = x1[i];
ys[ny++] = y0[i]; ys[ny++] = y1[i];
zs[nz++] = z0[i]; zs[nz++] = z1[i];
}
discretize(xs, nx);
discretize(ys, ny);
discretize(zs, nz); memset(color, , sizeof color);
for(int i = ; i < n; i++) {
int X1 = ID(xs, nx, x0[i]), X2 = ID(xs, nx, x1[i]);
int Y1 = ID(ys, ny, y0[i]), Y2 = ID(ys, ny, y1[i]);
int Z1 = ID(zs, nz, z0[i]), Z2 = ID(zs, nz, z1[i]);
for(int X = X1; X < X2; X++) for(int Y = Y1; Y < Y2; Y++)
for(int Z = Z1; Z < Z2; Z++) color[X][Y][Z] = ;
}
int v, s;
floodfill(v, s);
printf("%d %d\n", s, v);
}
return ;
}