# bzoj4182 Shopping 点分治+单调队列优化多重背包

## 题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4182

## 题解

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}

const int N = 500 + 7;
const int M = 4000 + 7;
const int INF = 0x3f3f3f3f;

int n, m, rt, mima, sum, dfc, ans;
int w[N], c[N], d[N];
int vis[N], siz[N], sq[N], dfn[N], q[M], p[M], dp[N][M];

struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }

inline void getrt(int x, int fa = 0, int dep = 0) {
int f = 0; siz[x] = 1;
for fec(i, x, y) if (!vis[y] && y != fa) getrt(y, x), siz[x] += siz[y], smax(f, siz[y]);
smax(f, sum - siz[x]);
if (smin(mima, f)) rt = x;
}

inline void dfs(int x, int fa = 0) {
siz[x] = 1;
for fec(i, x, y) if (!vis[y] && y != fa) dfs(y, x), siz[x] += siz[y];
dfn[x] = ++dfc, sq[dfc] = x;
}

inline void calc(int x) {
dfc = 0, dfs(x);
for (int i = 1; i <= dfc; ++i) {
int x = sq[i], hd = 1, tl = 0;
for (int j = 0; j < c[x]; ++j) {
hd = 1, tl = 0;
for (int k = 0; k * c[x] + j <= m; ++k) {
int v = k * c[x] + j, y = dp[i - 1][v] - k * w[x];
while (hd <= tl && q[hd] < k - d[x]) ++hd;
if (hd <= tl) dp[i][v] = std::max(p[hd] + k * w[x], dp[i - siz[x]][v]);
else dp[i][v] = dp[i - siz[x]][v];
while (hd <= tl && y >= p[tl]) --tl;
q[++tl] = k, p[tl] = y;
}
}
}
smax(ans, dp[dfc][m]);
}

inline void solve(int x) {
vis[x] = 1, calc(x);
for fec(i, x, y) if (!vis[y]) {
mima = sum = siz[y];
getrt(y), solve(rt);
}
}

inline void work() {
mima = sum = n;
getrt(1), solve(rt);
printf("%d\n", ans);
}

inline void cls() {
memset(vis, 0, sizeof(vis));
ans = tot = 0;
}

inline void init() {
cls();
for (int i = 1; i <= n; ++i) read(w[i]);
for (int i = 1; i <= n; ++i) read(c[i]);
for (int i = 1; i <= n; ++i) read(d[i]);
int x, y;
}

int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
int T;
while (T--) {
init();
work();
}
fclose(stdin), fclose(stdout);
return 0;
}

posted @ 2019-09-21 07:26  hankeke  阅读(86)  评论(0编辑  收藏  举报