poj 2763 Housewife Wind 树链剖分

题目要求修改的是第几条边,所以要用一个idx[]数组来存第i条边经过重新编号之后的编号是多少。

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 100010;

struct Edge {
    int to ;
    int id;
    int value;
    Edge * next;
}E[MAXN*2],*EE;

struct Gragh {
    Edge * first;
}G[MAXN];

struct Tree {
    int x,y;
    int value;
    int lazy;
}t[MAXN<<2];

int pre[MAXN],son[MAXN],siz[MAXN],dep[MAXN],top[MAXN],pos[MAXN],Rank[MAXN];
int N,Q,S;
int num[MAXN];
int idx[MAXN];
int tree_idx = 0;
int tot ;

void Init() {
    EE = E;
    dep[1] = 1;
    pre[1] = 0;
    tree_idx = 0;
    memset(G,0,sizeof(G));
    memset(son,0,sizeof(son));
}

void addedge(int u,int v,int value,int id) {
    EE->to = v ; EE->next = G[u].first ; EE->value = value ; EE->id = id ; G[u].first = EE++;
    EE->to = u ; EE->next = G[v].first ; EE->value = value ; EE->id = id ; G[v].first = EE++;
}

void dfs1(int u) {
    siz[u] = 1;
    son[u] = 0;
    for(Edge * p = G[u].first ; p ; p = p -> next) {
        if(p->to != pre[u]) {
            pre[p->to] = u;
            dep[p->to] = dep[u] + 1;
            dfs1(p->to);
            siz[u] += siz[p->to];
            if(siz[p->to] > siz[son[u]])
                son[u] = p -> to;
        }
    }
}

void dfs2(int u,int ancestor) {
    top[u] = ancestor;
    tree_idx ++;
    pos[u] = tree_idx;
    if(son[u] != 0) {
        dfs2(son[u],ancestor);
    }
    for(Edge * p = G[u].first ; p ; p = p -> next) {
        if(p->to != pre[u] && p->to != son[u]) {
            dfs2(p->to,p->to);
            num[pos[p->to]] = p->value;
            idx[p->id] = pos[p->to];
        }
        else if(p->to == son[u]) {
            num[pos[p->to]] = p->value;
            idx[p->id] = pos[p->to];
        }
    }
} 

void Push_Up(int rt) {
    t[rt].value = t[rt<<1].value + t[rt<<1|1].value; 
}

void Build(int x,int y,int rt) {
    t[rt].x = x ; t[rt].y = y; t[rt].lazy = 0; 
    if(x == y) {
        t[rt].value = num[x];
        return ;
    }
    int mid = (x + y) >> 1;
    Build(x,mid,rt<<1);
    Build(mid+1,y,rt<<1|1);
    Push_Up(rt);
}

void Update(int rt,int position,int value) {
    if(t[rt].x == t[rt].y) {
        t[rt].value = value;
        return ;
    }
    int mid = (t[rt].x + t[rt].y) >> 1;
    if(mid >= position) {
        Update(rt<<1,position,value);
    }
    else {
        Update(rt<<1|1,position,value);
    }
    Push_Up(rt);
}

int Query(int rt,int left,int right) {
    if(left <= t[rt].x && right >= t[rt].y) {
        return t[rt].value;
    }
    int ans = 0;
    int mid = (t[rt].x + t[rt].y) >> 1;
    // Push_Down(rt);
    if(mid >= left) {
        ans += Query(rt<<1,left,right);
    } 
    if(mid < right){
        ans += Query(rt<<1|1,left,right);
    }
    return ans;
}

// void Change(int x,int y,int lazy) {
//     //printf("lazy : %d\n",lazy);
//     while(top[x] != top[y]) {
//         if(dep[top[x]] < dep[top[y]]) swap(x,y);
//         Update(1,pos[top[x]],pos[x],lazy);
//         x = pre[top[x]];
//     }
//     if(dep[x] > dep[y]) swap(x,y);
//     Update(1,pos[x],pos[y],lazy);
// }

int Query_Tree(int x,int y) {
    int ans = 0;
    while(top[x] != top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x,y);
        ans += Query(1,pos[top[x]],pos[x]);
        x = pre[top[x]];
    }
    if(x == y) return ans;
    if(dep[x] > dep[y]) swap(x,y);
    ans += Query(1,pos[x]+1,pos[y]);
    return ans;
}

int op;

void print_tree(int rt) {
    printf("x : %d y : %d lazy : %d\n",t[rt].x,t[rt].y,t[rt].lazy);
    if(t[rt].x == t[rt].y) {
        printf("%d ",t[rt].value);
        return ;
    }
    print_tree(rt<<1);
    print_tree(rt<<1|1);
}

int ReadInt() {
    char c = getchar();
    while(c > '9' || c < '0') c = getchar();
    int ret = 0;
    while(c <= '9' && c >= '0') {
        ret = ret * 10 + c - '0';
        c = getchar();
    }
    return ret;
}

void input() {
    int u,v,x,value;
    for(int i = 1 ; i <= N - 1 ; i++) {
        scanf("%d %d %d",&u,&v,&value);
        addedge(u,v,value,i);
    }
    dfs1(1);
    dfs2(1,1);
    // for(int i = 1 ; i <= N ; i++) {
    //     Rank[pos[i]] = num[i];
    // }
    Build(1,N,1);
    for(int i = 1 ; i <= Q ; i ++) {
        scanf("%d",&op) ;
        if(op == 1) {
            scanf("%d %d",&u,&value);
            Update(1,idx[u],value);
        }
        else {
            scanf("%d",&x);
            printf("%d\n",Query_Tree(S,x));
            S = x;
        }
        //print_tree(1);
        //puts("");
    }
}

int main(void) {
    freopen("a.in","r",stdin);
    while(~scanf("%d %d %d",&N,&Q,&S)) {
        Init();
        input();
        //solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值