2023 杭电多校 1

B. City Upgrading

给定一棵树,你需要给所有节点染色,对每个节点染色,他会传染到于该节点相邻的节点,对每个节点染色的代价为\(w_i\),求给整棵树染色的最小代价

题解:最小支配集问题

  • 非常经典的最小支配集问题,考虑树形\(dp\)

  • 我们设计状态:

\(dp[u][0]:\)\(u\)本身不染色,被其父亲节点感染

\(dp[u][1]:\)\(u\)本身染色,花费代价\(w_u\)

\(dp[u][2]:\)\(u\)本身不染色,被其儿子节点染色

  • 考虑状态转移:

\(dp[u][0] = \sum min(dp[v][1], dp[v][2])\)

\(dp[u][1] = \sum min(dp[v][0], dp[v][1], dp[v][2]) + w_u\)

\(dp[u][2] = \sum min(dp[v][1], dp[v][2]) + \sum min(dp[v][1] - min(dp[v][1], dp[v][2]))\)

int n, a[N], dp[N][3];
vector<int> g[N];

void dfs(int u, int par)
{
    dp[u][1] = a[u];
    dp[u][0] = dp[u][2] = 0;
    int mi = INF;
    for (auto v : g[u])
    {
        if (v == par)
            continue;
        dfs(v, u);
        dp[u][0] += min(dp[v][1], dp[v][2]);
        dp[u][1] += min({dp[v][0], dp[v][1], dp[v][2]});
        dp[u][2] += min(dp[v][1], dp[v][2]);
        mi = min(mi, dp[v][1] - min(dp[v][1], dp[v][2]));
    }
    dp[u][2] += mi;
}

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= n; ++i)
        g[i].clear();
    for (int i = 1; i < n; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 0);
    cout << min(dp[1][1], dp[1][2]) << endl;
}

J. Easy problem I

给定长度为\(n\)的序列\(a\),存在两种类型的操作:
\(1,l,r,x:Modify\ i\in[l, r],a_i:=|a_i - x|\)\(x\)单增

\(2, l, r:Query\ \sum_{i=l}^ra_i\)

题解:势能线段树

  • 容易想到,对于一个数\(x\)还没被减到成为负数前,对于这个数的操作一定是区间减\(x:=x-k\),一旦\(x\)被减到了负数,因为每次减的数\(k\)是单增的,对于\(x\)来说一定是\(x:=-x+k\)
  • 所以我们把所有数分成两种数,第一种数只需要区间减,第二种数支持先取负再区间加,所以我们考虑建立两颗线段树,每当一个数被第一次减到负数的时候,将其在第一颗树上暴力删除,然后暴力添加到第二颗树上
  • 我们发现每个数一开始都是第一种数,最多只会有\(n\)个数转化为第二种数,所以复杂度为\(O(nlogn)\)
  • 第一颗线段树支持区间减,维护区间\(min\),第二颗线段树支持区间乘和区间加
  • 如果\(k<mi\),说明该区间内存在一些数,将会从第一种数转化为第二种数,所以我们可以在第一颗树上暴力递归下去
  • 对于查询区间和,我们直接查询第一颗线段树上的区间和以及第二颗树上的区间和即可
  • 时间复杂度:\(O(nlogn + qlogn)\)
int n, q, a[N];

