HDU 5029 Relief grain (树链剖分+权值线段树思想)

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"YES\n";
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define piii pair<pii,int>
#define fi first                                  
#define se second
#define mk(x,y) make_pair(x,y)
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
const int mod=201314;
const int maxn=1e5+5;
const int lz=0;
const ll INF=1e18;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);}

int n,m;
vector<int> g[maxn];
///树链剖分
int dep[maxn],fa[maxn],son[maxn],sz[maxn];
void dfs1(int u,int f){
    sz[u]=1,dep[u]=dep[f]+1,son[u]=0,fa[u]=f;
    for(auto v:g[u]) if(v!=f){
        dfs1(v,u);
        sz[u]+=sz[v];
        if(sz[v]>sz[son[u]]) son[u]=v;
    }
}
int dfn[maxn],tot=0,top[maxn],mp[maxn];
void dfs2(int u,int f){
    top[u]=f,dfn[u]=++tot,mp[tot]=u;
    if(son[u]) dfs2(son[u],f);
    for(auto v:g[u]) if(v!=fa[u]&&v!=son[u])
        dfs2(v,v);
}
///线段树
int maxc[maxn<<2],maxv[maxn<<2];///线段树
void build(lrt){
    maxc[rt]=maxv[rt]=0;
    if(l==r){
        maxv[rt]=l;
        return ;
    }
    int mid=l+r>>1;
    build(lson),build(rson);
}
void update(lrt,int pos,int v){
    if(l==r){
        maxv[rt]=l;
        maxc[rt]+=v;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid) update(lson,pos,v);
    else update(rson,pos,v);
    if(maxc[rt<<1]>=maxc[rt<<1|1])
        maxc[rt]=maxc[rt<<1],maxv[rt]=maxv[rt<<1];
    else 
        maxc[rt]=maxc[rt<<1|1],maxv[rt]=maxv[rt<<1|1];
}
vector<pii> p[maxn];
void change(int x,int y,int z){
    while(top[x]^top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        p[dfn[top[x]]].push_back(mk(z,1));
        p[dfn[x]+1].push_back(mk(z,-1));
        x=fa[top[x]];
    }
    if(dep[x]<dep[y]) swap(x,y);
    p[dfn[y]].push_back(mk(z,1));
    p[dfn[x]+1].push_back(mk(z,-1));
}
int ans[maxn];
int main(){
    ios::sync_with_stdio(false);/*
    freopen("d://in.txt","r",stdin);
    freopen("d://out.txt","w",stdout);*/
    while(cin>>n>>m&&(n+m)){
        tot=0;build(1,maxn,1);
        rep(i,1,n+1) g[i].clear(),p[i].clear();
        rep(i,1,n){
            int x,y;
            cin>>x>>y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        dfs1(1,0),dfs2(1,1);
        rep(i,0,m){
            int x,y,z;
            cin>>x>>y>>z;
            change(x,y,z);
        }
        rep(i,1,n+1){
            for(auto v:p[i])
                update(1,maxn,1,v.fi,v.se);
            if(maxc[1]==0) ans[mp[i]]=0;
            else ans[mp[i]]=maxv[1];
        }
        rep(i,1,n+1) cout<<ans[i]<<"\n";
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值