20221112_T1A+_整体二分背包

题意

给定一个树,有 \(q\) 个询问,每次都是其子树内做背包。

题解

赛时得分: 100/100

子树,我们不难想到用 dfs 序上操作,那么现在问题变成了区间背包。

区间背包怎么做,首先,对于一般的背包删除是需要动脑子的(并不是不行,所以其实按道理有一个 \(\mathcal{O}(nV\sqrt{n})\) 的莫队算法)但是应该非常勉强过不了把。

那么区间背包还有什么办法么?

整体二分!

我们把过区间中点的区间找出来就好了。

代码调了 1h,还不错。

代码

#include <bits/stdc++.h>
using namespace std;
static char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf;
#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
template <typename T>inline void read(T& t){t=0; register char ch=getchar(); register int fflag=1;while(!('0'<=ch&&ch<='9')) {if(ch=='-') fflag=-1;ch=getchar();}while(('0'<=ch&&ch<='9')){t=t*10+ch-'0'; ch=getchar();} t*=fflag;}
template <typename T,typename... Args> inline void read(T& t, Args&... args) {read(t);read(args...);}
const int N = 5e3 + 10, inf = 0x3f3f3f3f, V = 5e3;

typedef long long ll;
int n, L[N], R[N], cnt, w[N], v[N];
vector<int>G[N];

void dfs(int u, int fa) {
    L[u] = ++cnt;
    for(int v : G[u]) {
        if(v == fa) continue;
        dfs(v, u);
    }
    R[u] = cnt;
}
struct Querys {
    int val, l, r, id;
};
vector<Querys>Q;
ll ans[100008], dpl[N][N], dpr[N][N];

inline void solve(int l, int r, vector<Querys>q) {
    if(l > r) return;
    if(l == r) {
        for(int i = 0; i < q.size(); ++i) ans[q[i].id] = ((q[i].val >= v[l]) ? w[l] : 0);
        return;
    }
    vector<Querys>left, right, todo;
    int mid = l + r >> 1;
    for(int i = 0; i < q.size(); ++i) {
        if(q[i].r <= mid) left.push_back(q[i]);
        else if(q[i].l > mid) right.push_back(q[i]);
        else todo.push_back(q[i]);
    }
    for(int i = mid + 1; i >= l; --i) for(int j = 0; j <= V; ++j) dpl[i][j] = 0;
    for(int i = mid; i <= r; ++i) for(int j = 0; j <= V; ++j) dpr[i][j] = 0;
    for(int i = mid; i >= l; --i) {
        for(int j = 0; j <= V; ++j) {
            if(j >= v[i])dpl[i][j] = max(dpl[i + 1][j], dpl[i + 1][j - v[i]] + w[i]);
            else dpl[i][j] = dpl[i + 1][j];
        }
        for(int j = 1; j <= V; ++j) dpl[i][j] = max(dpl[i][j], dpl[i][j - 1]);
    }
    for(int i = mid + 1; i <= r; ++i) {
        for(int j = 0; j <= V; ++j) {
            if(j >= v[i]) dpr[i][j] = max(dpr[i - 1][j], dpr[i - 1][j - v[i]] + w[i]);
            else dpr[i][j] = dpr[i - 1][j];
        }
        for(int j = 1; j <= V; ++j) dpr[i][j] = max(dpr[i][j], dpr[i][j - 1]);
    }
    for(int i = 0; i < todo.size(); ++i) {
        int x = todo[i].l, y = todo[i].r, val = todo[i].val;
        for(int j = 0; j <= val; ++j) ans[todo[i].id] = max(ans[todo[i].id], dpl[x][j] + dpr[y][val - j]);
    }
    solve(l, mid, left), solve(mid + 1, r, right);
}
void write(ll x) {
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}

signed main() {
    freopen("A.in", "r", stdin);
    freopen("A.out", "w", stdout);
    read(n);
    for(int i = 1; i < n; ++i) {
        int u, v;
        read(u, v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, -1);
    for(int i = 1; i <= n; ++i) read(w[L[i]], v[L[i]]);
    int q;
    read(q);
    for(int i = 1; i <= q; ++i) {
        int x, val;
        read(x, val);
        Q.push_back({val, L[x], R[x], i});
    }
    solve(1, n, Q);
    for(int i = 1; i <= q; ++i) write(ans[i]), puts("");
    return 0;
}
posted @ 2022-11-12 19:29  Mercury_City  阅读(35)  评论(0编辑  收藏  举报