【树链剖分】月下毛景树

传送门

把边权转化为点权。由于每个点只有一个父亲,那么把这个点和它父亲连起来的边的权值转化到这个点身上。剩下的就是树剖裸题了。跳重链的时候还是先操作[dfn[tp[u]],dfn[u]],因为每个点对应它和它父亲连边的权值。最后到同一个重链的时候要变成[dfn[v]+1,dfn[u]],因为最上面多算了一条边,dfs序要加1。

注意一下线段树标记的覆盖。下推标记时先要下放cover标记再放add标记。下放cover标记的时候要把add标记清零。

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define mid ((T[root].l+T[root].r)>>1)
#define lc (root<<1)
#define rc (root<<1|1)
using namespace std;
const int maxn=1e5+10;
int Head[maxn],Next[maxn<<1],V[maxn<<1],W[maxn<<1],val[maxn],cnt=0;
int dep[maxn],tp[maxn],son[maxn],fa[maxn],siz[maxn],t[maxn],dfn[maxn],tot=0;
int n,m,u,v,w,k;char op[10];
struct node{int l,r,cov,add,mx;node(){l=r=cov=add=mx=0;}}T[maxn<<2];
struct E{int u,v,w;E(){}E(int u,int v,int w):u(u),v(v),w(w){}}e[maxn<<1];
inline void add(int u,int v,int w){++cnt,Next[cnt]=Head[u],V[cnt]=v,W[cnt]=w,Head[u]=cnt;}
inline int read(){
    int x=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x;
}
inline void init(){n=read();for(int i=1;i<n;++i)u=read(),v=read(),w=read(),add(u,v,w),add(v,u,w),e[i]=E(u,v,w);}
inline void pushup(int root){T[root].mx=max(T[lc].mx,T[rc].mx);}
inline void covnow(int root,int cov){T[root].add=0,T[root].cov=T[root].mx=cov;}
inline void addnow(int root,int add){T[root].mx+=add,T[root].add+=add;}
inline void pushdown(int root){
    if(T[root].cov)covnow(lc,T[root].cov),covnow(rc,T[root].cov),T[root].cov=0;
    if(T[root].add)addnow(lc,T[root].add),addnow(rc,T[root].add),T[root].add=0;
}
void cover(int root,int l,int r,int cov){
    if(T[root].l>=l&&T[root].r<=r){covnow(root,cov);return;}
    pushdown(root);
    if(l<=mid) cover(lc,l,r,cov);
    if(r> mid) cover(rc,l,r,cov);
    pushup(root);
}
void change(int root,int l,int r,int val){
    if(T[root].l>=l&&T[root].r<=r){addnow(root,val);return;}
    pushdown(root);
    if(l<=mid) change(lc,l,r,val);
    if(r> mid) change(rc,l,r,val);
    pushup(root);
}
int askmax(int root,int l,int r,int ret=0){
    if(T[root].l>=l&&T[root].r<=r) return T[root].mx;
    pushdown(root);
    if(l<=mid) ret=max(ret,askmax(lc,l,r));
    if(r> mid) ret=max(ret,askmax(rc,l,r));
    return ret;
}
inline void build(int root,int l,int r){
    T[root].l=l,T[root].r=r;
    if(l==r){T[root].mx=val[t[l]];return;}
    build(lc,l,mid),build(rc,mid+1,r);
    pushup(root);
}
inline void Update_Add(int u,int v,int w){
    while(tp[u]!=tp[v]){
        if(dep[tp[u]]<dep[tp[v]]) swap(u,v);
        change(1,dfn[tp[u]],dfn[u],w);
        u=fa[tp[u]];
    }
    if(dep[u]<dep[v]) swap(u,v);
    if((dfn[v]+1)<=dfn[u])change(1,dfn[v]+1,dfn[u],w);
}
inline void Update_Cover(int u,int v,int w){
    while(tp[u]!=tp[v]){
        if(dep[tp[u]]<dep[tp[v]]) swap(u,v);
        cover(1,dfn[tp[u]],dfn[u],w);
        u=fa[tp[u]];
    }if(dep[u]<dep[v]) swap(u,v);
    if((dfn[v]+1)<=dfn[u])cover(1,dfn[v]+1,dfn[u],w);
}
inline int getans(int u,int v,int ret=0){
    while(tp[u]!=tp[v]){
        if(dep[tp[u]]<dep[tp[v]]) swap(u,v);
        ret=max(ret,askmax(1,dfn[tp[u]],dfn[u]));
        u=fa[tp[u]];
    }if(dep[u]<dep[v]) swap(u,v);
    if((dfn[v]+1)<=dfn[u])ret=max(ret,askmax(1,dfn[v]+1,dfn[u]));
    return ret;
}
void dfs1(int u,int f,int value){
    dep[u]=dep[f]+1,fa[u]=f,son[u]=0,siz[u]=1,val[u]=value;
    for(int i=Head[u];i;i=Next[i]) if(V[i]!=f){
        dfs1(V[i],u,W[i]),siz[u]+=siz[V[i]];
        if(siz[son[u]]<siz[V[i]]) son[u]=V[i];
    }
}
void dfs2(int u,int top){
    t[dfn[u]=++tot]=u,tp[u]=top;
    if(son[u]) dfs2(son[u],top);
    for(int i=Head[u];i;i=Next[i]) if(V[i]!=fa[u]&&V[i]!=son[u])
        dfs2(V[i],V[i]);
}
inline void print(int x){
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
int main(){
    init(),dfs1(1,0,0),dfs2(1,1),build(1,1,tot);
    while(scanf("%s",op)&&op[0]!='S'){
        if(op[1]=='h'){
            k=read(),w=read();
            if(dep[e[k].u]<dep[e[k].v]) swap(e[k].u,e[k].v);
            cover(1,dfn[e[k].u],dfn[e[k].u],w);
        }
        if(op[1]=='o') u=read(),v=read(),w=read(),Update_Cover(u,v,w);
        if(op[1]=='d') u=read(),v=read(),w=read(),Update_Add(u,v,w);
        if(op[1]=='a') u=read(),v=read(),print(getans(u,v)),putchar('\n');
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值