LCT,虚实链剖分。支持连边和断边操作。Tarjan制造。
当然这题分块可以做,常数小,但是LCT更无脑。
建立一个虚拟的弹飞节点,初始化时对于一个点假如再弹一次就弹飞了,连n+1,否则连弹到的点。维护一个查询就直接split找size就行了。修改的时候对应的link,cut就解决了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=300010;
int n,m,a[N],fa[N],ch[N][3],rev[N],size[N],stack[N],k,x,y;
bool nroot(int x){
return ch[fa[x]][0]==x||ch[fa[x]][1]==x;
}
void update(int x){
size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
void rever(int x){
swap(ch[x][1],ch[x][0]);
rev[x]^=1;
}
void pushdown(int x)
}
bool son(int x){
return ch[fa[x]][1]==x;
}
void rotate(int x)
void splay(int x)
}
void access(int x){
for(int y=0;x;x=fa[y=x]){
splay(x),ch[x][1]=y,update(x);
}
}
void makeroot(int x){
access(x);
splay(x);
rever(x);
}
int findroot(int x){
access(x);
splay(x);
while(ch[x][0])pushdown(x),x=ch[x][0];
return x;
}
void split(int x,int y){
makeroot(x);
access(y);
splay(y);
}
void link(int x,int y)
void cut(int x,int y)
}
int main()
scanf("%d",&m);
for(int i=1;i<=m;i++)
else
}
return 0;
}
这题并没有用到link,cut。用到了access。我们该怎么求答案呢?记为i点到跟有多少不同的颜色。然后就可以得到答案,子树最值?把点的dfn扔到线段树上就行了。怎么维护?因为只会把当前节点到根的颜色改变,自然想到access。我们把相同颜色的节点扔到一个splay中,然后操作一就是一个access了。还是不能维护?我们在access的过程中会断开当前splay的一部分,splay中连上一些点,断开时我们区间加,连接时我们区间减就行了。
#include<cstdio>
#include<algorithm>
#define MN 410001
#define lp (p<<1)
#define rp ((p<<1)|1)
using namespace std;
int read_p,read_ca;
inline int read()
struct na{int y,ne;}b[MN<<1];
int n,m,o,l[MN],num=0,x,y,fa[MN],ch[MN][2],df[MN],lo[MN],nm=0,v[MN],de[MN],ma[MN],s[MN],pdf[MN],f[MN][20];
bool rt[MN];
inline int max(int a,int b){return a>b?a:b;}
inline void in(int x,int y){b[++num].y=y;b[num].ne=l[x];l[x]=num;}
void rot(int k)
void splay(int x)
}
void add(int p,int l,int r,int L,int R,int v)
}
int ask(int p,int l,int r,int k)
int ask(int p,int l,int r,int L,int R)
int lf(int x){return ch[x][0]?lf(ch[x][0]):x;}
void acc(int x)
}
void dfs(int x)
void build(int p,int l,int r)
int mid=l+r>>1;
build(lp,l,mid);build(rp,mid+1,r);
ma[p]=max(ma[lp],ma[rp]);
}
int lca(int x,int y)
int main()
}
LCT维护动态图两点最长边。
首先有一个问题,如何用LCT维护边的信息?把边看做一个点,连接两个点时,用这两个点分别连接这一条边就行了。
此题,时间倒流,先把不删的边都建好,有环怎么办?我们找到x,y路径上最长的一条边如果比当前边大,删去,在把当前边连上去。否则不连当前边。找最长边可以用如下代码实现。
void update(int now)
然后split之后直接找根的mxid就行了。
不删的边建好后,时光倒流一个一个加边就行了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=300001;
int n,m,q,w[2000][2000],f[N],a[N],b[N],book[2000][2000],ans[N],hhh;
int mxid[N],ch[N][2],v[N],fa[N],rev[N],tot,stack[N];
struct ques{
int u,v,type;
}qu[N];
struct edge{
int u,v,w;
}e[N];
void update(int now)
bool isroot(int now){
return ch[fa[now]][1]!=now&&ch[fa[now]][0]!=now;
}
bool son(int now){
return ch[fa[now]][1]==now;
}
void rotate(int x)
void rever(int now){
swap(ch[now][1],ch[now][0]);
rev[now]^=1;
}
void pushdown(int now)
rev[now]=0;
}
void splay(int x)
rotate(x);
}
}
void access(int x){
for(int y=0;x;x=fa[y=x])
splay(x),ch[x][1]=y,update(x);
}
void makeroot(int x){
access(x);
splay(x);
rever(x);
}
void split(int x,int y){
makeroot(x);
access(y);
splay(y);
}
int findroot(int x){
access(x);
splay(x);
pushdown(x);
while(ch[x][0])pushdown(x=ch[x][0]);
return x;
}
void link(int x,int y)
void cut(int x,int y)
int find(int x)
int read()
while(ch>='0'&&ch<='9')
return sum*f;
}
bool cmp(edge a,edge b){
return a.w<b.w;
}
int main()
tot=n;
for(int i=1;i<=m;i++)
}
for(int i=q;i>=1;i--)
else continue;
}
f[fu]=fv;
tot++;v[tot]=w[qu[i].u][qu[i].v];
a[tot]=qu[i].u;b[tot]=qu[i].v;
link(tot,a[tot]);
link(tot,b[tot]);
}
else{
split(qu[i].u,qu[i].v);
ans[++hhh]=v[mxid[qu[i].v]];
}
}
for(int i=hhh;i>=1;i--)printf("%d
",ans[i]);
return 0;
}
获得新技能——虚点。我反正是想不出来。单点询问,区间操作的时候可以离线。像差分一样把区间分成两个点。一个插入一个删除。发现还是不好做,就要用到虚点了。预处理时每一个更换生长节点的操作我们建一个虚点接在上一个虚点上。我们跟据操作的时间顺序,每一次生长操作把生长出来的节点挂在最近的一个虚点上。我们离线扫整个区间的时候,扫到一个更改生长点的操作。就把对应的虚点挂在生长点上,到区间末尾时,在把虚点挂回原来的位置。这个过程中处理询问就行了。有一些问题,虚点这么多挂在树上不会影响询问吗?不会,我们把虚点的大小都设为0。把一个区间的生长出来的点挂在不交区间的虚点上不会错吗?不会,因为,当没有扫到虚点对应区间时,虚点回挂在上一个虚点上,又因为虚点大小为0,等价虚点不存在。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=500010;
int n,m,v[N],ch[N][3],sum[N],fa[N],size[N],now,ans[N],tot,num,nu,last,cnt,l[N],r[N],id[N];
bool isroot(int x){
return ch[fa[x]][0]==x||ch[fa[x]][1]==x;
}
void update(int x){
sum[x]=v[x]+sum[ch[x][1]]+sum[ch[x][0]];
}
bool son(int x){
return ch[fa[x]][1]==x;
}
void rotate(int x)
void splay(int x)
}
int access(int x){
int y=0;
for(;x;y=x,x=fa[x]){
splay(x);ch[x][1]=y;update(x);
}
return y;
}
void link(int x,int y){
splay(x);
fa[x]=y;
}
void cut(int x){
access(x);
splay(x);
fa[ch[x][0]]=0;ch[x][0]=0;
update(x);
}
int lca(int x,int y){
int tmp=0;
access(x);splay(x);tmp+=sum[x];
int t=access(y);splay(y);tmp+=sum[y];
access(t);splay(t);tmp-=2*sum[t];
return tmp;
}
struct ask{
int k,num,x,y;
friend bool operator <(const ask&a,const ask&b){
return a.k<b.k||(a.k==b.k&&a.num<b.num);
}
}q[N];
void newnode(int x){
v[++tot]=x;
sum[tot]=x;
}
int main()
else if(k==1)
}
else{
scanf("%d%d%d",&x,&a,&b);
q[++cnt].k=x;q[cnt].num=++nu;q[cnt].x=id[a];q[cnt].y=id[b];
}
}
sort(q+1,q+cnt+1);
for(int i=1,j=1;i<=n;i++)
else ans[q[j].num]=lca(q[j].x,q[j].y);
}
}
for(int i=1;i<=nu;i++)printf("%d
",ans[i]);
return 0;
}









