其他分享
首页 > 其他分享> > 树链剖分 HYSBZ - 1036

树链剖分 HYSBZ - 1036

作者:互联网

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
    char ch = getchar(); ll x = 0, f = 1;
    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 3e4 + 10;
int dep[N], id[N], son[N], fa[N], top[N], siz[N], rk[N], wi[N];
struct node { int to, next; }edge[2*N];
int head[N];
int cnt = 0;
int num = 0;
int n, q;
ll maxn[N << 2];
ll sum[N << 2];
void pushup(int root)
{
    maxn[root] = max(maxn[lrt], maxn[rrt]);
    sum[root] = sum[lrt] + sum[rrt];
}
void build(int l, int r, int root)
{
    if (l == r)
    {
        sum[root]= maxn[root] = wi[rk[l]];
        return;
    }
    int mid = (l + r) >> 1;
    build(lson); build(rson);
    pushup(root);
}
void update(int l,int r,int root,int lf,int rt,int val)
{
    if (lf <= l && r <= rt)
    {
        maxn[root] = val;
        sum[root] = (r - l + 1)*1ll*val;
        return;
    }
    int mid = (l + r) >> 1;
    if(lf<=mid)update(lson, lf, rt, val);
    if (rt > mid)update(rson, lf, rt, val);
    pushup(root);
}
ll querrysum(int l, int r, int root, int lf, int rt)
{
    if (lf <= l && r <= rt)
    {
        return sum[root];
    }
    int mid = (l + r) >> 1;
    ll ans = 0;
    if (lf <= mid)ans += querrysum(lson, lf, rt);
    if (rt > mid)ans += querrysum(rson, lf, rt);
    return ans;
}
ll querrymax(int l, int r, int root, int lf, int rt)
{
    if (lf <= l && r <= rt)
    {
        return maxn[root];
    }
    int mid = (l + r) >> 1;
    ll ans = -1e18;
    if (lf <= mid)ans = max(ans, querrymax(lson, lf, rt));
    if (rt > mid)ans = max(ans, querrymax(rson, lf, rt));
    return ans;
}
void addedge(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
void dfs1(int u,int pre,int d)
{
    dep[u] = d;
    siz[u] = 1;
    fa[u] = pre;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int t = edge[i].to;
        if (t == pre)continue;
        dfs1(t, u, d + 1);
        siz[u] += siz[t];
        if (son[u] == -1 || siz[t] > siz[son[u]])
            son[u] = t;
    }
}
void dfs2(int u, int tp)
{
    top[u] = tp;
    id[u] = ++num;
    rk[num] = u;
    if (son[u] == -1)return;
    dfs2(son[u], tp);
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (v == fa[u] || v == son[u])continue;
        dfs2(v, v);
    }
}
void rgupdate(int x, int y,int val)
{
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]])swap(x, y);
        update(1, n, 1, id[top[x]], id[x], val);
        x = fa[top[x]];
    }
    if (dep[x] < dep[y])swap(x, y);
    update(1, n, 1, id[y], id[x], val);
}
ll rgsum(int x, int y)
{
    ll ans = 0;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]])swap(x, y);
        ans += querrysum(1, n, 1, id[top[x]], id[x]);
        x = fa[top[x]];
    }
    if (dep[x] < dep[y])swap(x, y);
    ans += querrysum(1, n, 1, id[y], id[x]);
    return ans;
}
ll rgmax(int x, int y)
{
    ll ans = -1e18;
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]])swap(x, y);
        ans = max(ans, querrymax(1, n, 1, id[top[x]], id[x]));
        x = fa[top[x]];
    }
    if (dep[x] < dep[y])swap(x, y);
    ans = max(ans, querrymax(1, n, 1, id[y], id[x]));
    return ans;
}
int main()
{
    n = read();
    memset(head, -1, sizeof(head));
    memset(son, -1, sizeof(son));
    int u, v, w;
    up(i, 0, n - 1)
    {
        u = read(), v = read();
        addedge(u, v); addedge(v, u);
    }
    upd(i, 1, n)wi[i] = read();
    dfs1(1, -1, 1);
    dfs2(1, 1);
    build(1, n, 1);
    q = read();
    char s[10];
    while (q--)
    {
        scanf("%s", s);
        if (s[0] == 'C')
        {
            u = read(), w = read();
            rgupdate(u, u, w);
        }
        else if (s[1] == 'M')
        {
            u = read(), v = read();
            printf("%lld\n", rgmax(u, v));
        }
        else {
            u = read(), v = read();
            printf("%lld\n", rgsum(u, v));
        }
    }
    return 0;
}

标签:剖分,int,HYSBZ,top,dep,ans,include,1036,id
来源: https://www.cnblogs.com/LORDXX/p/11707131.html