HDU_3624
这个题目要求去求覆盖三次及以上的部分的体积。
如果我们把z坐标离散化的话,每一层z中如果某个部分被覆盖了三次及以上的话,那么等价于这一层z在xy平面上的对应的投影被覆盖了三次及以上,因此对于每一层z我们可以先求出投影中被覆盖了三次及以上的面积,然后乘以这一层z的高度就是这一层z中被覆盖了三次及以上的体积了。
#include<stdio.h> #include<string.h> #include<stdlib.h> #define MAXD 2010 #define K 3 int N, M, Z, S, ty[MAXD], tz[MAXD], cover[4 * MAXD][4], cnt[4 * MAXD]; struct Rec { int x1, y1, z1, x2, y2, z2; }rec[MAXD]; struct Seg { int x, y1, y2, col; }seg[MAXD]; int cmpint(const void *_p, const void *_q) { int *p = (int *)_p, *q = (int *)_q; return *p < *q ? -1 : 1; } int cmpseg(const void *_p, const void *_q) { Seg *p = (Seg *)_p, *q = (Seg *)_q; return p->x < q->x ? -1 : 1; } void build(int cur, int x, int y) { int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1; memset(cover[cur], 0, sizeof(cover[cur])); cover[cur][0] = ty[y + 1] - ty[x]; cnt[cur] = 0; if(x == y) return ; build(ls, x, mid); build(rs, mid + 1, y); } void init() { int i, j, k; 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); tz[i << 1] = rec[i].z1, tz[(i << 1) | 1] = rec[i].z2; ty[i << 1] = rec[i].y1, ty[(i << 1) | 1] = rec[i].y2; } qsort(tz, N << 1, sizeof(tz[0]), cmpint); Z = -1; for(i = 0; i < (N << 1); i ++) if(i == 0 || tz[i] != tz[i - 1]) tz[++ Z] = tz[i]; qsort(ty, N << 1, sizeof(ty[0]), cmpint); M = -1; for(i = 0; i < (N << 1); i ++) if(i == 0 || ty[i] != ty[i - 1]) ty[++ M] = ty[i]; build(1, 0, M - 1); } int BS(int x) { int mid, min = 0, max = M + 1; for(;;) { mid = (max + min) >> 1; if(mid == min) break; if(ty[mid] <= x) min = mid; else max = mid; } return mid; } void update(int cur, int x, int y) { int ls = cur << 1, rs = (cur << 1) | 1; memset(cover[cur], 0, sizeof(cover[cur])); if(cnt[cur] >= K) cover[cur][K] = ty[y + 1] - ty[x]; else if(x == y) cover[cur][cnt[cur]] = ty[y + 1] - ty[x]; else { int i; for(i = cnt[cur]; i <= K; i ++) cover[cur][i] = cover[ls][i - cnt[cur]] + cover[rs][i - cnt[cur]]; for(i = K - cnt[cur] + 1; i <= K; i ++) cover[cur][K] += cover[ls][i] + cover[rs][i]; } } void refresh(int cur, int x, int y, int s, int t, int c) { int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1; if(x >= s && y <= t) { cnt[cur] += c; update(cur, x, y); return ; } if(mid >= s) refresh(ls, x, mid, s, t, c); if(mid + 1 <= t) refresh(rs, mid + 1, y, s, t, c); update(cur, x, y); } void solve() { int i, j, l, r; long long int ans = 0, temp; for(i = 0; i < Z; i ++) { S = 0; for(j = 0; j < N; j ++) if(rec[j].z1 <= tz[i] && rec[j].z2 >= tz[i + 1]) { seg[S].x = rec[j].x1, seg[S].y1 = rec[j].y1, seg[S].y2 = rec[j].y2, seg[S].col = 1; ++ S; seg[S].x = rec[j].x2, seg[S].y1 = rec[j].y1, seg[S].y2 = rec[j].y2, seg[S].col = -1; ++ S; } qsort(seg, S, sizeof(seg[0]), cmpseg); seg[S].x = seg[S - 1].x; temp = 0; for(j = 0; j < S; j ++) { l = BS(seg[j].y1), r = BS(seg[j].y2); refresh(1, 0, M - 1, l, r - 1, seg[j].col); temp += (long long int)cover[1][3] * (seg[j + 1].x - seg[j].x); } ans += temp * (tz[i + 1] - tz[i]); } printf("%I64d\n", ans); } int main() { int t, tt; scanf("%d", &t); for(tt = 0; tt < t; tt ++) { init(); printf("Case %d: ", tt + 1); solve(); } return 0; }