bzoj3083: 遥远的国度

题目

Solution

1. x = = r o o t x==root x==root,查询整棵树。
2. l c a ( r o o t , x ) ! = x lca(root,x)!=x lca(root,x)!=x,查询 x x x的子树
3. l c a ( r o o t , x ) = = x lca(root,x)==x lca(root,x)==x,查询与 x x x相邻的节点中与 r o o t root root最近的点 v v v在整棵树中的补集
注意:既然这题是树剖,那还打什么倍增?直接树剖求 l c a lca lca好了

Code

#include<bits/stdc++.h>
using namespace std;
const int N=100002;
struct node{
    int to,ne;
}e[N<<1];
int mn[N<<2],lazy[N<<2],fa[N],dep[N],w[N],wt[N],cnt,rt,x,y,id[N],sz[N],son[N],top[N],tot,h[N],n,m,v,i,opt;
#define mid (l+r>>1)
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
    int x=0,fl=1;char ch=gc();
    for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
    for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
    return x*fl;
}
inline void wri(int a){if(a<0)a=-a,putchar('-');if(a>=10)wri(a/10);putchar(a%10|48);}
inline void wln(int a){wri(a);puts("");}
void add(int x,int y){
    e[++tot]=(node){y,h[x]};
    h[x]=tot;
}
void build(int t,int l,int r){
    if (l==r){
        mn[t]=wt[l];
        return;
    }
    build(t<<1,l,mid);
    build(t<<1|1,mid+1,r);
    mn[t]=min(mn[t<<1],mn[t<<1|1]);
}
void down(int t){
    if (lazy[t]){
        mn[t<<1]=mn[t<<1|1]=lazy[t<<1]=lazy[t<<1|1]=lazy[t];
        lazy[t]=0;
    }
}
void change(int t,int l,int r,int x,int y,int v){
    if (x<=l && r<=y){
        mn[t]=lazy[t]=v;
        return;
    }
    down(t);
    if (x<=mid) change(t<<1,l,mid,x,y,v);
    if (mid<y) change(t<<1|1,mid+1,r,x,y,v);
    mn[t]=min(mn[t<<1],mn[t<<1|1]);
}
int query(int t,int l,int r,int x,int y){
    if (x<=l && r<=y) return mn[t];
    down(t);int ans=2147483647;
    if (x<=mid) ans=min(ans,query(t<<1,l,mid,x,y));
    if (mid<y) ans=min(ans,query(t<<1|1,mid+1,r,x,y));
    return ans;
}
void Change(int x,int y,int v){
    while (top[x]!=top[y]){
        if (dep[top[x]]<dep[top[y]]) swap(x,y);
        change(1,1,n,id[top[x]],id[x],v);
        x=fa[top[x]];
    }
    if (dep[x]>dep[y]) swap(x,y);
    change(1,1,n,id[x],id[y],v);
}
int lca(int x,int y){
    while (top[x]!=top[y]){
        if (dep[top[x]]<dep[top[y]]) swap(x,y);
        x=fa[top[x]];
    }
    return dep[x]<dep[y]?x:y;
}
void dfs1(int u,int f){
    sz[u]=1;fa[u]=f;
    int mx=0;
    for (int i=h[u],v;i;i=e[i].ne)
        if ((v=e[i].to)!=f){
            dep[v]=dep[u]+1;
            dfs1(v,u);
            sz[u]+=sz[v];
            if (sz[v]>mx) mx=sz[v],son[u]=v;
        }
}
void dfs2(int u,int topf){
    id[u]=++cnt;wt[cnt]=w[u];top[u]=topf;
    if (!son[u]) return;
    dfs2(son[u],topf);
    for (int i=h[u],v;i;i=e[i].ne)
        if ((v=e[i].to)!=fa[u] && v!=son[u]) dfs2(v,v);
}
int main(){
    n=rd();m=rd();
    for (i=1;i<n;i++) x=rd(),y=rd(),add(x,y),add(y,x);
    for (i=1;i<=n;i++) w[i]=rd();
    dep[0]=-1;//如果用倍增求lca的话,这句话一定要加
    dfs1(1,0);dfs2(1,1);
    build(1,1,n);
    rt=rd();
    for (;m--;){
        opt=rd();x=rd();
        if (opt==1) rt=x;
        if (opt==2) y=rd(),v=rd(),Change(x,y,v);
        if (opt==3){
            if (x==rt) wln(mn[1]);
            else if (lca(x,rt)==x){
                y=rt;
                while (top[x]!=top[y])
                    if (fa[top[y]]!=x) y=fa[top[y]];
                    else{//x在某条链的底端
                        y=top[y];
                        break;
                    }
                if (fa[y]!=x) y=son[x];//x不在某条链底端
                wln(min(query(1,1,n,1,id[y]-1),query(1,1,n,id[y]+sz[y],n)));
            }else wln(query(1,1,n,id[x],id[x]+sz[x]-1));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值