LightOJ 1348 Aladdin and the Return Journey (tree link splitting), lightojaladdin

Source: Internet
Author: User

LightOJ 1348 Aladdin and the Return Journey (tree link splitting), lightojaladdin

Tree link splitting template questions.
There have been competitions recently .. I haven't written it for a long time. Obviously unfamiliar .. Find a template question to familiarize yourself.
The Code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)#define root 1, n, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1//#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-3;const int MAXN=30000+10;int head[MAXN], cnt, tot, n;int siz[MAXN], dep[MAXN], w[MAXN], top[MAXN], fa[MAXN], son[MAXN];int sum[MAXN<<2];int a[MAXN];struct node{        int u, v, next;}edge[MAXN<<1];void add(int u, int v){        edge[cnt].u=u;        edge[cnt].v=v;        edge[cnt].next=head[u];        head[u]=cnt++;}void init(){        memset(head,-1,sizeof(head));        cnt=tot=0;        memset(sum,0,sizeof(sum));        memset(son,0,sizeof(son));        memset(dep,0,sizeof(dep));}void dfs1(int u, int p){        siz[u]=1;        for(int i=head[u];i!=-1;i=edge[i].next){                int v=edge[i].v;                if(v==p) continue ;                fa[v]=u;                dep[v]=dep[u]+1;                dfs1(v,u);                siz[u]+=siz[v];                if(siz[v]>siz[son[u]]) son[u]=v;        }}void dfs2(int u, int tp){        w[u]=++tot;top[u]=tp;        if(son[u]) dfs2(son[u],tp);        for(int i=head[u];i!=-1;i=edge[i].next){                int v=edge[i].v;                if(v!=fa[u]&&v!=son[u])                        dfs2(v,v);        }}struct Line_Tree{        void PushUp(int rt)        {                sum[rt]=sum[rt<<1]+sum[rt<<1|1];        }        void Update(int p, int x, int l, int r, int rt)        {                if(l==r){                        sum[rt]=x;                        return ;                }                int mid=l+r>>1;                if(p<=mid) Update(p,x,lson);                else Update(p,x,rson);                PushUp(rt);        }        int Query(int ll, int rr, int l, int r, int rt)        {                if(ll<=l&&rr>=r){                        return sum[rt];                }                int mid=l+r>>1, ans=0;                if(ll<=mid) ans+=Query(ll,rr,lson);                if(rr>mid) ans+=Query(ll,rr,rson);                return ans;        }}lt;int SumQuery(int u, int v){        int f1=top[u], f2=top[v], ans=0;        while(f1!=f2){                if(dep[f1]<dep[f2]){                        swap(u,v);                        swap(f1,f2);                }                ans+=lt.Query(w[f1],w[u],root);                u=fa[f1];f1=top[u];        }        if(dep[u]<dep[v]){                swap(u,v);        }        return ans+lt.Query(w[v],w[u],root);}int main(){        int t, u, v, q, i, x, Case=0;        scanf("%d",&t);        while(t--){                scanf("%d",&n);                for(i=1;i<=n;i++){                        scanf("%d",&a[i]);                }                init();                for(i=1;i<n;i++){                        scanf("%d%d",&u,&v);                        u++;v++;                        add(u,v);                        add(v,u);                }                dfs1(1,-1);                dfs2(1,1);                for(i=1;i<=n;i++){                        lt.Update(w[i],a[i],root);                }                scanf("%d",&q);                printf("Case %d:\n",++Case);                while(q--){                        scanf("%d",&x);                        if(!x){                                scanf("%d%d",&u,&v);                                u++;v++;                                printf("%d\n",SumQuery(u,v));                        }                        else{                                scanf("%d%d",&u,&v);                                u++;                                lt.Update(w[u],v,root);                        }                }        }        return 0;}

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.