记录编号 585488 评测结果 AAAAAAAAAAAAAAAAAA
题目名称 遥远的国度 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.377 s
提交时间 2023-12-13 19:08:34 内存使用 31.81 MiB
显示代码纯文本
#include <bits/stdc++.h> 
using namespace std;
#define ll long long
const int N = 2e5+10;
int n,m,root;
struct made{
    int ver,nx;
}e[N<<1];
int hd[N],tot,cnt;
int fa[N],son[N],size[N],dep[N];
int a[N],top[N],dfn[N],rnk[N];
struct Tree{
    int l,r,mi,la;
    #define l(x) t[x].l
    #define r(x) t[x].r
    #define la(x) t[x].la
    #define mi(x) t[x].mi
}t[N<<2];
void pushup(int x){
    mi(x) = min(mi(x<<1),mi(x<<1|1));
}
void pushdown(int x){
    if(!la(x))return;
    la(x<<1) = la(x<<1|1) = la(x);
    mi(x<<1) = mi(x<<1|1) = la(x);
    la(x) = 0; 
}
void build(int x,int l,int r){
    l(x) = l,r(x) = r;
    if(l == r){
        mi(x) = a[rnk[l]];
        return;
    }
    int mid = l + r >> 1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    pushup(x);
}
int ask(int x,int l,int r){
    if(l <= l(x) && r(x) <= r)return mi(x);
    pushdown(x);
    int mid = l(x) + r(x) >> 1,ans = INT_MAX;
    if(l <= mid)ans = min(ans,ask(x<<1,l,r));
    if(r > mid)ans = min(ans,ask(x<<1|1,l,r));
    return ans;
}
void change(int x,int l,int r,int z){
    if(l <= l(x) && r(x) <= r){
        la(x) = mi(x) = z;
        return;
    }
    pushdown(x);
    int mid = l(x) + r(x) >> 1;
    if(l <= mid)change(x<<1,l,r,z);
    if(r > mid)change(x<<1|1,l,r,z);
    pushup(x);
}
//----------------------
void add(int x,int y){
    tot++;
    e[tot].ver = y,e[tot].nx = hd[x],hd[x] = tot;
}
void dfs1(int x){
    size[x] = 1;
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(dep[y])continue;
        dep[y] = dep[x] + 1;
        fa[y] = x;
        dfs1(y);
        size[x] += size[y];
        if(!son[x] || size[y] > size[son[x]])son[x] = y;
    }
}
void dfs2(int x,int t){
    dfn[x] = ++cnt;
    rnk[cnt] = x;
    top[x] = t;
    if(!son[x])return;
    dfs2(son[x],t);
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(y != fa[x] && y != son[x])dfs2(y,y);
    }
}
void caladd(int x,int y,int z){
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]])swap(x,y);
        change(1,dfn[top[x]],dfn[x],z);
        x = fa[top[x]];
    }
    if(dep[x] > dep[y])swap(x,y);
    change(1,dfn[x],dfn[y],z);
}
int find(int x,int r){
    bool f = 1; 
    while(1){
        if(top[x] == top[r])return son[x];
        if(fa[top[r]] == x)return top[r];
        r = fa[top[r]];
    }
}
int calask(int x){
    if(x == root)return t[1].mi;//特判!!! 
    if(dfn[root] <= dfn[x] || dfn[root] >= dfn[x]+size[x])return ask(1,dfn[x],dfn[x]+size[x]-1);
    x = find(x,root);
    return min(ask(1,1,dfn[x]-1),ask(1,dfn[x]+size[x],n));
}
int main(){
    freopen("bbbbb.in","r",stdin);
    freopen("bbbbb.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y),add(y,x);
    }
    for(int i = 1;i <= n;i++)scanf("%d",&a[i]);
    scanf("%d",&root);
    dep[1] = 1;
    dfs1(1),dfs2(1,1);
    build(1,1,n);
    for(int i = 1;i <= m;i++){
        int op,x,y,z;
        scanf("%d%d",&op,&x);
        if(op == 1)root = x;
        else if(op == 2){
           scanf("%d%d",&y,&z);
           caladd(x,y,z); 
        }
        else printf("%d\n",calask(x));
    }
    
    return 0;
    
}