POJ 2763 Housewife Wind 两种解法

Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 9637 Accepted: 2642

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

【题意】给一个数,边之间有权值,然后两种操作,第一种:求任意两点的权值和,第二,修改树上两点的权值。

【解题方法1】树剖。

【AC 代码1】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
const int inf =1e9;
struct edge{
    int to,next;
}E[maxn*3];
int head[maxn],tot,tim;
int e[maxn][3];
int num[maxn];
int siz[maxn],top[maxn],son[maxn];
int dep[maxn],Rank[maxn],tid[maxn],fa[maxn];
void init()
{
    tot=tim=0;
    memset(head,-1,sizeof(head));
    memset(son,-1,sizeof(son));
}
void addedge(int u,int v){
    E[tot].to=v,E[tot].next=head[u],head[u]=tot++;
    E[tot].to=u,E[tot].next=head[v],head[v]=tot++;
}
void dfs1(int u,int father,int d)
{
    dep[u]=d;
    fa[u]=father;
    siz[u]=1;
    for(int i=head[u]; ~i; i=E[i].next){
        int v=E[i].to;
        if(v!=father){
            dfs1(v,u,d+1);
            if(son[u]==-1||siz[v]>siz[son[u]])
                son[u]=v;
        }
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    tid[u]=++tim;
    Rank[tid[u]]=u;
    if(son[u]==-1) return ;
    dfs2(son[u],tp);
    for(int i=head[u]; ~i; i=E[i].next){
        int v=E[i].to;
        if(v!=fa[u]&&v!=son[u]){
            dfs2(v,v);
        }
    }
}
struct node{
    int l,r,sum;
}Tree[maxn<<2];
void pushup(int rt)
{
    Tree[rt].sum=Tree[rt*2].sum+Tree[rt*2+1].sum;
}
void Build(int l,int r,int rt)
{
    Tree[rt].l=l,Tree[rt].r=r;
    if(l==r){
        Tree[rt].sum=num[l];
        return ;
    }
    int m=(l+r)/2;
    Build(l,m,rt*2);
    Build(m+1,r,rt*2+1);
    pushup(rt);
}
void update(int pos,int val,int rt)
{
    if(Tree[rt].l==pos&&Tree[rt].r==pos){
        Tree[rt].sum=val;
        return ;
    }
    int mid=(Tree[rt].l+Tree[rt].r)/2;
    if(pos<=mid) update(pos,val,rt*2);
    else         update(pos,val,rt*2+1);
    pushup(rt);
}
int queryans(int L,int R,int rt)
{
    if(L==Tree[rt].l&&Tree[rt].r==R){
        return Tree[rt].sum;
    }
    int mid=(Tree[rt].l+Tree[rt].r)/2;
    if(R<=mid) return queryans(L,R,rt*2);
    else if(L>mid) return queryans(L,R,rt*2+1);
    else{
        return queryans(L,mid,rt*2)+queryans(mid+1,R,rt*2+1);
    }
}

int querysum(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        ans+=queryans(tid[top[x]],tid[x],1);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    if(x!=y) ans+=queryans(tid[x]+1,tid[y],1);
    return ans;
}

int main()
{
    int n,q,s;
    while(~scanf("%d%d%d",&n,&q,&s)){
        init();
        for(int i=1; i<n; i++){
            scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]);
            addedge(e[i][0],e[i][1]);
        }
//        dfs1(1,0,0);
//        dfs2(1,1);
//        Build(0,tim,1);
//        for(int i=0; i<n-1; i++){
//            if(dep[e[i][0]]>dep[e[i][1]]) swap(e[i][0],e[i][1]);
//
//        }
        dfs1(1,0,1);
        dfs2(1,1);
        for(int i=1; i<n; i++)
        {
            if(dep[e[i][0]]<dep[e[i][1]]) swap(e[i][0],e[i][1]);
            num[tid[e[i][0]]]=e[i][2];
        }
        Build(1,tim,1);
//        puts("success");
        int op,x,y;
        for(int i=0; i<q; i++)
        {
            scanf("%d",&op);
            if(op==0){
                scanf("%d",&x);
                printf("%d\n",querysum(s,x));
                s=x;
            }
            else{
                scanf("%d%d",&x,&y);
                update(tid[e[x][0]],y,1);
            }
        }
    }
}

【解题方法2】LCA+BIT,具体可以参见《挑战程序设计》P332。

【AC 代码】

//
//Created By just_sort 2016/9/3
//All rights reserved
//POJ.2763 Housewife Wind
//

#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100005;
const int maxm = 200005;
int n,q,s;
int vs[maxm],es[maxm],dep[maxm],first[maxn],dfsnum,c[maxn];

int getmin(int x,int y)
{
    return dep[x]<dep[y]?x:y;
}

struct SparseTable{
    int dp[20][maxm];
    void Init(int n){
        for(int i=1; i<=n; i++) dp[0][i]=i;
        for(int i=1; (1<<i)<=n; i++)
            for(int j=1; j+(1<<i)-1<=n; j++)
                dp[i][j]=getmin(dp[i-1][j],dp[i-1][j+(1<<(i-1))]);
    }
    int RMQ(int l,int r)
    {
        int k=31-__builtin_clz(r-l+1);
        return getmin(dp[k][l],dp[k][r-(1<<k)+1]);
    }
}st;

struct BIT{
    int b[maxm],n;
    void init(int _n){
        n=_n;memset(b,0,sizeof(b));
    }
    void update(int i,int v){
        while(i<=n){
            b[i]+=v;
            i+=i&-i;
        }
    }
    int getans(int i){
        int ret=0;
        while(i){
            ret+=b[i];
            i-=i&-i;
        }
        return ret;
    }
}bit;

int head[maxn],tot;
struct edge{
    int id,v,cost;
    edge(){}
    edge(int id,int v,int cost):id(id),v(v),cost(cost){}
};
vector<edge>G[maxn];
void dfs(int u,int f,int d)
{
    vs[++dfsnum]=u;
    dep[dfsnum]=d;
    first[u]=dfsnum;
    for(int i=0; i<(int)G[u].size(); i++){
        edge e=G[u][i];
        if(e.v==f) continue;
        es[2*e.id-1]=dfsnum;
        bit.update(dfsnum,e.cost);
        dfs(e.v,u,d+1);
        vs[++dfsnum]=u;
        dep[dfsnum]=d;
        es[2*e.id]=dfsnum;
        bit.update(dfsnum,-e.cost);
    }
}

void init(){
    dfsnum=0;
    bit.init(2*(n-1));
    dfs(1,-1,0);
    st.Init(2*n-1);
}

int lca(int u,int v)
{
    if(first[u]>first[v]) swap(u,v);
    int idx=st.RMQ(first[u],first[v]);
    return vs[idx];
}

int main()
{
    while(scanf("%d%d%d",&n,&q,&s)!=EOF)
    {
        int i,u,w,v;
        for(int i=1; i<=n; i++) G[i].clear();
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%d",&u,&v,&c[i]);
            G[u].push_back(edge(i,v,c[i]));
            G[v].push_back(edge(i,u,c[i]));
        }
        init();
        while(q--)
        {
            int op;
            scanf("%d",&op);
            if(op){
                scanf("%d%d",&i,&w);
                bit.update(es[2*i-1],w-c[i]);
                bit.update(es[2*i],c[i]-w);
                c[i]=w;
            }
            else{
                scanf("%d",&v);
                int _lca=lca(s,v);
                printf("%d\n",bit.getans(first[s]-1)+bit.getans(first[v]-1)-2*bit.getans(first[_lca]-1));
                s=v;
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值