HDU_3225
这个题目和求被覆盖K次的矩形面积的并比较相似,只要把dp的过程稍加修改,用len[i][j]表示第i个节点控制的区域中第j种蔬菜覆盖的长度,然后根据当前节点的状态和子节点的状态完成状态转移。
#include<stdio.h> #include<string.h> #include<stdlib.h> #define MAXD 60010 int N, K, M, ty[MAXD], cnt[4 * MAXD][3], len[4 * MAXD][3], price[5], hash[110]; struct Seg { int x, y1, y2, col, p; }seg[MAXD]; int cmpint(const void *_p, const void *_q) { int *p = (int *)_p, *q = (int *)_q; return *p < *q ? -1 : 1; } int cmps(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(cnt[cur], 0, sizeof(cnt[cur])), memset(len[cur], 0, sizeof(len[cur])); if(x == y) return ; build(ls, x, mid); build(rs, mid + 1, y); } void init() { int i, j, k, p[5], x1, y1, x2, y2, s; scanf("%d%d", &N, &K); for(i = 0; i < K; i ++) { scanf("%d", &p[i]); price[i] = p[i]; } qsort(price, K, sizeof(price[0]), cmpint); for(i = 0; i < K; i ++) hash[price[i]] = i; for(i = 0; i < N; i ++) { j = i << 1, k = (i << 1) | 1; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &s); -- s; seg[j].x = x1, seg[k].x = x2; seg[j].y1 = seg[k].y1 = y1, seg[j].y2 = seg[k].y2 = y2; seg[j].col = 1, seg[k].col = -1; seg[j].p = seg[k].p = hash[p[s]]; ty[j] = y1, ty[k] = y2; } 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 min = 0, max = M + 1, mid; for(;;) { mid = (min + max) >> 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(len[cur], 0, sizeof(len[cur])); if(x == y) { int i; for(i = K - 1; i >= 0; i --) if(cnt[cur][i]) { len[cur][i] = ty[y + 1] - ty[x]; break; } } else { int i, t = 0; for(i = K - 1; i >= 0; i --) { if(cnt[cur][i]) { len[cur][i] = ty[y + 1] - ty[x] - t; break; } else { len[cur][i] = len[ls][i] + len[rs][i]; t += len[cur][i]; } } } } void refresh(int cur, int x, int y, int s, int t, int c, int p) { int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1; if(x >= s && y <= t) { cnt[cur][p] += c; update(cur, x, y); return ; } if(mid >= s) refresh(ls, x, mid, s, t, c, p); if(mid + 1 <= t) refresh(rs, mid + 1, y, s, t, c, p); update(cur, x, y); } void solve() { int i, j, k; long long int ans = 0, t; qsort(seg, N << 1, sizeof(seg[0]), cmps); seg[N << 1].x = seg[(N << 1) - 1].x; for(i = 0; i < (N << 1); i ++) { j = BS(seg[i].y1), k = BS(seg[i].y2); if(j < k) refresh(1, 0, M - 1, j, k - 1, seg[i].col, seg[i].p); t = 0; for(j = 0; j < K; j ++) t += price[j] * len[1][j]; ans += t * (seg[i + 1].x - seg[i].x); } 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; }