POJ 2763 Housewife Wind 树链剖分裸题

来源:互联网 发布:linux vncserver 安装 编辑:程序博客网 时间:2024/05/18 17:44

Description

After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.

Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: ‘Mummy, take me home!’

At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.

Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?


Input

The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.

The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.

The following q lines each is one of the following two types:

Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.


Output

For each message A, print an integer X, the time required to take the next child.


Sample Input

3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3


Sample Output

1
3


直接边权下放树链剖分裸题,强制动态,涉及点修改区间和查询,我用的线段树维护

#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>const int MAXN = 400000;using namespace std;int readin(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9')ch=getchar();    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int n,q,current,opt,tail,head[MAXN],a,b,w,point[MAXN];int dep[MAXN],fa[MAXN],siz[MAXN],son[MAXN],tid[MAXN],timer,top[MAXN],rank[MAXN];long long sum[MAXN*4],val[MAXN];struct Line{int from,to,nxt,v;}line[MAXN*2];void add_line(int from,int to,int v){    tail++;    line[tail].from=from;    line[tail].to=to;    line[tail].nxt=head[from];    line[tail].v=v;    head[from]=tail;}void add(int a,int b,int v){add_line(a,b,v);add_line(b,a,v);}void dfs1(int u,int father,int d){    dep[u]=d;    fa[u]=father;    siz[u]=1;    for(register int i=head[u];i;i=line[i].nxt){        int v=line[i].to;        if(v==father) continue;        val[v]=line[i].v;        dfs1(v,u,d+1);        siz[u]+=siz[v];        if(son[u]==-1||siz[son[u]]<siz[v]) son[u]=v;    }}void dfs2(int u,int tp){    tid[u]=++timer;    top[u]=tp;    rank[tid[u]]=u;    if(son[u]==-1) return;    dfs2(son[u],tp);    for(register int i=head[u];i;i=line[i].nxt){        int v=line[i].to;        if(v==fa[u]||v==son[u]) continue;        dfs2(v,v);    }}void pushup(int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void build(int l,int r,int rt){    if(l==r){        sum[rt]=val[rank[l]];        return;    }    int mid=(l+r)>>1;    build(l,mid,rt<<1);    build(mid+1,r,rt<<1|1);    pushup(rt);}void modify(int pos,long long v,int l,int r,int rt){    if(l==r){        sum[rt]=v;        return;    }    int mid=(l+r)>>1;    if(pos<=mid) modify(pos,v,l,mid,rt<<1);    else         modify(pos,v,mid+1,r,rt<<1|1);    pushup(rt);}long long query(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r){        return sum[rt];    }    int mid=(l+r)>>1;    long long ans=0;    if(L<=mid) ans+=query(L,R,l,mid,rt<<1);    if(R>mid)  ans+=query(L,R,mid+1,r,rt<<1|1);    return ans;}long long _query(int from,int to){    long long ans=0;    while(top[from]!=top[to]){        if(dep[top[from]]<dep[top[to]]) swap(from,to);        ans+=query(tid[top[from]],tid[from],1,n,1);        from=fa[top[from]];    }    if(dep[from]<dep[to]) swap(from,to);    if(from!=to) ans+=query(tid[son[to]],tid[from],1,n,1);    return ans;}int main(){    freopen("POJ2763.txt","r",stdin);    //freopen(".out","w",stdout);    n=readin();q=readin();current=readin();     memset(son,-1,sizeof(son));    for(register int i=1;i<=n-1;i++){        a=readin();        b=readin();        w=readin();        add(a,b,w);    }    dfs1(1,0,0);    dfs2(1,1);    build(1,n,1);    for(register int i=1;i<=tail;i+=2){        if(dep[line[i].from]>dep[line[i].to]) point[i/2+1]=line[i].from;        else point[i/2+1]=line[i].to;    }    for(register int i=1;i<=q;i++){        scanf("%d",&opt);        if(opt==0){            scanf("%d",&a);            printf("%lld\n",_query(current,a));            current=a;        }else{            long long v;            scanf("%d%lld",&a,&v);            modify(tid[point[a]],v,1,n,1);        }    }    return 0;}

这里写图片描述