給定n個立方體,求這些立方體覆寫兩次以上的體積。
之前做過求矩形面積交的題,這題可以轉化為求矩形面積交。先對y坐标和z坐标離散化,然後枚舉每個z軸區間,求在該區間内矩形覆寫兩次以上的面積,再乘以該區間寬度,即為該區間覆寫兩次以上的體積。由于更新節點時有些地方沒考慮好,TLE和WA了幾次。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
using namespace std;
const int maxn = 2010;
int yy[maxn], zz[maxn];
int cov[maxn<<2], once[maxn<<2];
int twice[maxn<<2], more[maxn<<2];
map<int, int> my;
int t, n;
struct Point
{
int x, y, z;
void get()
{
scanf("%d%d%d", &x, &y, &z);
}
};
struct Cube
{
Point a, b;
}cube[maxn];
struct Line
{
int y1, y2, x;
int flag;
}line[maxn];
bool cmp(Line a, Line b)
{
if (a.x != b.x)
return a.x < b.x;
return a.flag > b.flag;
}
void addLine(int y1, int y2, int x, int flag, int idx)
{
line[idx].y1 = y1;
line[idx].y2 = y2;
line[idx].x = x;
line[idx].flag = flag;
}
void build(int l, int r, int rt)
{
cov[rt] = once[rt] = twice[rt] = more[rt] = 0;
if (l + 1 == r) return ;
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m, r, rt << 1 | 1);
}
void pushUp(int l, int r, int rt)
{
if (cov[rt] > 2)
{
more[rt] = yy[r] - yy[l];
once[rt] = twice[rt] = 0;
return ;
}
if (cov[rt] == 2)
{
if (l + 1 == r)
{
twice[rt] = yy[r] - yy[l];
once[rt] = more[rt] = 0;
}
else
{
more[rt] = more[rt<<1] + more[rt<<1|1] + twice[rt<<1] +
twice[rt<<1|1] + once[rt<<1] + once[rt<<1|1];
twice[rt] = yy[r] - yy[l] - more[rt];
once[rt] = 0;
}
return ;
}
if (cov[rt] == 1)
{
if (l + 1 == r)
{
once[rt] = yy[r] - yy[l];
twice[rt] = more[rt] = 0;
}
else
{
more[rt] = more[rt<<1] + more[rt<<1|1] + twice[rt<<1] + twice[rt<<1|1];
twice[rt] = once[rt<<1] + once[rt<<1|1];
once[rt] = yy[r] - yy[l] - twice[rt] - more[rt];
}
return ;
}
if (cov[rt] == 0)
{
if (l + 1 == r)
more[rt] = twice[rt] = once[rt] = 0;
else
{
more[rt] = more[rt<<1] + more[rt<<1|1];
twice[rt] = twice[rt<<1] + twice[rt<<1|1];
once[rt] = once[rt<<1] + once[rt<<1|1];
}
return ;
}
}
void update(int L, int R, int c, int l, int r, int rt)
{
if (L <= l && R >= r)
{
cov[rt] += c;
pushUp(l, r, rt);
return ;
}
if (l + 1 == r) return ;
int m = (l + r) >> 1;
if (L < m) update(L, R, c, l, m, rt << 1);
if (R > m) update(L, R, c, m, r, rt << 1 | 1);
pushUp(l, r, rt);
}
void solve()
{
sort(yy, yy + n * 2);
sort(zz, zz + n * 2);
int cnty = 1;
for (int i = 1; i < n * 2; ++i)
{
if (yy[i] != yy[cnty-1])
yy[cnty++] = yy[i];
}
int cntz = 1;
for (int i = 1; i < n * 2; ++i)
{
if (zz[i] != zz[cntz-1])
zz[cntz++] = zz[i];
}
for (int i = 0; i < cnty; ++i)
my[yy[i]] = i;
long long ans = 0;
build(0, cnty - 1, 1);
for (int i = 0; i < cntz - 1; ++i)
{
int cnt = 0;
for (int j = 0; j < n; ++j)
{
if (cube[j].a.z <= zz[i] && cube[j].b.z > zz[i])
{
addLine(cube[j].a.y, cube[j].b.y, cube[j].a.x, 1, cnt++);
addLine(cube[j].a.y, cube[j].b.y, cube[j].b.x, -1, cnt++);
}
}
sort(line, line + cnt, cmp);
for (int j = 0; j < cnt - 1; ++j)
{
update(my[line[j].y1], my[line[j].y2], line[j].flag, 0, cnty - 1, 1);
ans += (zz[i+1] - zz[i]) * (long long)more[1] * (line[j+1].x - line[j].x);
}
update(my[line[cnt-1].y1], my[line[cnt-1].y2], line[cnt-1].flag, 0, cnty - 1, 1);
}
printf("%I64d\n", ans);
}
int main()
{
scanf("%d", &t);
for (int cas = 1; cas <= t; ++cas)
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
cube[i].a.get();
yy[i*2] = cube[i].a.y;
zz[i*2] = cube[i].a.z;
cube[i].b.get();
yy[i*2+1] = cube[i].b.y;
zz[i*2+1] = cube[i].b.z;
}
printf("Case %d: ", cas);
solve();
}
return 0;
}