Lightoj 1348 Aladdin and the return journey

Source: Internet
Author: User

1348-Aladdin and the return journey

  PDF (English) Statistics Forum
Time Limit: 2 second (s) Memory limit: 32 MB

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)NNodes, 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 inNodesThat occur in path from NodeIToJ (0 ≤ I, j <n).

2)1 I V,It means that number of genies in nodeIIs changedV (0 ≤ I <n, 0 ≤ v ≤ 1000).

Input

Input starts with an integerT (≤ 10), Denoting the number of test cases.

Each case starts with a blank line. Next line contains an integerN (2 ≤ n ≤30000). The next line containsNSpace separated integers0And1000, Denoting the number of genies in the nodes respectively. Then there areN-1Lines each containing two integers:U v (0 ≤ u, v <n, u =v)Meaning that there is an edge from NodeUAndV. Assume that the edges form a valid tree. Next line contains an integerQ (1 ≤ q≤105)FollowedQLines each containing a query as described above.

Output

For each case, print the case number in a single line. Then for each query0 I j, Print the total number of genies in the nodes that occur in PathIToJ.

Sample Input Output for sample input

1

 

4

10 20 30 40

0 1

1 2

1 3

3

0 2 3

1 100

0 2 3

Case 1:

90

170

Note

Dataset is huge, use faster I/O methods.

Problem setter: Jane Alam Jan, which is also a tree chunking. The difference is that this question has the right on the vertex, not on the edge. When querying, pay attention not to directly return the result when Dep [u] = Dep [v], but to query it again.
#include <cstdio>#include <queue>#include <algorithm>#include <iostream>#include <cstring>using namespace std;#define root 1,n,1#define lr rt<<1#define rr rt<<1|1#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long LL;const int N= 100010;int n,q,s;int eh[N],et[N<<2],nxt[N<<2],w[N<<2],tot=0;int pos,p[N],top[N],rnk[N],dep[N],fa[N],son[N],siz[N];void init(){    memset(eh,-1,sizeof(eh));    memset(son,-1,sizeof(son));    tot=0;    pos=1;}void addedge(int u,int v){    et[tot]=v;nxt[tot]=eh[u];eh[u]=tot++;    et[tot]=u;nxt[tot]=eh[v];eh[v]=tot++;}void dfs1(int u,int father,int d){    dep[u]=d;    fa[u]=father;    siz[u]=1;    for(int i=eh[u];~i;i=nxt[i]){        int v=et[i];        if(v != father){            dfs1(v,u,d+1);            siz[u] += siz[v];            if( son[u]==-1 || siz[v] > siz[son[u]])                son[u]=v;        }    }}void dfs2(int u,int tp){    top[u]=tp;    p[u]= pos++;    rnk[ p[u] ]= u;    if( son[u] == -1 )return ;    dfs2(son[u], tp );    for(int i=eh[u]; ~i ;i=nxt[i]){        int v=et[i];        if(v != son[u] && v!=fa[u] ){            dfs2(v,v);        }    }}int sum[ N<<2 ];void Up(int rt){    sum[rt]=sum[lr]+sum[rr];}void build(int l,int r,int rt){    if(l == r ){        sum[rt]=0;        return ;    }    int m=(l+r)>>1;    build(lson);    build(rson);    Up(rt);}void update(int l,int r,int rt,int x,int v){    if( l == r ){        sum[rt]=v;        return ;    }    int m=(l+r)>>1;    if(x<=m)        update(lson,x,v);    if(x>m)        update(rson,x,v);    Up(rt);}int query(int l,int r,int rt,int L,int R){    int res=0;    if( L<=l && r<=R ){        return sum[rt];    }    int m=(l+r)>>1;    if(L <= m)        res += query(lson,L,R);    if(R > m)        res += query(rson,L,R);    return res;}int Q(int u,int v){    int res=0;    int f1=top[u],f2=top[v];    while(f1 != f2){        if(dep[f1] < dep[f2]){            swap(f1,f2);            swap(u,v);        }        res += query(root,p[f1],p[u]);        u=fa[f1];        f1=top[u];    }    if(dep[u]>dep[v])swap(u,v);    res += query(root,p[u],p[v]);    return res;}int W[N];int run(){    int u,v,w;    int op;    int _,cas=1;    scanf("%d",&_);    while(_--){        printf("Case %d:\n",cas++);        scanf("%d",&n);        init();        for(int i=1;i<=n;++i){            scanf("%d",&W[i]);        }        for(int i=0;i<n-1;++i){            scanf("%d%d",&u,&v);            u++;v++;            addedge(u,v);        }        dfs1(1,0,0);        dfs2( 1,1 );        build(root);       // for(int i=1;i<=n;++i){cout<<p[i]<<‘ ‘;}cout<<endl;        for(int i=1;i<=n;++i)        {            update(root,p[i],W[i]);        }        scanf("%d",&q);        while(q--)        {            scanf("%d",&op);            if( !op )            {                scanf("%d%d",&u,&v);                  u++;v++;                printf("%d\n",Q(u,v));            }            else {                scanf("%d%d",&v,&w);                v++;                update(root,p[v],w);            }        }    }    return 0;}int main(){    #ifdef LOCAL        freopen("in.txt","r",stdin);    #endif    ios::sync_with_stdio(0);    return run();}

 

 

Lightoj 1348 Aladdin and the return journey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.