hdu 2763 Housewife Wind

Housewife Wind

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 14271 Accepted: 3950

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

思路: lca+ 求到根的距离+树状数组区间更新,单点查询。

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;

const int N =1e5+5; /// 节点的个数
int rmq[N*2]; /// 就是欧拉序列对应的深度序列

struct ST
{
    int mm[N*2];
    int anc[2*N][20];
    void init(int n)
    {
        mm[0]=-1;
        for(int i=1;i<=n;i++){
            mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
            anc[i][0]=i;
        }
        for(int j=1;j<=mm[n];j++){
            for(int i=1;i+(1<<j)-1<=n;i++){
                anc[i][j]=rmq[anc[i][j-1]] < rmq[anc[i+(1<<(j-1))][j-1]]?anc[i][j-1]:anc[i+(1<<(j-1))][j-1];
            }
        }
    }

    int query(int a,int b)
    {
        if(a>b) swap(a,b);
        int k=mm[b-a+1];
        return rmq[anc[a][k]] <= rmq[anc[b-(1<<k)+1][k]]?anc[a][k]:anc[b-(1<<k)+1][k];
    }
};

struct node
{
    int v,next;
    int id;
    ll w;
}edge[N*2];

int tot,head[N];
int dfns[N*2]; ///  欧拉序列,也就是dfs序,  长度为2*n-1, 下标从1 开始.
int pp[N];  /// pp[i] 表示在dfns 中第一次出现的位置
int L[N],R[N]; /// 表示当前节点dfs序中管辖的区间看情况使用
int cnt;
int clo; /// 时钟标记 用于L,R
ST st;

int n,m,ss;
ll ww[N];
ll c[N];
int to[N];

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(c,0,sizeof(c));
    clo=0; cnt=0;
}

void add(int u,int v,ll w,int id)
{
    edge[++tot].v=v; edge[tot].id=id; edge[tot].w=w;
    edge[tot].next=head[u]; head[u]=tot;
}

void dfs(int u,int fa,int deep)
{
    //cout<<"u "<<u<<" fa "<<fa<<" deep "<<deep<<endl;
    dfns[++cnt]=u; rmq[cnt]=deep; pp[u]=cnt;
    L[u]=++clo;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fa) continue;
        to[edge[i].id]=v;
        dfs(v,u,deep+1);
        dfns[++cnt]=u;
        rmq[cnt]=deep;
    }
    R[u]=clo;
}

void LCA_init(int rt,int node_num)
{
    cnt=0; clo=0;
    dfs(rt,rt,0);
    st.init(2*node_num-1);
}

int LCA(int u,int v)
{
    return dfns[st.query(pp[u],pp[v])];
}

///  init 初始化边, LCA_init() 初始化lca

int lowbit(int x)
{
    return x&(-x);
}

void update(int x,ll w)
{
    for(;x<=n;x+=lowbit(x)) c[x]+=w;
}

ll getsum(int x)
{
    ll s=0;
    for(;x>0;x-=lowbit(x)) s+=c[x];
    return s;
}

ll getans(int u,int v)
{
    ll sum1,sum2,sum3;
    int lca=LCA(u,v);
    sum1=getsum(L[u]);
    sum2=getsum(L[v]);
    sum3=getsum(L[lca]);
    return sum1+sum2-2*sum3;
}

int main()
{
    int u,v;
    ll w;
    while(scanf("%d %d %d",&n,&m,&ss)!=EOF)
    {
        init();
        for(int i=1;i<n;i++){
            scanf("%d %d %lld",&u,&v,&w);
            add(u,v,w,i);
            add(v,u,w,i);
            ww[i]=w;
        }
        LCA_init(1,n);
        for(int i=1;i<n;i++){
            int tmp=to[i];
            update(L[tmp],ww[i]);
            update(R[tmp]+1,-ww[i]);
        }
        int op;
        while(m--)
        {
            scanf("%d",&op);
            if(op==0){
                scanf("%d",&v);
                ll ans=getans(ss,v);
                printf("%lld\n",ans);
                ss=v;
            }
            else{
                scanf("%d %lld",&u,&w);
                int tmp=to[u];
                update(L[tmp],w-ww[u]);
                update(R[tmp]+1,ww[u]-w);
                ww[u]=w;
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值