struct SEG2
{
    struct info
    {
        int sum, len;
        info(int sum = 0, int len = 0) : sum(sum), len(len) {}
        friend info operator+(const info &a, const info &b)
        {
            info c;
            c.len = a.len + b.len;
            c.sum = a.sum + b.sum;
            return c;
        }
    };
    struct node
    {
        int lazy_mul, lazy_add;
        info val;
    } seg[N << 2];
    void up(int id) { seg[id].val = seg[lson].val + seg[rson].val; }
    void build(int id, int l, int r)
    {
        seg[id].lazy_add = 0, seg[id].lazy_mul = 1;
        if (l == r)
        {
            seg[id].val = info(0, 0);
            return;
        }
        int mid = l + r >> 1;
        build(lson, l, mid);
        build(rson, mid + 1, r);
        up(id);
    }
    void settag_mul(int id, int tag)
    {
        seg[id].val.sum *= tag;
        seg[id].lazy_mul *= tag;
        seg[id].lazy_add *= tag;
    }
    void settag_add(int id, int tag)
    {
        seg[id].val.sum += tag * seg[id].val.len;
        seg[id].lazy_add += tag;
    }
    void down(int id)
    {
        if (seg[id].lazy_mul != 1)
        {
            settag_mul(lson, seg[id].lazy_mul);
            settag_mul(rson, seg[id].lazy_mul);
        }
        if (seg[id].lazy_add != 0)
        {
            settag_add(lson, seg[id].lazy_add);
            settag_add(rson, seg[id].lazy_add);
        }
        seg[id].lazy_mul = 1;
        seg[id].lazy_add = 0;
    }
    void modify_mul(int id, int l, int r, int ql, int qr, int v)
    {
        if (ql <= l && r <= qr)
        {
            settag_mul(id, v);
            return;
        }
        down(id);
        int mid = l + r >> 1;
        if (qr <= mid)
            modify_mul(lson, l, mid, ql, qr, v);
        else if (ql > mid)
            modify_mul(rson, mid + 1, r, ql, qr, v);
        else
        {
            modify_mul(lson, l, mid, ql, qr, v);
            modify_mul(rson, mid + 1, r, ql, qr, v);
        }
        up(id);
    }
    void modify_add(int id, int l, int r, int ql, int qr, int v)
    {
        if (ql <= l && r <= qr)
        {
            settag_add(id, v);
            return;
        }
        down(id);
        int mid = l + r >> 1;
        if (qr <= mid)
            modify_add(lson, l, mid, ql, qr, v);
        else if (ql > mid)
            modify_add(rson, mid + 1, r, ql, qr, v);
        else
        {
            modify_add(lson, l, mid, ql, qr, v);
            modify_add(rson, mid + 1, r, ql, qr, v);
        }
        up(id);
    }
    void change(int id, int l, int r, int x, int v)
    {
        if (l == r)
        {
            seg[id].val = info(v, 1);
            return;
        }
        down(id);
        int mid = l + r >> 1;
        if (x <= mid)
            change(lson, l, mid, x, v);
        else
            change(rson, mid + 1, r, x, v);
        up(id);
    }
    info query(int id, int l, int r, int ql, int qr)
    {
        if (ql <= l && r <= qr)
            return seg[id].val;
        down(id);
        int mid = l + r >> 1;
        if (qr <= mid)
            return query(lson, l, mid, ql, qr);
        else if (ql > mid)
            return query(rson, mid + 1, r, ql, qr);
        else
            return query(lson, l, mid, ql, qr) + query(rson, mid + 1, r, ql, qr);
    }
} seg2;

struct SEG1
{
    struct info
    {
        int sum, len, mi;
        info(int sum = 0, int len = 0, int mi = 0) : sum(sum), len(len), mi(mi) {}
        friend info operator+(const info &a, const info &b)
        {
            info c;
            c.len = a.len + b.len;
            c.sum = a.sum + b.sum;
            c.mi = min(a.mi, b.mi);
            return c;
        }
    };
    struct node
    {
        int lazy;
        info val;
    } seg[N << 2];
    void up(int id) { seg[id].val = seg[lson].val + seg[rson].val; }
    void build(int id, int l, int r)
    {
        seg[id].lazy = 0;
        if (l == r)
        {
            seg[id].val = info(a[l], 1, a[l]);
            return;
        }
        int mid = l + r >> 1;
        build(lson, l, mid);
        build(rson, mid + 1, r);
        up(id);
    }
    void settag(int id, int tag)
    {
        seg[id].val.sum += tag * seg[id].val.len;
        seg[id].val.mi += tag;
        seg[id].lazy += tag;
    }
    void down(int id)
    {
        if (!seg[id].lazy)
            return;
        settag(lson, seg[id].lazy);
        settag(rson, seg[id].lazy);
        seg[id].lazy = 0;
    }
    void modify(int id, int l, int r, int ql, int qr, int v)
    {
        if (ql <= l && r <= qr && seg[id].val.mi >= v)
        {
            settag(id, -v);
            return;
        }
        if (l == r)
        {
            seg2.change(1, 1, n, l, v - seg[id].val.sum);
            seg[id].val = info(0, 0, INF);
            return;
        }
        down(id);
        int mid = l + r >> 1;
        if (qr <= mid)
            modify(lson, l, mid, ql, qr, v);
        else if (ql > mid)
            modify(rson, mid + 1, r, ql, qr, v);
        else
        {
            modify(lson, l, mid, ql, qr, v);
            modify(rson, mid + 1, r, ql, qr, v);
        }
        up(id);
    }
    info query(int id, int l, int r, int ql, int qr)
    {
        if (ql <= l && r <= qr)
            return seg[id].val;
        down(id);
        int mid = l + r >> 1;
        if (qr <= mid)
            return query(lson, l, mid, ql, qr);
        else if (ql > mid)
            return query(rson, mid + 1, r, ql, qr);
        else
            return query(lson, l, mid, ql, qr) + query(rson, mid + 1, r, ql, qr);
    }
} seg1;

