树上距离为1子集修改

http://www.elijahqi.win/archives/3691
为了纪念下自己丑陋的代码

每次给一条链及其距离为1的点进行修改

首先划分轻重链 然后在链上编号连续 乱搞即可

#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
#define lc (x<<1)
#define rc (x<<1|1)
#define uint unsigned int
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while (!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=1e5+10;
struct node{
    int op,x,y,z;
}qr[N];
struct node1{
    int y,next;
}data[N<<1];
int h[N],num,n,m,w[N];
namespace sol1{
    vector<int> eg[N];uint w[N];int fa[N],dep[N];
    bool visit[N];
    inline void dfs(int x){
        for (int i=0;i<eg[x].size();++i){
            int y=eg[x][i];if (y==fa[x]) continue;
            fa[y]=x;dep[y]=dep[x]+1;dfs(y);
        }
    }
    inline void dfs1(int x,int v){
        w[x]+=v;
        for (int i=0;i<eg[x].size();++i){
            int y=eg[x][i];if (y==fa[x]) continue;
            dfs1(y,v);
        }
    }
    inline void dfs2(int x,int v){
        w[x]=v;
        for (int i=0;i<eg[x].size();++i){
            int y=eg[x][i];if (y==fa[x]) continue;
            dfs2(y,v);
        }
    }uint Ans;
    inline void dfs3(int x){
        Ans+=w[x];
        for (int i=0;i<eg[x].size();++i){
            int y=eg[x][i];if (y==fa[x]) continue;
            dfs3(y);
        }
    }
    inline bool dfs4(int x,int v){
        if (x==v) return 1;
        for (int i=0;i<eg[x].size();++i){
            int y=eg[x][i];if (y==fa[x]) continue;
            if (dfs4(y,v)) return 1;
        }return 0;
    }
    inline void gao(){
        for (int i=1;i<=n;++i) w[i]=read();uint tmp=0;
        //for (int i=2378;i<=3050;++i) tmp+=w[i];puts("---");printf("%u\n",tmp);
        for (int i=1;i<n;++i){
            int x=read(),y=read();
            eg[x].push_back(y);eg[y].push_back(x);
        }dfs(1);
        for (int i=1;i<=m;++i){
            int op=read();
            if (op==2){
                int x=read(),y=read(),z=read();
                if (dep[x]<dep[y]) swap(x,y);
                while(dep[x]!=dep[y]) w[x]+=z,x=fa[x];
                while(x!=y){
                    w[x]+=z;w[y]+=z;
                    x=fa[x];y=fa[y];
                }w[x]+=z;
            }
            if (op==1){
                int x=read(),y=read(),z=read();
                if (dep[x]<dep[y]) swap(x,y);
                while(dep[x]!=dep[y]) w[x]=z,x=fa[x];
                while(x!=y){
                    w[x]=z;w[y]=z;
                    x=fa[x];y=fa[y];
                }w[x]=z;
            }
            if (op==3){
                int x=read(),y=read();uint ans=0;
                if (dep[x]<dep[y]) swap(x,y);
                while(dep[x]!=dep[y]) ans+=w[x],x=fa[x];
                while(x!=y){
                    ans+=w[x];ans+=w[y];
                    x=fa[x];y=fa[y];
                }ans+=w[x];printf("%u\n",ans);
            }
            if (op==5){
                int x=read(),z=read();dfs1(x,z);
            }
            if (op==4){
                int x=read(),z=read();dfs2(x,z);
            }
            if (op==6){
                int x=read();Ans=0;dfs3(x);printf("%u\n",Ans);
            }
            if (op==8){
                int x=read(),y=read(),z=read();
                memset(visit,0,sizeof(visit));
                if (dep[x]<dep[y]) swap(x,y);
                while(dep[x]!=dep[y]) {
                    if (!visit[x]) w[x]+=z;visit[x]=1;
                    for (int i=0;i<eg[x].size();++i){
                        int to=eg[x][i];if (visit[to]) continue;
                        visit[to]=1;w[to]+=z;
                    }
                    x=fa[x];
                }
                while(x!=y){
                    if (!visit[x]) w[x]+=z;visit[x]=1;
                    if (!visit[y]) w[y]+=z;visit[y]=1;
                    for (int i=0;i<eg[x].size();++i){
                        int to=eg[x][i];if (visit[to]) continue;
                        visit[to]=1;w[to]+=z;
                    }
                    for (int i=0;i<eg[y].size();++i){
                        int to=eg[y][i];if (visit[to]) continue;
                        visit[to]=1;w[to]+=z;
                    }
                    x=fa[x];y=fa[y];
                }if (!visit[x]) w[x]+=z;
                for (int i=0;i<eg[x].size();++i){
                    int to=eg[x][i];if (visit[to]) continue;
                    visit[to]=1;w[to]+=z;
                }
            }
            if (op==7){
                int x=read(),y=read(),z=read();
                memset(visit,0,sizeof(visit));
                if (dep[x]<dep[y]) swap(x,y);
                while(dep[x]!=dep[y]) {
                    if (!visit[x]) w[x]=z;visit[x]=1;
                    for (int i=0;i<eg[x].size();++i){
                        int to=eg[x][i];if (visit[to]) continue;
                        visit[to]=1;w[to]=z;
                    }
                    x=fa[x];
                }
                while(x!=y){
                    if (!visit[x]) w[x]=z;visit[x]=1;
                    if (!visit[y]) w[y]=z;visit[y]=1;
                    for (int i=0;i<eg[x].size();++i){
                        int to=eg[x][i];if (visit[to]) continue;
                        visit[to]=1;w[to]=z;
                    }
                    for (int i=0;i<eg[y].size();++i){
                        int to=eg[y][i];if (visit[to]) continue;
                        visit[to]=1;w[to]=z;
                    }
                    x=fa[x];y=fa[y];
                }if (!visit[x]) w[x]=z;
                for (int i=0;i<eg[x].size();++i){
                    int to=eg[x][i];if (visit[to]) continue;
                    visit[to]=1;w[to]=z;
                }
            }
            if (op==9){
                int x=read(),y=read();uint ans=0;tmp=0;
            //  for (int i=2378;i<=7359;++i) tmp+=w[i];puts("---");printf("%u\n",tmp);
                memset(visit,0,sizeof(visit));
                if (dep[x]<dep[y]) swap(x,y);
                while(dep[x]!=dep[y]) {
                    if (!visit[x]) ans+=w[x];visit[x]=1;
                    for (int i=0;i<eg[x].size();++i){
                        int to=eg[x][i];if (visit[to]) continue;
                        visit[to]=1;ans+=w[to];
                    }
                    x=fa[x];
                }tmp=0;
                //for (int i=6847;i<=7359;++i) tmp+=w[i];puts("--");printf("%u\n",tmp);
                while(x!=y){
                    if (!visit[x]) ans+=w[x];visit[x]=1;
                    if (!visit[y]) ans+=w[y];visit[y]=1;
                    for (int i=0;i<eg[x].size();++i){
                        int to=eg[x][i];if (visit[to]) continue;
                        visit[to]=1;ans+=w[to];
                    }
                    for (int i=0;i<eg[y].size();++i){
                        int to=eg[y][i];if (visit[to]) continue;
                        visit[to]=1;ans+=w[to];
                    }
                    x=fa[x];y=fa[y];
                }if (!visit[x]) ans+=w[x];
                for (int i=0;i<eg[x].size();++i){
                        int to=eg[x][i];if (visit[to]) continue;
                        visit[to]=1;ans+=w[to];
                    }
                printf("%u\n",ans);
            }
            if (op==10){
                int x=read();fa[x]=0;dep[x]=0;dfs(x);
            }
            if (op==11){
                int x=read(),y=read();
                bool flag=dfs4(x,y);if (flag) continue;
                int p;
                for (int i=0;i<eg[fa[x]].size();++i){
                    if (eg[fa[x]][i]==x) {p=i;break;}
                }
                eg[fa[x]].erase(eg[fa[x]].begin()+p);
                for (int i=0;i<eg[x].size();++i){
                    if (eg[x][i]==fa[x]) {p=i;break;}
                }
                eg[x].erase(eg[x].begin()+p);
                eg[y].push_back(x);eg[x].push_back(y);
                fa[x]=y;dfs(y);
            }
        }
    }
}
namespace sol5{
    int size[N],tp[N],son[N],fa[N],bl[N],dfn[N],st[N],ed[N],dep[N];
    bool used[N];
    inline void dfs(int x){
        size[x]=1;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;if (y==fa[x]) continue;
            fa[y]=x;dep[y]=dep[x]+1;dfs(y);size[x]+=size[y];
            if (size[y]>size[son[x]]) son[x]=y;
        }
    }
    inline void dfs1(int x,int top){
        st[x]=num+1;tp[x]=top;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;if (y==fa[x]||y==son[x]) continue;
            used[y]=1;dfn[y]=++num,bl[num]=y;
        }ed[x]=num;if(son[x]) dfs1(son[x],top);
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;if(y==fa[x]||y==son[x]) continue;
            dfs1(y,y);
        }
    }
    inline void dfs2(int x){
        if(!used[x]) dfn[x]=++num,bl[num]=x;
        if(son[x]) dfs2(son[x]);
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;if (y==fa[x]||y==son[x]) continue;
            dfs2(y);
        }
    }
    uint sum[N<<2],tag[N<<2],add[N<<2];
    inline void build(int x,int l,int r){
        tag[x]=2e9;
        if (l==r) {sum[x]=w[bl[l]];return;}
        int mid=l+r>>1;
        build(lc,l,mid);build(rc,mid+1,r);
        sum[x]=sum[lc]+sum[rc];
    }
    inline void pushdown(int x,int l,int r){
        int mid=l+r>>1;
        if (tag[x]!=2e9){
            tag[rc]=tag[lc]=tag[x];add[lc]=add[rc]=0;
            sum[lc]=(mid-l+1)*tag[x];sum[rc]=(r-mid)*tag[x];tag[x]=2e9;
        }
        if (add[x]){
            add[lc]+=add[x];add[rc]+=add[x];
            sum[lc]+=(mid-l+1)*add[x];sum[rc]+=(r-mid)*add[x];
            add[x]=0;
        }
    }
    inline void modify(int x,int l,int r,int l1,int r1,int v){
        if (l1<=l&&r1>=r){
            tag[x]=v;add[x]=0;sum[x]=(uint)(r-l+1)*v;return;
        }int mid=l+r>>1;pushdown(x,l,r);
        if (l1<=mid) modify(lc,l,mid,l1,r1,v);
        if (r1>mid) modify(rc,mid+1,r,l1,r1,v);
        sum[x]=sum[lc]+sum[rc];
    }
    inline void add1(int x,int l,int r,int l1,int r1,int v){
        if (l1<=l&&r1>=r){
            add[x]+=v;sum[x]+=(uint)(r-l+1)*v;return;
        }int mid=l+r>>1;pushdown(x,l,r);
        if(l1<=mid) add1(lc,l,mid,l1,r1,v);
        if (r1>mid) add1(rc,mid+1,r,l1,r1,v);
        sum[x]=sum[lc]+sum[rc];
    }
    inline uint query(int x,int l,int r,int l1,int r1){
        if (l1<=l&&r1>=r) return sum[x];
        int mid=l+r>>1;pushdown(x,l,r);uint tmp=0;
        if (l1<=mid) tmp+=query(lc,l,mid,l1,r1);
        if (r1>mid) tmp+=query(rc,mid+1,r,l1,r1);return tmp;
    }
    inline void tadd1(int x,int y,int z){
        while(tp[x]!=tp[y]){
            if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
            if (x!=tp[x]) add1(1,1,n,dfn[son[tp[x]]],dfn[x],z);
            add1(1,1,n,dfn[tp[x]],dfn[tp[x]],z);x=fa[tp[x]];
        }
        if (dfn[x]>dfn[y]) swap(x,y);
        if (x==tp[x]){
            if(x!=y) add1(1,1,n,dfn[son[tp[x]]],dfn[y],z);
            add1(1,1,n,dfn[x],dfn[x],z);
        }else{
            add1(1,1,n,dfn[x],dfn[y],z);
        }
    }
    inline void tmodify(int x,int y,int z){
        while(tp[x]!=tp[y]){
            if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
            if (x!=tp[x]) modify(1,1,n,dfn[son[tp[x]]],dfn[x],z);
            modify(1,1,n,dfn[tp[x]],dfn[tp[x]],z);x=fa[tp[x]];
        }
        if (dfn[x]>dfn[y]) swap(x,y);
        if (x==tp[x]){
            if(x!=y) modify(1,1,n,dfn[son[tp[x]]],dfn[y],z);
            modify(1,1,n,dfn[x],dfn[x],z);
        }else{
            modify(1,1,n,dfn[x],dfn[y],z);
        }
    }
    inline uint tquery(int x,int y){
        uint ans=0;
        while(tp[x]!=tp[y]){
            if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
            if (x!=tp[x]) ans+=query(1,1,n,dfn[son[tp[x]]],dfn[x]);
            ans+=query(1,1,n,dfn[tp[x]],dfn[tp[x]]);x=fa[tp[x]];
        }
        if (dfn[x]>dfn[y]) swap(x,y);
        if (x==tp[x]){
            if(x!=y) ans+=query(1,1,n,dfn[son[tp[x]]],dfn[y]);
            ans+=query(1,1,n,dfn[x],dfn[x]);
        }else{
            ans+=query(1,1,n,dfn[x],dfn[y]);
        }return ans;
    }
    inline void sadd1(int x,int y,int z){
        while(tp[x]!=tp[y]){
            if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
            if (x!=tp[x]) add1(1,1,n,dfn[son[tp[x]]],dfn[x],z);
        //  if (st[tp[x]]<=ed[tp[x]]&&st[tp[x]]) add1(1,1,n,dfn[tp[x]],dfn[tp[x]],z);
            x=fa[tp[x]];
        }
        if (dfn[x]>dfn[y]) swap(x,y);
        if (x==tp[x]){
            if (x!=y) add1(1,1,n,dfn[son[tp[x]]],dfn[y],z);
            add1(1,1,n,dfn[x],dfn[x],z);
        }else{
            add1(1,1,n,dfn[x],dfn[y],z);
        }
    }
    inline void smodify(int x,int y,int z){
        while(tp[x]!=tp[y]){
            if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
            if (x!=tp[x]) modify(1,1,n,dfn[son[tp[x]]],dfn[x],z);
        //  if (st[tp[x]]<=ed[tp[x]]&&st[tp[x]]) modify(1,1,n,dfn[tp[x]],dfn[tp[x]],z);
            x=fa[tp[x]];
        }
        if (dfn[x]>dfn[y]) swap(x,y);
        if (x==tp[x]){
            if (x!=y) modify(1,1,n,dfn[son[tp[x]]],dfn[y],z);
            modify(1,1,n,dfn[x],dfn[x],z);
        }else{
            modify(1,1,n,dfn[x],dfn[y],z);
        }
    }
    inline uint squery(int x,int y){
        uint ans=0;
        while(tp[x]!=tp[y]){
            if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
            if (x!=tp[x]) ans+=query(1,1,n,dfn[son[tp[x]]],dfn[x]);
            //if (st[tp[x]]<=ed[tp[x]]&&st[tp[x]]) ans+=;
            x=fa[tp[x]];
        }
        if (dfn[x]>dfn[y]) swap(x,y);
        if (x==tp[x]){
            if (x!=y) ans+=query(1,1,n,dfn[son[tp[x]]],dfn[y]);
            ans+=query(1,1,n,dfn[x],dfn[x]);
        }else{
            ans+=query(1,1,n,dfn[x],dfn[y]);
        }return ans;
    }
    inline void print(int x,int l,int r){
        if(l==r) {printf("%d %d %u\n",l,r,sum[x]);return;}
        int mid=l+r>>1;pushdown(x,l,r);
        print(lc,l,mid);
        //printf("%d %d %u\n",l,r,sum[x]);
        print(rc,mid+1,r);
    }
    inline void gao(){
        dfs(1);num=0;dfs1(1,1);dfs2(1);build(1,1,n);
        for (int i=1;i<=m;++i){
            int op=qr[i].op;
            if (op==2) tadd1(qr[i].x,qr[i].y,qr[i].z);
            if (op==1) tmodify(qr[i].x,qr[i].y,qr[i].z);
            if (op==3) printf("%u\n",tquery(qr[i].x,qr[i].y));
            if (op==8){
                int x=qr[i].x,y=qr[i].y,z=qr[i].z;
                sadd1(x,y,z);
                while(tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    if (son[x]) add1(1,1,n,dfn[son[x]],dfn[son[x]],z);
                    if (st[tp[x]]<=ed[x]&&st[tp[x]]) add1(1,1,n,st[tp[x]],ed[x],z);
                    x=fa[tp[x]];
                }if (dfn[x]>dfn[y]) swap(x,y);
                if (fa[x]) add1(1,1,n,dfn[fa[x]],dfn[fa[x]],z);
                if (son[y]) add1(1,1,n,dfn[son[y]],dfn[son[y]],z);
                if (st[x]<=ed[y]&&st[x]) add1(1,1,n,st[x],ed[y],z);
            }
            if (op==7){
                int x=qr[i].x,y=qr[i].y,z=qr[i].z;
                smodify(x,y,z);
                while(tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    if (son[x]) modify(1,1,n,dfn[son[x]],dfn[son[x]],z);
                    if (st[tp[x]]<=ed[x]&&st[tp[x]]) modify(1,1,n,st[tp[x]],ed[x],z);
                    x=fa[tp[x]];
                }if (dfn[x]>dfn[y]) swap(x,y);
                if (fa[x]) modify(1,1,n,dfn[fa[x]],dfn[fa[x]],z);
                if (son[y]) modify(1,1,n,dfn[son[y]],dfn[son[y]],z);
                if (st[x]<=ed[y]&&st[x]) modify(1,1,n,st[x],ed[y],z);
            }
            if (op==9){
                int x=qr[i].x,y=qr[i].y;
                uint ans=0;ans+=squery(x,y);
                while(tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    if (son[x]) ans+=query(1,1,n,dfn[son[x]],dfn[son[x]]);
                    if (st[tp[x]]<=ed[x]&&st[tp[x]]) ans+=query(1,1,n,st[tp[x]],ed[x]);
                    x=fa[tp[x]];
                }if (dfn[x]>dfn[y]) swap(x,y);
                if (fa[x]) ans+=query(1,1,n,dfn[fa[x]],dfn[fa[x]]);
                if (son[y]) ans+=query(1,1,n,dfn[son[y]],dfn[son[y]]);
                if (st[x]<=ed[y]&&st[x]) ans+=query(1,1,n,st[x],ed[y]);
                printf("%u\n",ans);
            }//print(1,1,n);
        }
    }
}
namespace sol2{
    uint sum[N<<2],tag[N<<2],add[N<<2];
    int size[N],son[N],dfn[N],out[N],tp[N],dep[N],fa[N],bl[N];
    inline void build(int x,int l,int r){
        tag[x]=2e9;
        if (l==r) {sum[x]=w[bl[l]];return;}
        int mid=l+r>>1;
        build(lc,l,mid);build(rc,mid+1,r);
        sum[x]=sum[lc]+sum[rc];
    }
    inline void pushdown(int x,int l,int r){
        int mid=l+r>>1;
        if (tag[x]!=2e9){
            tag[rc]=tag[lc]=tag[x];add[lc]=add[rc]=0;
            sum[lc]=(mid-l+1)*tag[x];sum[rc]=(r-mid)*tag[x];tag[x]=2e9;
        }
        if (add[x]){
            add[lc]+=add[x];add[rc]+=add[x];
            sum[lc]+=(mid-l+1)*add[x];sum[rc]+=(r-mid)*add[x];
            add[x]=0;
        }
    }
    inline void modify(int x,int l,int r,int l1,int r1,int v){
        if (l1<=l&&r1>=r){
            tag[x]=v;add[x]=0;sum[x]=(uint)(r-l+1)*v;return;
        }int mid=l+r>>1;pushdown(x,l,r);
        if (l1<=mid) modify(lc,l,mid,l1,r1,v);
        if (r1>mid) modify(rc,mid+1,r,l1,r1,v);
        sum[x]=sum[lc]+sum[rc];
    }
    inline void add1(int x,int l,int r,int l1,int r1,int v){
        if (l1<=l&&r1>=r){
            add[x]+=v;sum[x]+=(uint)(r-l+1)*v;return;
        }int mid=l+r>>1;pushdown(x,l,r);
        if(l1<=mid) add1(lc,l,mid,l1,r1,v);
        if (r1>mid) add1(rc,mid+1,r,l1,r1,v);
        sum[x]=sum[lc]+sum[rc];
    }
    inline uint query(int x,int l,int r,int l1,int r1){
        if (l1<=l&&r1>=r) return sum[x];
        int mid=l+r>>1;pushdown(x,l,r);uint tmp=0;
        if (l1<=mid) tmp+=query(lc,l,mid,l1,r1);
        if (r1>mid) tmp+=query(rc,mid+1,r,l1,r1);return tmp;
    }
    inline void dfs(int x){
        size[x]=1;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;if (y==fa[x]) continue;
            fa[y]=x;dep[y]=dep[x]+1;dfs(y);size[x]+=size[y];if (size[son[x]]<size[y]) son[x]=y;
        }
    }
    inline void dfs1(int x,int top){
        dfn[x]=++num;bl[num]=x;tp[x]=top;
        if (son[x]) dfs1(son[x],top);
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;if (y==fa[x]||y==son[x]) continue;
            dfs1(y,y);
        }out[x]=num;
    }
    inline void gao(){
        num=0;dfs(1);dfs1(1,1);
        build(1,1,n);
        for (int i=1;i<=m;++i){
            int op=qr[i].op;
            if (op==1){
                int x=qr[i].x,y=qr[i].y,z=qr[i].z;
                while(tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    if (x!=tp[x]) modify(1,1,n,dfn[son[tp[x]]],dfn[x],z);
                    modify(1,1,n,dfn[tp[x]],dfn[tp[x]],z);x=fa[tp[x]];
                }
                if (dfn[x]>dfn[y]) swap(x,y);
                if (x==tp[x]){
                    if(x!=y) modify(1,1,n,dfn[son[tp[x]]],dfn[y],z);
                    modify(1,1,n,dfn[x],dfn[x],z);
                }else{
                    modify(1,1,n,dfn[x],dfn[y],z);
                }
            }
            if (op==2){
                int x=qr[i].x,y=qr[i].y,z=qr[i].z;
                while(tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    if (x!=tp[x]) add1(1,1,n,dfn[son[tp[x]]],dfn[x],z);
                    add1(1,1,n,dfn[tp[x]],dfn[tp[x]],z);x=fa[tp[x]];
                }
                if (dfn[x]>dfn[y]) swap(x,y);
                if (x==tp[x]){
                    if(x!=y) add1(1,1,n,dfn[son[tp[x]]],dfn[y],z);
                    add1(1,1,n,dfn[x],dfn[x],z);
                }else{
                    add1(1,1,n,dfn[x],dfn[y],z);
                }
            }
            if (op==3){
                int x=qr[i].x,y=qr[i].y;
                uint ans=0;
                while(tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    if (x!=tp[x]) ans+=query(1,1,n,dfn[son[tp[x]]],dfn[x]);
                    ans+=query(1,1,n,dfn[tp[x]],dfn[tp[x]]);x=fa[tp[x]];
                }
                if (dfn[x]>dfn[y]) swap(x,y);
                if (x==tp[x]){
                    if(x!=y) ans+=query(1,1,n,dfn[son[tp[x]]],dfn[y]);
                    ans+=query(1,1,n,dfn[x],dfn[x]);
                }else{
                    ans+=query(1,1,n,dfn[x],dfn[y]);
                }printf("%u\n",ans);
            }
            if (op==4){
                int x=qr[i].x,z=qr[i].y;
                modify(1,1,n,dfn[x],out[x],z);
            }
            if (op==5){
                int x=qr[i].x,z=qr[i].y;
                add1(1,1,n,dfn[x],out[x],z);
            }
            if (op==6){
                int x=qr[i].x;
                printf("%u\n",query(1,1,n,dfn[x],out[x]));
            }
        }
    }
}
int main(){
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    n=read();m=read();
    if(n<=10000) {sol1::gao();return 0;}
    for (int i=1;i<=n;++i) w[i]=read();
    for (int i=1;i<n;++i){
        int x=read(),y=read();
        data[++num].y=y;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].next=h[y];h[y]=num;
    }
    bool flag5=1;bool flag6=1;
    for (int i=1;i<=m;++i){
        int op=read();if (op>=4&&op<=6) flag5=0;
        if (op>9) flag5=0,flag6=0;
        if(op==1||op==2||op==7||op==8){
            qr[i].op=op;qr[i].x=read();qr[i].y=read();
            qr[i].z=read();
        }else if(op==6||op==10){
            qr[i].op=op;qr[i].x=read();
        }else qr[i].op=op,qr[i].x=read(),qr[i].y=read();
    }
    if (flag5) {sol5::gao();return 0;}
    if (flag6) {sol2::gao();return 0;}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值