LightOJ - 1348 Aladdin and the Return Journey (树链剖分+线段树单点更新,区间求和)

Aladdin and the Return Journey

Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for him. All he remembered was the paths he had taken to reach there. But since he took the lamp, all the genies in the cave became angry and they were planning to attack. As Aladdin was not afraid, he wondered how many genies were there. He summoned the Genie from the lamp and asked this.

Now you are given a similar problem. For simplicity assume that, you are given a tree (a connected graph with no cycles) with n nodes, nodes represent places, edges represent roads. In each node, initially there are an arbitrary number of genies. But the numbers of genies change in time. So, you are given a tree, the number of genies in each node and several queries of two types. They are:

1)      0 i j, it means that you have to find the total number of genies in the nodes that occur in path from node i to j (0 ≤ i, j < n).

2)      1 i v, it means that number of genies in node i is changed to v (0 ≤ i < n, 0 ≤ v ≤ 1000).

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a blank line. Next line contains an integer n (2 ≤ n ≤ 30000). The next line contains n space separated integers between 0 and 1000, denoting the number of genies in the nodes respectively. Then there are n-1 lines each containing two integers: u v (0 ≤ u, v < n, u ≠ v) meaning that there is an edge from node u and v. Assume that the edges form a valid tree. Next line contains an integer q (1 ≤ q ≤ 105) followed by q lines each containing a query as described above.

Output

For each case, print the case number in a single line. Then for each query 0 i j, print the total number of genies in the nodes that occur in path i to j.

Sample Input

1

 

4

10 20 30 40

0 1

1 2

1 3

3

0 2 3

1 1 100

0 2 3

Sample Output

Case 1:

90

170

 

题目链接:https://vjudge.net/problem/LightOJ-1348

题目大意:t组输入,有n个节点,给出每个节点的权值 w[i],n-1条边 ,m次操作,操作有两种:

0 x y  查询x到y路径上所有节点的权值和,    1 x z   把节点x的权值更新为z

思路:树链剖分+线段树维护区间和,单点更新,区间求和

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N=30005;
int first[N],tot,cnt,n,w[N],wt[N],sum[N<<2];
int d[N],fa[N],siz[N],son[N],top[N],id[N],rk[N];
struct node
{
    int v,nex;
}e[N<<1];
void adde(int u,int v)
{
    e[tot].v=v;
    e[tot].nex=first[u];
    first[u]=tot++;
}
void init()
{
    memset(first,-1,sizeof(first));
    memset(son,0,sizeof(son));
    tot=cnt=0;
}
void dfs1(int u,int pre,int depth)
{
    d[u]=depth;
    fa[u]=pre;
    siz[u]=1;
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v==pre) continue;
        dfs1(v,u,depth+1);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]])
            son[u]=v;
    }
}
void dfs2(int u,int t)
{
    top[u]=t;
    id[u]=++cnt;
    rk[cnt]=u;
    wt[cnt]=w[u];
    if(!son[u]) return;
    dfs2(son[u],t);
    for(int i=first[u];~i;i=e[i].nex)
    {
        int v=e[i].v;
        if(v!=fa[u]&&v!=son[u])
            dfs2(v,v);
    }
}
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=wt[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int k,int c,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=c;
        return;
    }
    int mid=(l+r)>>1;
    if(k<=mid) update(k,c,lson);
    else update(k,c,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return sum[rt];
    int mid=(l+r)>>1,ans=0;
    if(L<=mid) ans+=query(L,R,lson);
    if(R>mid) ans+=query(L,R,rson);
    return ans;
}
int quesum(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(d[top[x]]<d[top[y]]) swap(x,y);
        ans+=query(id[top[x]],id[x],1,n,1);
        x=fa[top[x]];
    }
    if(d[x]>d[y]) swap(x,y);
    ans+=query(id[x],id[y],1,n,1);
    return ans;
}
int main()
{
    int t,T=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        int x,y,z,op,m;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            x+=1,y+=1;
            adde(x,y);
            adde(y,x);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        build(1,n,1);
        scanf("%d",&m);
        printf("Case %d:\n",T++);
        while(m--)
        {
            scanf("%d",&op);
            if(op==0)
            {
                scanf("%d%d",&x,&y);
                x+=1,y+=1;
                printf("%d\n",quesum(x,y));
            }
            else if(op==1)
            {
                scanf("%d%d",&x,&z);
                x+=1;
                update(id[x],z,1,n,1);
            }
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值