void solve()
{
    cin >> n >> q;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    seg1.build(1, 1, n), seg2.build(1, 1, n);
    while (q--)
    {
        int op, l, r, x;
        cin >> op >> l >> r;
        if (op == 1)
        {
            cin >> x;
            seg2.modify_mul(1, 1, n, l, r, -1);
            seg2.modify_add(1, 1, n, l, r, x);
            seg1.modify(1, 1, n, l, r, x);
        }
        else
            cout << seg1.query(1, 1, n, l, r).sum + seg2.query(1, 1, n, l, r).sum << endl;
    }
}

L. Play on Tree

给定一颗树,每个点成为根节点的概率相同,两个人在这颗树上进行游戏,每个回合一个人可以选择一个节点\(u\),然后删除其子树,最先删除根节点的人输,求先手获胜的概率

题解:树上博弈 + 换根\(dp\)

  • 考虑到叶子节点的\(sg = 0\),设\(dp1[u]\)代表以\(u\)为根的子树的\(sg\)
  • $dp1[u] = (dp1[v_1] + 1) \oplus (dp1[v_2] + 1) ... $
  • 定义\(dp2[u]\)代表\(u\)的父亲节点作为其儿子时的\(sg\)值,\(ans[u]\)代表\(u\)作为根节点时的\(sg\)
  • 显然\(ans[u] = dp1[u] \oplus (dp2[u] + 1)\)
  • 那么\(dp2[v] = ans[u] \oplus (dp1[v] + 1), u是v的父亲\)
  • 注意:\(ans[1] = dp1[1]\),所以我们必须保证\(dp2[1] = -1\),因为必须保证\(dp2[1] + 1 = 0\)
int n, dp1[N], dp2[N], ans[N], Ans;
vector<int> g[N];

int qpow(int a, int b, int p)
{
    int res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a % p;
        b >>= 1;
        a = a * a % p;
    }
    return res;
}

void dfs1(int u, int par)
{
    for (auto v : g[u])
    {
        if (v == par)
            continue;
        dfs1(v, u);
        dp1[u] ^= (dp1[v] + 1);
    }
}

void dfs2(int u, int par)
{
    ans[u] = (dp1[u] ^ (dp2[u] + 1));
    if (ans[u])
        Ans++;
    for (auto v : g[u])
    {
        if (v == par)
            continue;
        dp2[v] = (ans[u] ^ (dp1[v] + 1));
        dfs2(v, u);
    }
}

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
        g[i].clear(), ans[i] = dp1[i] = dp2[i] = 0;
    Ans = 0;
    for (int i = 1; i < n; ++i)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs1(1, 0);
    dp2[1] = -1; // 保证 :dp2[1] + 1 == 0 --> dp2[1] = -1
    dfs2(1, 0);
    cout << Ans * qpow(n, mod - 2, mod) % mod << endl;
}
posted @ 2023-09-21 15:21  Zeoy_kkk  阅读(52)  评论(0编辑  收藏  举报