POJ 2763 -- Housewife Wind 树链剖分 LCT 动态树

Housewife  Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 7479 Accepted: 1938

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

题目:求路径上的边权和,

也可以修改某条边的边权。


分析:

对每个点建立一个结点,值为0

对每个边建立一个结点,值为边权。

然后用LCT维护即可


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define maxn 500007
#define inf  1000000000
#define ll int

struct Node{
    Node *fa,*ch[2];
    bool rev,root;
    int val;
    ll minv;
};
Node pool[maxn];
Node *nil,*tree[maxn];
int cnt = 0;
void init(){
    cnt = 1;
    nil = tree[0] = pool;
    nil->ch[0] = nil->ch[1] = nil;
    nil->val = 0;
    nil->minv = 0;
}
Node *newnode(int val,Node *f){
    pool[cnt].fa = f;
    pool[cnt].ch[0]=pool[cnt].ch[1]=nil;
    pool[cnt].rev = false;
    pool[cnt].root = true;
    pool[cnt].val = val;
    pool[cnt].minv = val;
    return &pool[cnt++];
}

//左右子树反转******真正把结点变为根
void update_rev(Node *x){
    if(x == nil) return ;
    x->rev = !x->rev;
    swap(x->ch[0],x->ch[1]);
}
//splay向上更新信息******
void update(Node *x){
    if(x == nil) return ;
    x->minv = x->val;
    if(x->ch[0] != nil)
        x->minv += x->ch[0]->minv;
    if(x->ch[1] != nil)
        x->minv += x->ch[1]->minv;
}

//splay下推信息******
void pushdown(Node *x){
    if(x->rev != false){
        update_rev(x->ch[0]);
        update_rev(x->ch[1]);
        x->rev = false;
    }
}
//splay在root-->x的路径下推信息******
void push(Node *x){
    if(!x->root) push(x->fa);
    pushdown(x);
}
//将结点x旋转至splay中父亲的位置******
void rotate(Node *x){
    Node *f = x->fa, *ff = f->fa;
    int t = (f->ch[1] == x);
    if(f->root)
        x->root = true, f->root = false;
    else ff->ch[ff->ch[1] == f] = x;
    x->fa = ff;
    f->ch[t] = x->ch[t^1];
    x->ch[t^1]->fa = f;
    x->ch[t^1] = f;
    f->fa = x;
    update(f);
}
//将结点x旋转至x所在splay的根位置******
void splay(Node *x){
    push(x);
    Node *f, *ff;
    while(!x->root){
        f = x->fa,ff = f->fa;
        if(!f->root)
            if((ff->ch[1]==f)&&(f->ch[1] == x)) rotate(f);
            else rotate(x);
        rotate(x);
    }
    update(x);
}
//将x到树根的路径并成一条path******
Node *access(Node *x){
    Node *y = nil,*z;
    while(x != nil){
        splay(x);
        x->ch[1]->root = true;
        (x->ch[1] = y)->root = false;
        update(x);
        y = x;
        x = x->fa;
    }
    return y;
}
//将结点x变成树根******
void be_root(Node *x){
    access(x);
    splay(x);
    update_rev(x);
}
//将x连接到结点f上******
void link(Node *x, Node *f){
    be_root(x);
    x->fa = f;
}
Node * find(Node *root){
    if(root->ch[0] == nil) return root;
    return find(root->ch[0]);
}
int ru[maxn],rv[maxn];
int main(){
    int n,q,s;
    Node*x,*y,*z;
    while(scanf("%d%d%d",&n,&q,&s)!=EOF){
        init();
        int u,v,t;
        for(int i = 1;i <= n; i++)
            tree[i] = newnode(0,nil);
        for(int i = 1;i < n ;i++){
            scanf("%d%d%d",&u,&v,&t);
            x = newnode(t,nil);
            link(tree[u],x);
            link(tree[v],x);
            ru[i] = u;
            rv[i] = v;
        }
        while(q--){
            scanf("%d",&t);
            if(t == 0){
                scanf("%d",&u);
                be_root(tree[s]);
                y = access(tree[u]);
                printf("%d\n",y->minv);
                s = u;
            }
            else {
                scanf("%d%d",&u,&v);
                be_root(tree[ru[u]]);
                access(tree[rv[u]]);
                splay(tree[ru[u]]);
                y = find(tree[ru[u]]->ch[1]);
                y->val = v;
                update(y);
                splay(y);
            }
        }
    }

    return 0;
}

/*
3 100 1
1 2 1
2 3 2
0 2
1 2 3
0 3
1 2 5
0 2

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值