POJ 2763 Housewife Wind (LCA+线段树)

来源:互联网 发布:发哥 知乎 编辑:程序博客网 时间:2024/05/21 09:30

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 11554 Accepted: 3173

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 11 2 12 3 20 21 2 30 3

Sample Output

13

Source

POJ Monthly--2006.02.26,zgl & twb



        好久没有做树的题了……

        这题就是一棵树,给出两种操作,一是求两点最近距离,二是修改对应边的权值(是修改而不是更新)。

        其实呢,这题用很多方法都可以做,什么树链剖分、LCT都行,但是我还是用了线段树,一是最近这个模板用的比较熟悉,而是练练LCA的写法。具体的话,首先在LCA预处理的时候,顺便把每个点的dfs序给求出来。之后求出被修改边的改变量,对其子树的所有点区间增加这个改变量,就达到了这个要求。求最短路的时候,直接d[x]+d[y]+d[lca(x,y)]即可。

        虽说思路简单明了,但是实际写的时候却错了好几次,修改的时候的序列是用dfs序,然后初始的还要弄个反id数组……这些具体就见代码吧,我也不想说什么了:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstdlib>#include<cstring>#include<cmath>#include<iomanip>#include<vector>#include<queue>#define LL int#define MAX_V 201000using namespace std;int n,m,s,num,id[MAX_V],sz[MAX_V],d[MAX_V],Rid[MAX_V];LL a[MAX_V];namespace LCA{    struct edge    {        int x,y,w,nxt;    } g[MAX_V*2];    struct node    {        int x,y,nxt,lca;    } q[MAX_V*2];    int f[MAX_V],ls[MAX_V],lq[MAX_V];    int e,p; bool v[MAX_V];    void init()    {        e=p=num=0;        for(int i=1;i<=n;i++)            f[i]=i,ls[i]=d[i]=lq[i]=v[i]=0;        memset(q,0,sizeof(q));    }    int find(int x)    {        if (f[x]==x) return x;        f[x]=find(f[x]);        return f[x];    }    inline void addquery(int x,int y)    {        q[++p].x=x;q[p].y=y;        q[p].nxt=lq[x];        lq[x]=p;        q[++p].y=x;q[p].x=y;        q[p].nxt=lq[y];        lq[y]=p;    }    inline void addedge(int x,int y,int w)    {        g[++e].x=x;        g[e].y=y;        g[e].w=w;        g[e].nxt=ls[x];        ls[x]=e;    }    inline void lca(int s,int dist)    {        f[s]=s;        v[s]=1;        sz[s]=++num;        id[s]=num;//lca预处理的时候顺便获取id号、反id号以及最大后代节点编号sz        Rid[num]=s;        d[s]=dist;        for(int i=ls[s];i;i=g[i].nxt)            if (!v[g[i].y])            {                lca(g[i].y,g[i].w+dist);                sz[s]=max(sz[s],sz[g[i].y]);                f[find(g[i].y)]=find(s);            }        for(int i=lq[s];i;i=q[i].nxt)            if (v[q[i].y]) q[i].lca=f[find(q[i].y)];    }}struct ST{struct node{LL sum,lazy;int l,r;} tree[MAX_V<<2];inline void push_up(int i){tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;}inline void build(int i,int l,int r){tree[i].r=r;tree[i].l=l;tree[i].lazy=0;if (l==r){tree[i].sum=d[Rid[l]];//记得使用反id!!!return;}LL mid=(l+r)>>1;build(i<<1,l,mid);build(i<<1|1,mid+1,r);push_up(i);}inline void push_down(int i){tree[i<<1].sum+=(tree[i<<1].r-tree[i<<1].l+1)*tree[i].lazy;tree[i<<1|1].sum+=(tree[i<<1|1].r-tree[i<<1|1].l+1)*tree[i].lazy;tree[i<<1].lazy+=tree[i].lazy; tree[i<<1|1].lazy+=tree[i].lazy; tree[i].lazy=0;}inline void update(int i,int l,int r,LL x){if ((tree[i].l==l)&&(tree[i].r==r)){tree[i].sum+=(r-l+1)*x;tree[i].lazy+=x; return;}if (tree[i].lazy!=0) push_down(i);LL mid=(tree[i].l+tree[i].r)>>1;if (mid>=r) update(i<<1,l,r,x);else if (mid<l) update(i<<1|1,l,r,x);else{update(i<<1,l,mid,x);update(i<<1|1,mid+1,r,x);}push_up(i);}inline LL getsum(int i,int l,int r){if ((tree[i].l==l)&&(tree[i].r==r)) return tree[i].sum;if (tree[i].lazy) push_down(i);LL mid=(tree[i].l+tree[i].r)>>1;if (mid>=r) return getsum(i<<1,l,r);else if (mid<l) return getsum(i<<1|1,l,r);else return getsum(i<<1,l,mid)+getsum(i<<1|1,mid+1,r);}} seg;struct query{    int op,x,y;} q[MAX_V];int main(){    while(~scanf("%d%d%d",&n,&m,&s))    {        LCA::init();        for(int i=1;i<n;i++)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            LCA::addedge(u,v,w);            LCA::addedge(v,u,w);        }        for(int i=1;i<=m;i++)        {            scanf("%d",&q[i].op);            if (q[i].op==0)            {                scanf("%d",&q[i].x);                LCA::addquery(q[i].x,s);                q[i].y=LCA::p; s=q[i].x;            } else scanf("%d%d",&q[i].x,&q[i].y);        }        LCA::lca(1,0);        seg.build(1,1,n);        for(int i=1;i<=m;i++)        {            int u,v,a,b,c;            if (q[i].op==1)            {                u=LCA::g[q[i].x*2].x;                v=LCA::g[q[i].x*2].y;                if (id[u]>id[v]) swap(u,v);                a=seg.getsum(1,id[u],id[u]);                b=seg.getsum(1,id[v],id[v]);                c=q[i].y-(b-a);//求出边权改变量                seg.update(1,id[v],sz[v],c);            } else            {                u=LCA::q[q[i].y].x; v=LCA::q[q[i].y].y;                int lca=max(LCA::q[q[i].y-1].lca,LCA::q[q[i].y].lca);                a=seg.getsum(1,id[u],id[u]);                b=seg.getsum(1,id[v],id[v]);                c=seg.getsum(1,id[lca],id[lca]); printf("%d\n",a+b-2*c);            }        }    }    return 0;}

        最后的最后,我要立个flag,这题在实习期间我要用其他两种姿势A了它!

原创粉丝点击