bzoj3531: [Sdoi2014]旅行

27 篇文章 0 订阅
20 篇文章 0 订阅

题面在这里
做法:
树剖+线段树动态开点。
容易想到对每个宗教建一棵线段树,不过空间限制不够,所以需要动态开点。别的都和正常树剖一样。

代码如下:

/*************************************************************
    Problem: bzoj 3531 [Sdoi2014]旅行
    User: fengyuan
    Language: C++
    Result: Accepted
    Time: 10784 ms
    Memory: 71536 kb
    Submit_Time: 2017-12-06 12:52:53
*************************************************************/

#include<bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i ++)
#define down(i, x, y) for (int i = (x); i >= (y); i --)
#define mid ((l+r)/2)
#define lc (o<<1)
#define rc (o<<1|1)
#define pb push_back
#define mp make_pair
#define PII pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
using namespace std;
typedef long long LL;
//head

const int N = 100010, M = 4000010;
int n, m, cnt, clk, tot;
char ch[5];
int head[N], w[N], c[N], rt[N], in[N], depth[N], fa[N], sz[N], son[N], top[N], pre[N], mx[M], sum[M], L[M], R[M];
struct Edge{
    int to, nex;
}e[N<<1];

inline void add(int x, int y)
{
    e[++ cnt].to = y;
    e[cnt].nex = head[x];
    head[x] = cnt;
}

inline void dfs(int u, int last, int s)
{
    depth[u] = s; fa[u] = last; sz[u] = 1;
    for (int i = head[u]; i; i = e[i].nex){
        int v = e[i].to; if (v == last) continue;
        dfs(v, u, s+1); sz[u] += sz[v];
        if (!son[u] || sz[v] > sz[son[u]]) son[u] = v;
    }
}

inline void dfs2(int u, int t)
{
    top[u] = t; in[u] = ++ clk; pre[clk] = u;
    if (son[u]) dfs2(son[u], t);
    for (int i = head[u]; i; i = e[i].nex){
        int v = e[i].to; if (v == fa[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}

inline void pushup(int o)
{
    sum[o] = sum[L[o]] + sum[R[o]];
    mx[o] = max(mx[L[o]], mx[R[o]]);
}

inline void update(int &o, int l, int r, int x, int v)
{
    if (!o) o = ++ tot;
    if (l == r){
        sum[o] = mx[o] = v;
        return;
    }
    if (x <= mid) update(L[o], l, mid, x, v);
    else update(R[o], mid+1, r, x, v);
    pushup(o);
}

inline int query(int o, int l, int r, int x, int y, int flag)
{
    if (l == x && r == y) return flag ? mx[o] : sum[o];
    if (y <= mid) return query(L[o], l, mid, x, y, flag);
    else if (x > mid) return query(R[o], mid+1, r, x, y, flag);
    else {
        int s1 = query(L[o], l, mid, x, mid, flag);
        int s2 = query(R[o], mid+1, r, mid+1, y, flag);
        if (flag) return max(s1, s2);
        else return s1 + s2;
    }
}

inline int solve(int t, int x, int y, int flag)
{
    int ret = 0;
    while (top[x] != top[y]){
        if (depth[top[x]] < depth[top[y]]) swap(x, y);
        if (flag) ret = max(ret, query(rt[t], 1, n, in[top[x]], in[x], flag));
        else ret += query(rt[t], 1, n, in[top[x]], in[x], flag);
        x = fa[top[x]];
    }
    if (depth[x] > depth[y]) swap(x, y);
    if (flag) ret = max(ret, query(rt[t], 1, n, in[x], in[y], flag));
    else ret += query(rt[t], 1, n, in[x], in[y], flag);
    return ret;
}

int main()
{
    scanf("%d%d", &n, &m);
    rep(i, 1, n) scanf("%d%d", &w[i], &c[i]);
    rep(i, 1, n-1){
        int x, y; scanf("%d%d", &x, &y);
        add(x, y); add(y, x);
    }
    dfs(1, 0, 0); dfs2(1, 1);
    rep(i, 1, n) update(rt[c[i]], 1, n, in[i], w[i]);
    while (m --){
        int x, y;
        scanf("%s%d%d", ch, &x, &y);
        if (ch[0] == 'C'){
            if (ch[1] == 'C'){
                update(rt[c[x]], 1, n, in[x], 0);
                c[x] = y;
                update(rt[c[x]], 1, n, in[x], w[x]);
            } else {
                update(rt[c[x]], 1, n, in[x], y);
                w[x] = y;
            }
        } else{
            if (ch[1] == 'M') printf("%d\n", solve(c[x], x, y, 1));
            else printf("%d\n", solve(c[x], x, y, 0));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值