luoguP3979 遥远的国度 LCT + multiset维护子树信息

题目描述

zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度。当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn完成任务后才能进入遥远的国度继续追杀。

问题是这样的:遥远的国度有n个城市,这些城市之间由一些路连接且这些城市构成了一颗树。这个国度有一个首都,我们可以把这个首都看做整棵树的根,但遥远的国度比较奇怪,首都是随时有可能变为另外一个城市的。遥远的国度的每个城市有一个防御值,有些时候RapiD会使得某两个城市之间的路径上的所有城市的防御值都变为某个值。

RapiD想知道在某个时候,如果把首都看做整棵树的根的话,那么以某个城市为根的子树的所有城市的防御值最小是多少。

由于RapiD无法解决这个问题,所以他拦住了zcwwzdjn希望他能帮忙。但zcwwzdjn还要追杀sb的zhx,所以这个重大的问题就被转交到了你的手上。

输入输出格式

输入格式:

 

第1行两个整数n m,代表城市个数和操作数。

第2行至第n行,每行两个整数 u v,代表城市u和城市v之间有一条路。

第n+1行,有n个整数,代表所有点的初始防御值。

第n+2行一个整数 id,代表初始的首都为id。

第n+3行至第n+m+2行,首先有一个整数opt,如果opt=1,接下来有一个整数id,代表把首都修改为id;如果opt=2,接下来有三个整数p1 p2 v,代表将p1 p2路径上的所有城市的防御值修改为v;如果opt=3,接下来有一个整数 id,代表询问以城市id为根的子树中的最小防御值。

 

输出格式:

 

对于每个opt=3的操作,输出一行代表对应子树的最小点权值。

题解 : 

别人都是拿树剖写的,我用 $LCT + multiset$ 维护子树信息写了一次.
依次考虑每一个操作.
对于换首都,直接在 $LCT$ 中 $makeroot$ 一下即可.
链赋值,在 $LCT$ 中 $split$ 并修改.
难点在于求子树最小值.
我们维护几个变量.
$minv[x]$ : $x$ 及 $x$ 所在的 $splay$ 中的最小值.
$mins[x]$ : $x$ 的虚儿子及 $splay$ 中虚儿子的最小值.
$mintot[x]$ : $min(minv[x],mins[x])$
最后合并一下就行了
#include<bits/stdc++.h>
#define maxn 150000 
#define ll long long 
#define inf 2147483647000   
using namespace std; 
#define getset chil[x].empty()?inf:*chil[x].begin()      
void setIO(string s)
{
    string in=s+".in", out=s+".out"; 
    freopen(in.c_str(),"r",stdin);  
    // freopen(out.c_str(),"w",stdout);  
}         
multiset<ll>chil[maxn];            
ll value[maxn],mintot[maxn],mins[maxn],minv[maxn];        
namespace tree
{   
    #define lson ch[x][0] 
    #define rson ch[x][1]        
    int ch[maxn][2], f[maxn], rev[maxn], sta[maxn];      
    ll tag[maxn];   
    int get(int x)
    {
        return ch[f[x]][1]==x; 
    }
    int isroot(int x)
    {
        return (1^(ch[f[x]][1]==x||ch[f[x]][0]==x));            
    }
    void markrev(int x)
    {
        if(!x) return; 
        swap(lson,rson),rev[x]^=1;          
    }
    void pushup(int x)
    {
        if(!x) return;     
        minv[x]=mintot[x]=value[x], mins[x]=inf; 
        minv[x]=min(minv[x], min(minv[lson], minv[rson])); 
        mins[x]=min(min(mins[lson], mins[rson]), getset); 
        mintot[x]=min(minv[x], mins[x]); 
    }
    void marktag(int x,ll delta)
    {
        if(!x) return;     
        value[x]=tag[x]=minv[x]=delta;                
        mintot[x]=min(min(mins[lson], mins[rson]),getset);                 
    }
    void pushdown(int x)
    {
        if(!x) return;
        if(tag[x]) 
        {
            marktag(lson,tag[x]);
            marktag(rson,tag[x]);
            tag[x]=0; 
        } 
        if(rev[x]) 
        {
            markrev(lson);
            markrev(rson);
            rev[x]^=1; 
        } 
    }    
    void rotate(int x)
    {
        int old=f[x],fold=f[old],which=get(x); 
        if(!isroot(old)) ch[fold][ch[fold][1]==old]=x; 
        ch[old][which]=ch[x][which^1],f[ch[old][which]]=old; 
        ch[x][which^1]=old,f[old]=x,f[x]=fold; 
        pushup(old),pushup(x);     
    }
    void splay(int x)
    {
        int u=x,v=0,fa; 
        sta[++v]=u; 
        while(!isroot(u)) sta[++v]=f[u],u=f[u]; 
        while(v) pushdown(sta[v--]); 
        for(u=f[u];(fa=f[x])!=u;rotate(x)) 
            if(f[fa]!=u) 
                rotate(get(fa)==get(x)?fa:x); 
    }
    void Access(int x)
    {
        for(int t = 0 ; x ; t = x, x = f[x])
        {
            splay(x);               
            if(rson) chil[x].insert(mintot[rson]); 
            if(t) chil[x].erase(chil[x].lower_bound(mintot[t]));     
            rson=t; 
            pushup(x);     
        }
    }
    void MakeRoot(int x)
    {
        Access(x),splay(x), markrev(x); 
    }   
    void split(int x,int y)
    {
        MakeRoot(x),Access(y),splay(y);        
    }    
    void link(int x,int y)
    {           
        MakeRoot(x), pushdown(x), pushup(x),  f[x]=y, chil[y].insert(mintot[x]), pushup(y);    
    }  
}; 
int n,Q,edges,root; 
int from[maxn],to[maxn<<1],hd[maxn],nex[maxn<<1];           
int main()
{
    // setIO("input");  
    scanf("%d%d",&n,&Q);
    for(int i=1,u,v;i<n;++i) 
    {
        scanf("%d%d",&u,&v); 
        from[++edges]=u, to[edges]=v;    
    }       
    minv[0]=mins[0]=mintot[0]=value[0]=inf; 
    chil[0].insert(inf); 
    for(int i=1;i<=n;++i) scanf("%lld",&value[i]),mintot[i]=minv[i]=value[i], mins[i]=inf;                    
    for(int i=1;i<=edges;++i) tree::link(from[i],to[i]);                 
    scanf("%d",&root); 
    tree::MakeRoot(root); 
    int opt,a,b,c,x;     
    ll xx;    
    while(Q--)
    {
        scanf("%d",&opt); 
        switch(opt)
        {
            case 1 : 
            {
                scanf("%d",&a); 
                tree::MakeRoot(root=a); 
                break; 
            }
            case 2 : 
            {
                scanf("%d%d%lld",&a,&b,&xx); 
                tree::split(a,b);         
                tree::marktag(b,xx); 
                tree::MakeRoot(root); 
                break; 
            }
            case 3 : 
            {   
                scanf("%d",&x);  
                tree::Access(x); 
                tree::splay(x); 
                printf("%lld\n",min(value[x],getset)); 
                break; 
            }
        }
    }
    return 0; 
}

  

posted @ 2019-06-14 07:23  EM-LGH  阅读(264)  评论(0编辑  收藏  举报