树链剖分模板题-SPOJ QTREE Query on a tree

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3…N-1.

We will ask you to perfrom some instructions of the following form:

CHANGE i ti : change the cost of the i-th edge to ti
or
QUERY a b : ask for the maximum edge cost on the path from node a to node b
Input

The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.

For each test case:

In the first line there is an integer N (N <= 10000),
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
The next lines contain instructions “CHANGE i ti” or “QUERY a b”,
The end of each test case is signified by the string “DONE”.
There is one blank line between successive tests.

Output

For each “QUERY” operation, write one integer representing its result.

Example

Input:
1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Output:
1
3

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=10009;
vector<int> mp[maxn];
int sz[maxn],fa[maxn],tp[maxn],dep[maxn],son[maxn],id[maxn],idx=0,val[maxn];
int t,n;
char op[20];
struct edge
{
    int u,v,c;
}e[maxn];
void dfs1(int u,int f,int d)
{
    sz[u]=1;
    fa[u]=f;
    dep[u]=d;
    son[u]=0;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==f) continue;
        dfs1(v,u,d+1);
        sz[u]+=sz[v];
        if(son[u]==0||sz[son[u]]<sz[v])
            son[u]=v;
    }
}
void dfs2(int u,int tpp)
{
    tp[u]=tpp;
    id[u]=++idx;
    if(son[u]) dfs2(son[u],tpp);
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==fa[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}

int tv[maxn<<2];
void pushup(int rt)
{
    tv[rt]=max(tv[rt<<1],tv[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        tv[rt]=val[l];
        return;
    }
    int m=l+r>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p,int v,int l,int r,int rt)
{
    if(l==r)
    {
        tv[rt]=v;
        return;
    }
    int m=l+r>>1;
    if(p<=m) update(p,v,lson);
    else update(p,v,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return tv[rt];
    int m=l+r>>1;
    int ans=-0x3f3f3f3f;
    if(L<=m)
        ans=max(ans,query(L,R,lson));
    if(m<R)
        ans=max(ans,query(L,R,rson));
    return ans;
}
int qq(int u,int v)
{
    int t1=tp[u],t2=tp[v];
    int ans=-0x3f3f3f3f;
    while(t1!=t2)
    {
        if(dep[t1]<dep[t2])
        {
            swap(u,v);
            swap(t1,t2);
        }
        ans=max(query(id[t1],id[u],1,idx,1),ans);
        u=fa[t1];
        t1=tp[u];
    }
    if(u==v) return ans;
    if(dep[u]>dep[v]) swap(u,v);
    ans=max(query(id[son[u]],id[v],1,idx,1),ans);
    return ans;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        idx=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            mp[i].clear();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);
            mp[e[i].u].push_back(e[i].v);
            mp[e[i].v].push_back(e[i].u);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        for(int i=1;i<n;i++)
        {
            if(dep[e[i].u]<dep[e[i].v])
                swap(e[i].u,e[i].v);
            val[id[e[i].u]]=e[i].c;
        }
        build(1,idx,1);
        while(1)
        {
            scanf("%s",op);
            if(op[0]=='D') break;
            int a,b;
            scanf("%d%d",&a,&b);
            if(op[0]=='Q')
            {
                printf("%d\n",qq(a,b));
            }
            else
            {
                update(id[e[a].u],b,1,idx,1);
            }
        }

    }
    return 0;
}
#include <cstdio> #include <iostream> #include <vector> #define N 30003 #define INF 2147483647 using namespace std; int n,f[N][20],dep[N],siz[N],son[N],top[N],tot,pos[N],w[N]; int Max[N*4],Sum[N*4]; vector <int> to[N]; void dfs1(int x){ siz[x]=1; int sz=to[x].size(); for(int i=0;i<sz;++i){ int y=to[x][i]; if(y==f[x][0])continue; f[y][0]=x; dep[y]=dep[x]+1; dfs1(y); siz[x]+=siz[y]; if(siz[y]>siz[son[x]])son[x]=y; } } void dfs2(int x,int root){ top[x]=root; pos[x]=++tot; if(son[x])dfs2(son[x],root); int sz=to[x].size(); for(int i=0;i<sz;++i){ int y=to[x][i]; if(y==f[x][0] || y==son[x])continue; dfs2(y,y); } } void update(int k,int l,int r,int P,int V){ if(l==r){ Max[k]=Sum[k]=V; return; } int mid=(l+r)>>1; if(P<=mid)update(k*2,l,mid,P,V); else update(k*2+1,mid+1,r,P,V); Max[k]=max(Max[k*2],Max[k*2+1]); Sum[k]=Sum[k*2]+Sum[k*2+1]; } void up(int &x,int goal){ for(int i=15;i>=0;--i) if(dep[f[x][i]]>=goal)x=f[x][i]; } int lca(int x,int y){ if(dep[x]>dep[y])up(x,dep[y]); if(dep[x]<dep[y])up(y,dep[x]); if(x==y)return x; for(int i=15;i>=0;--i) if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i]; return f[x][0]; } int getm(int k,int l,int r,int L,int R){ if(L<=l && r<=R)return Max[k]; int res=-INF,mid=(l+r)>>1; if(L<=mid)res=max(res,getm(k*2,l,mid,L,R)); if(R>mid)res=max(res,getm(k*2+1,mid+1,r,L,R)); return res; } int gets(int k,int l,int r,int L,int R){ if(L<=l && r<=R)return Sum[k]; int res=0,mid=(l+r)>>1; if(L<=mid)res+=gets(k*2,l,mid,L,R); if(R>mid)res+=gets(k*2+1,mid+1,r,L,R); return res; } int main(){ scanf("%d",&n); for(int i=1,a,b;i<n;++i){ scanf("%d%d",&a,&b); to[a].push_back(b); to[b].push_back(a); } dep[1]=1; dfs1(1); dfs2(1,1); for(int i=1;i<=15;++i) for(int j=1;j<=n;++j)f[j][i]=f[f[j][i-1]][i-1]; for(int i=1;i<=n;++i){ scanf("%d",&w[i]); update(1,1,n,pos[i],w[i]); } int q; scanf("%d",&q); while(q--){ char s[10]; int u,v,t; scanf("%s",s); if(s[1]=='H'){ scanf("%d%d",&u,&t); w[u]=t; update(1,1,n,pos[u],t); } if(s[1]=='M'){ scanf("%d%d",&u,&v); int ans=-INF,t=lca(u,v); for(int i=u;i;i=f[top[i]][0]) if(dep[t]<dep[top[i]]) ans=max(ans,getm(1,1,n,pos[top[i]],pos[i])); else{ ans=max(ans,getm(1,1,n,pos[t],pos[i])); break; } for(int i=v;i;i=f[top[i]][0]) if(dep[t]<dep[top[i]]) ans=max(ans,getm(1,1,n,pos[top[i]],pos[i])); else{ ans=max(ans,getm(1,1,n,pos[t],pos[i])); break; } printf("%d\n",ans); } if(s[1]=='S'){ scanf("%d%d",&u,&v); int ans=0,t=lca(u,v); for(int i=u;i;i=f[top[i]][0]) if(dep[t]<dep[top[i]]) ans+=gets(1,1,n,pos[top[i]],pos[i]); else{ ans+=gets(1,1,n,pos[t],pos[i]); break; } for(int i=v;i;i=f[top[i]][0]) if(dep[t]<dep[top[i]]) ans+=gets(1,1,n,pos[top[i]],pos[i]); else{ ans+=gets(1,1,n,pos[t],pos[i]); break; } printf("%d\n",ans-w[t]); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值