您的位置:首页 > 运维架构

BZOJ 3510: 首都 LCT维护子树信息 启发式合并

2017-12-22 19:35 337 查看

3510: 首都

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 236  Solved: 89

[Submit][Status][Discuss]

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信息的结果。 

Sample Input

10 10

Xor

Q 1

A 10 1

A 1 4

Q 4

Q 10

A 7 6

Xor

Q 7

Xor

Sample Output

11

1

1

1

2

6

2

HINT

对于100%的数据,2<=N<=100000,1<=M<=200000。 

启发式合并把小树拆成点一个个搞进大树

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef double db;
typedef long long ll;

inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=100100;

int last
,ecnt;
struct EDGE{int to,nt;}e[N<<1];
inline void readd(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}
inline void add(int u,int v)
{readd(u,v);readd(v,u);}

int n,x_sum;

int ch
[2],fa
,size
,S
;
bool rev
;

inline bool isroot(int x)
{return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;}

inline void pushup(int x)
{size[x]=S[x]+size[ch[x][0]]+size[ch[x][1]]+1;}

inline void pushdown(int x)
{
if(rev[x])
{
rev[x]=0;
rev[ch[x][0]]^=1;rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
}
}

void getdown(int x)
{if(!isroot(x))getdown(fa[x]);pushdown(x);}

inline void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
l=(ch[y][1]==x);r=l^1;
if(!isroot(y)) ch[z][ch[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[ch[x][r]]=y;
ch[y][l]=ch[x][r];ch[x][r]=y;
pushup(y);pushup(x);
}

void splay(int x)
{
getdown(x);
int y,z;
while(!isroot(x))
{
y=fa[x],z=fa[y];
if(!isroot(y))
{
if((ch[z][0]==y)^(ch[y][0]==x)) rotate(x);
else rotate(y);
}
rotate(x);
}
}

void access(int u)
{int t=0;while(u){splay(u);S[u]+=size[ch[u][1]]-size[t];ch[u][1]=t;pushup(u);t=u;u=fa[u];}}

void rever(int x)
{access(x);splay(x);rev[x]^=1;}

int root
,Fa
,Siz
;
int find(int x)
{return Fa[x]==x ? x : Fa[x]=find(Fa[x]);}

void link(int u,int v)
{
rever(u);rever(v);
fa[u]=v;
S[v]+=size[u];
pushup(v);

rever(root[find(v)]);
access(u);
splay(root[find(v)]);
int tmp=size[root[find(v)]];
int tp=ch[root[find(v)]][1];
pushdown(tp);
while(ch[tp][0])
{
tp=ch[tp][0];
pushdown(tp);
}
access(tp);
if((S[tp]+1)<<1>tmp || ((S[tp]+1)<<1==tmp&&tp<root[find(v)]))
{
x_sum^=root[find(v)];
x_sum^=tp;
root[find(v)]=tp;
}
}

inline void clear(int x)
{size[x]=1;S[x]=fa[x]=ch[x][1]=ch[x][0]=0;rev[x]=0;}

void dfs(int u,int pre)
{
clear(u);
link(u,pre);
for(int i=last[u],v;i;i=e[i].nt)
if((v=e[i].to)!=pre)
dfs(v,u);
}

int main()
{
n=read();int Q=read();
register int i,u,v;
for(i=1;i<=n;++i)
x_sum^=i,size[i]=Siz[i]=1,Fa[i]=root[i]=i;
char opt[10];
while(Q--)
{
scanf("%s",opt);
switch(opt[0])
{
case 'A':
u=read();v=read();
if(Siz[find(u)]>Siz[find(v)]) swap(u,v);
Siz[find(v)]+=Siz[find(u)];
x_sum^=root[find(u)];
Fa[find(u)]=find(v);
dfs(u,v);add(u,v);
break;
case 'Q':
u=read();
print(root[find(u)]);puts("");
break;
case 'X':
print(x_sum);puts("");
break;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: