poj 2763(树链剖分维护边权模版题)

Housewife Wind

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 16709 Accepted: 4561

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

题意:给一棵树,树的边有边权。有两种操作,询问一个位置到另一个位置的路径长度,或是更改第 i 条边的边权。

思路:树剖边权模版题。。   相对于点权,是把边权放在  深度较深的那个点,然后跑点权的树剖,唯一不同的是,在利用重链的函数里面,在最后的时候需要先判断 x 与 y 是否是同一点,然后把 深度浅的那个点移向他的重儿子。

Code:
 

#include<cstdio>
#include<algorithm>
#include<iostream>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 1e6 + 10;
int head[maxn],cnt = 0;
struct xx{
    int u,v,w,nex;
    xx(int u = 0,int v = 0,int w = 0,int nex = 0):
        u(u),v(v),w(w),nex(nex){};
}edge[maxn << 1];

int Hson[maxn],dep[maxn],Size[maxn];
int fa[maxn],dfsx[maxn],rk[maxn];
int top[maxn],val[maxn];
///先dfs1,再dfs2, 如根节点为 r, 则 dfs1(r,0);  dfs2(r,r);
void dfs1(int p,int f){ /// 处理fa[],dep[],Size[],Hson[]
    Size[p] = 1;
    fa[p] = f;
    dep[p] = dep[f] + 1;
    for(int i = head[p]; ~i;i = edge[i].nex){
        int v = edge[i].v;
        if(v == f) continue;
        dfs1(v,p);
        Size[p] += Size[v];
        if(Size[Hson[p]] < Size[v])
            Hson[p] = v;
    }
}

int xu = 0;
void dfs2(int p,int tp){ /// 处理dfs序,top[],dfsx[]
    rk[p] = ++ xu;
    top[p] = tp;
    dfsx[xu] = p;
    if(Hson[p])         ///保证重链的dfs序连续
        dfs2(Hson[p],tp);
    for(int i = head[p]; ~i;i = edge[i].nex){
        int v = edge[i].v;
        if(v != fa[p] && v != Hson[p])
            dfs2(v,v);
    }
}

LL sum[maxn << 2];
LL query(int i,int l,int r,int ql,int qr){
    if(ql <= l && r <= qr){
        return sum[i];
    }
    LL ans = 0;
    int mid = (l + r) >> 1;
    if(ql <= mid) ans += query(ls,l,mid,ql,qr);
    if(qr > mid) ans += query(rs,mid + 1,r,ql,qr);
    return ans;
}

LL Allquery(int x,int y){
    LL ans = 0;
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) swap(x,y);
        ans += query(1,1,xu,rk[top[x]],rk[x]);
        x = fa[top[x]];
    }
    if(x == y) return ans;
    if(rk[x] > rk[y]) swap(x,y);
    ans += query(1,1,xu,rk[Hson[x]],rk[y]);
    return ans;
}

void update(int i,int l,int r,int pos,int VAL){
    if(l == r){
        sum[i] = (LL) VAL;
        return ;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) update(ls,l,mid,pos,VAL);
    if(pos > mid) update(rs,mid + 1,r,pos,VAL);
    sum[i] = sum[ls] + sum[rs];
}

void init(int n){
    for(int i = 0;i < n;++ i)
        head[i] = -1;
    for(int i = 0;i < (n << 2);++ i)
        sum[i] = 0;
    for(int i = 0;i < n;++ i)
        Size[i] = 0;
    xu = 0;
    cnt = 1;
}

int main(){
    int n,q,s;
    while(~scanf("%d%d%d",&n,&q,&s)){
        init(n + 5);
        for(int i = 1;i < n;++ i){
            int u,v,w; scanf("%d%d%d",&u,&v,&w);
            edge[cnt] = xx(u,v,w,head[u]);
            head[u] = cnt ++;
            edge[cnt] = xx(v,u,w,head[v]);
            head[v] = cnt ++;
        }
        dfs1(s,0); dfs2(s,s);
        for(int i = 1;i < cnt;++ i){
            int u = edge[i].u;
            int v = edge[i].v;
            if(dep[u] > dep[v])
                update(1,1,xu,rk[u],edge[i].w);
            else update(1,1,xu,rk[v],edge[i].w);
        }
        while(q --){
            int op; scanf("%d",&op);
            if(!op){
                int u; scanf("%d",&u);
                printf("%lld\n",Allquery(s,u));
                s = u;
            }
            else {
                int i,w; scanf("%d%d",&i,&w);
                i = 2 * i - 1;
                int pos = dep[edge[i].u] > dep[edge[i].v] ? edge[i].u : edge[i].v;
                pos = rk[pos];
                update(1,1,xu,pos,w);
            }
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值