• BZOJ 3510: 首都 LCT + multiset维护子树信息 + 树的重心


    Description

    在X星球上有N个国家,每个国家占据着X星球的一座城市。由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的。
    X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失,而B国的国土也将归A国管辖。A国国王为了加强统治,会在A国和B国之间修建一条公路,即选择原A国的某个城市和B国某个城市,修建一条连接这两座城市的公路。
    同样为了便于统治自己的国家,国家的首都会选在某个使得其他城市到它距离之和最小的城市,这里的距离是指需要经过公路的条数,如果有多个这样的城市,编号最小的将成为首都。
    现在告诉你发生在X星球的战事,需要你处理一些关于国家首都的信息,具体地,有如下3种信息需要处理:
    1、A x y:表示某两个国家发生战乱,战胜国选择了x城市和y城市,在它们之间修建公路(保证其中城市一个在战胜国另一个在战败国)。
    2、Q x:询问当前编号为x的城市所在国家的首都。
    3、Xor:询问当前所有国家首都编号的异或和。

    Input

    第一行是整数N,M,表示城市数和需要处理的信息数。
    接下来每行是一个信息,格式如题目描述(A、Q、Xor中的某一种)。

    Output

    输出包含若干行,为处理Q和Xor信息的结果。

     
     题解: 
    在线维护树的重心.
    有一个性质,连接两个树,新树的重心一定在原来两个树重心的连线上.
    每次连接时,只需在两个重心相连的链上特判一下即可.
    具体怎么判呢 ?我们先把这个链给 $split$ 出来.
    我们知道,树的重心的最大儿子的大小不会超过总大小的一半,只要找到那个最大儿子大小不超过总大小一半的点即可.
    具体的,我们在 $splay$ 上二分,查询一下当前点的最大儿子的大小.
    如果左面的总大小更大,就走到左面,否则,走到右面.
    每碰到一个可以作为重心的就更新一下答案即可. 
    #include<bits/stdc++.h>
    #define maxn 200000
    #define inf 1000000000 
    using namespace std;
    void setIO(string s)
    {
        string in=s+".in",out=s+".out"; 
        freopen(in.c_str(),"r",stdin);  
    }
    multiset<int>sonmax[maxn];    
    int n,Q,X=0; 
    int siz[maxn],son[maxn],p[maxn];   
    char str[20];   
    void init() { for(int i=0;i<maxn;++i) p[i]=i; }
    int find(int x) 
    {
    	return p[x]==x?x:p[x]=find(p[x]);       
    }
    namespace tr
    {
        #define isrt(x) (!(ch[f[x]][0]==x||ch[f[x]][1]==x)) 
        #define get(x) (ch[f[x]][1]==x) 
        #define lson ch[x][0]
        #define rson ch[x][1]    
        int ch[maxn][2],f[maxn],rev[maxn],sta[maxn];   
        void mark(int x)
        {
            if(!x)return; 
            swap(lson,rson), rev[x]^=1; 
        }
        void pushup(int x)
        {
            if(!x)return;    
            siz[x]=siz[lson]+siz[rson]+son[x]+1; 
        }
        void rotate(int x)
        {
            int old=f[x],fold=f[old],which=get(x); 
            if(!isrt(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 pushdown(int x)
        {
            if(!rev[x]||!x)return; 
            mark(lson), mark(rson),rev[x]^=1; 
        } 
        void splay(int x)
        {
            int v=0,u=x,fa; 
            sta[++v]=u; 
            while(!isrt(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(x)==get(fa)?fa:x);                    
        }
        void Access(int x)
        {
            int t=0;
            while(x)
            {
                splay(x); 
                son[x]=son[x]+siz[rson]-siz[t]; 
                if(t) sonmax[x].erase(sonmax[x].lower_bound(-siz[t]));         
                if(rson) sonmax[x].insert(-siz[rson]); 
                rson=t;   
                pushup(x); 
                t=x,x=f[x];               
            }
        }
        void MakeRoot(int x)
        {
            Access(x), splay(x), mark(x); 
        }   
        void split(int x,int y)
        {
            MakeRoot(x),Access(y),splay(y); 
        }  
        void link(int x,int y)
        { 
        	MakeRoot(x), MakeRoot(y); 
        	f[x]=y, son[y]+=siz[x];    
        	sonmax[y].insert(-siz[x]);     
        	pushup(y); 
        	x=find(x),y=find(y);  
        	split(x,y);              // y is on top of x 
        	int size=siz[y]>>1;     
        	int now=y,ls=0,rs=0,newg=n+233;  
        	while(now)
        	{
        		pushdown(now); 
        		int lsum=ls+siz[ch[now][0]],rsum=rs+siz[ch[now][1]]; 
        		if(lsum<=size&&rsum<=size&&(-*sonmax[now].begin())<=size) newg=min(newg,now); 
        		if(lsum>rsum) rs+=siz[ch[now][1]]+son[now]+1,now=ch[now][0]; 
        		else ls+=siz[ch[now][0]]+son[now]+1, now=ch[now][1];    
        	}
        	splay(newg); 
        	p[x]=p[y]=p[newg]=newg; 
        	X^=x,X^=y,X^=newg; 
        }      
    }; 
    int main()
    {
        // setIO("input");  
        init();    
        scanf("%d%d",&n,&Q);    
        for(int i=1;i<=n;++i) X^=i,sonmax[i].insert(3);  
        int x,y,a,b,c,d; 
        while(Q--)
        {
            scanf("%s",str);
            switch(str[0]) 
            {
                case 'A' : 
                {       
                    scanf("%d%d",&x,&y);  
                    tr::link(x,y); 
                    break;              
                }
                case 'Q' : 
                {
                    scanf("%d",&x);
                    printf("%d
    ",find(x));  
                    break; 
                }
                case 'X' : 
                {
                    printf("%d
    ",X);     
                    break; 
                }
            } 
        }
        return 0; 
    }
    

      

  • 相关阅读:
    3.创建第一个android项目
    2.SDK目录结构和adb工具及命令介绍
    1.安卓开发之环境搭建
    组成原理习题(一)
    微软职位内部推荐-Software Development Engineer II
    微软职位内部推荐-Enterprise Architect
    微软职位内部推荐-Senior NLP Scientist & Developer
    微软职位内部推荐-Sr DEV Lead, Bing Search Relevance
    微软职位内部推荐-Principal DEV Manager for Bing Client
    微软职位内部推荐-Principal Dev Manager for Windows Phone Shell
  • 原文地址:https://www.cnblogs.com/guangheli/p/11037849.html
Copyright © 2020-2023  润新知