poj 2763 Housewife Wind 【LCA 与 树状数组】

13 篇文章 0 订阅
11 篇文章 0 订阅

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 10609 Accepted: 2943

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

Source



求树上两点间的距离,每条边的大小可以变化,所以可以将树转化为一个链,然后再链上可以用树状数组求两点距离,


对于任意两个点A , B之间的距离=LCA ( A , B ) 到A和B的距离之和---------- (LCA可以用线段树)。


有一点需要注意---标记一下每条边的两次使用---(更新边时需要用)



错误代码:

#include<cstdio>
#include<vector>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int lowit(int x) {return x&(-x);}
struct node{int to,cost;}Q;
vector<node>V[100100];
int a[100100],b[100100],c[100100],wx[100100][2],tree[400200],fx[300200];
struct nn{int l,r,mi;}PP[1500000];
bool fafe[200100];int ll,n;
void add_edge(int x,int y,int z)//guo
{
    Q.to=y;Q.cost=z;
    V[x].push_back(Q);
    Q.to=x;
    V[y].push_back(Q);
}
void add(int x,int y)//
{
    for (;x<=n;x+=lowit(x))
        tree[x]+=y;
}
int query(int x)//
{
    int ans=0;
    for (;x;x-=lowit(x))
        ans+=tree[x];
    return ans;
}
void create(int l,int r,int x)
{
    PP[x].l=l;PP[x].r=r;
    if (l==r)
    {
        PP[x].mi=wx[fx[l]][0];
        return ;
    }
    int m=(l+r)>>1;
    create(l,m,x*2);create(m+1,r,x*2+1);
    PP[x].mi=min(PP[x*2].mi,PP[x*2+1].mi);
}
int LCA(int l,int r,int x)
{
    if (l==PP[x].l&&r==PP[x].r) return PP[x].mi;
    int m=(PP[x].l+PP[x].r)>>1;
    if (l>m)
        return LCA(l,r,x*2+1);
    else if (r<=m)
        return LCA(l,r,x*2);
    else
        return min(LCA(l,m,x*2),LCA(m+1,r,x*2+1));
}
void dfs(int x)
{
    fafe[x]=false;
    wx[x][0]=++ll;
    fx[ll]=x;
    for (int i=0;i<V[x].size();i++)
    {
        if (fafe[V[x][i].to])
        {
            add(ll,V[x][i].cost);
            dfs(V[x][i].to);
            add(ll,-V[x][i].cost);
            wx[x][1]=++ll;fx[ll]=x;
        }
    }
    wx[x][1]=ll;fx[ll]=x;
}
int main()
{
    int m,s;
    scanf("%d%d%d",&n,&m,&s);
    for (int i=1;i<n;i++)
    {
        scanf("%d%d%d",&a[i],&b[i],&c[i]);
        add_edge(a[i],b[i],c[i]);
    }
    int q,x,y,z;
    if (n==1)
    {
        while (m--)
        {
            scanf("%d",&q);
            if (q)
            {
                scanf("%d%d",&x,&y);
                printf("0\n");
            }
            else
            {
                printf("%d",&x);
                printf("0\n");
            }
        }
        return 0;
    }
    ll=0;
    memset(fafe,true,sizeof(fafe));
    memset(tree,0,sizeof(tree));
    n=2*n-2;dfs(1);
    create(1,n,1);
    while (m--)
    {
        scanf("%d",&q);
        if (q)
        {
            scanf("%d%d",&x,&y);
            z=y-c[x];c[x]=y;
            q=x;x=a[q];y=b[q];
            if (wx[x][0]>wx[y][0])
            {
                q=x;x=y;y=q;
            }
            add(wx[x][0],z);
            add(wx[y][1],-z);
        }
        else
        {
            scanf("%d",&y);
            x=s;s=y;
            x=wx[x][0];
            y=wx[y][0];
            if (x<y)
                z=LCA(x,y,1);
            else
                z=LCA(y,x,1);
            int ans=query(x-1)-2*query(z-1)+query(y-1);
            printf("%d\n",ans);
        }
    }
    return 0;
}



正确代码:

#include<cstdio>
#include<vector>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int lowit(int x) {return x&(-x);}
struct node{int to,cost,hao;}Q;
vector<node>V[100100];
int a[100100],b[100100],c[100100],wx[100100][2],tree[400200],fx[300200],front_bian[101000][2];
struct nn{int l,r,mi;}PP[1500000];
bool fafe[200100];int ll,n;
void add_edge(int x,int y,int z,int i)//guo
{
    Q.to=y;Q.cost=z;Q.hao=i;
    V[x].push_back(Q);
    Q.to=x;
    V[y].push_back(Q);
}
void add(int x,int y)//
{
    for (;x<=n;x+=lowit(x))
        tree[x]+=y;
}
int query(int x)//
{
    int ans=0;
    for (;x;x-=lowit(x))
        ans+=tree[x];
    return ans;
}
void create(int l,int r,int x)
{
    PP[x].l=l;PP[x].r=r;
    if (l==r)
    {
        PP[x].mi=wx[fx[l]][0];
        return ;
    }
    int m=(l+r)>>1;
    create(l,m,x*2);create(m+1,r,x*2+1);
    PP[x].mi=min(PP[x*2].mi,PP[x*2+1].mi);
}
int LCA(int l,int r,int x)
{
    if (l==PP[x].l&&r==PP[x].r) return PP[x].mi;
    int m=(PP[x].l+PP[x].r)>>1;
    if (l>m)
        return LCA(l,r,x*2+1);
    else if (r<=m)
        return LCA(l,r,x*2);
    else
        return min(LCA(l,m,x*2),LCA(m+1,r,x*2+1));
}
void dfs(int x)
{
    fafe[x]=false;
    wx[x][0]=++ll;
    fx[ll]=x;
    for (int i=0;i<V[x].size();i++)
    {
        if (fafe[V[x][i].to])
        {
            front_bian[V[x][i].hao][0]=ll;
            add(ll,V[x][i].cost);
            dfs(V[x][i].to);
            front_bian[V[x][i].hao][1]=ll;
            add(ll,-V[x][i].cost);
            wx[x][1]=++ll;fx[ll]=x;
        }
    }
    wx[x][1]=ll;fx[ll]=x;
}
int main()
{
    int m,s;
    scanf("%d%d%d",&n,&m,&s);
    for (int i=1;i<n;i++)
    {
        scanf("%d%d%d",&a[i],&b[i],&c[i]);
        add_edge(a[i],b[i],c[i],i);
    }
    int q,x,y,z;
    if (n==1)
    {
        while (m--)
        {
            scanf("%d",&q);
            if (q)
            {
                scanf("%d%d",&x,&y);
            }
            else
            {
                printf("%d",&x);
                printf("0\n");
            }
        }
        return 0;
    }
    ll=0;
    memset(fafe,true,sizeof(fafe));
    memset(tree,0,sizeof(tree));
    n=2*n-2;dfs(1);
    create(1,n,1);
    while (m--)
    {
        scanf("%d",&q);
        if (q)
        {
            scanf("%d%d",&x,&y);
            z=y-c[x];c[x]=y;
            add(front_bian[x][0],z);
            add(front_bian[x][1],-z);
        }
        else
        {
            scanf("%d",&y);
            x=s;s=y;
            x=wx[x][0];
            y=wx[y][0];
            if (x<y)
                z=LCA(x,y,1);
            else
                z=LCA(y,x,1);
            int ans=query(x-1)-2*query(z-1)+query(y-1);//树状数组
            printf("%d\n",ans);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值