bzoj1095 [ZJOI2007]Hide 捉迷藏 线段树

90 篇文章 0 订阅
15 篇文章 0 订阅

Description


捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子。某天,Jiajia、Wind和孩子们决定在家里玩
捉迷藏游戏。他们的家很大且构造很奇特,由N个屋子和N-1条双向走廊组成,这N-1条走廊的分布使得任意两个屋
子都互相可达。游戏是这样进行的,孩子们负责躲藏,Jiajia负责找,而Wind负责操纵这N个屋子的灯。在起初的
时候,所有的灯都没有被打开。每一次,孩子们只会躲藏在没有开灯的房间中,但是为了增加刺激性,孩子们会要
求打开某个房间的电灯或者关闭某个房间的电灯。为了评估某一次游戏的复杂性,Jiajia希望知道可能的最远的两
个孩子的距离(即最远的两个关灯房间的距离)。 我们将以如下形式定义每一种操作: C(hange) i 改变第i个房
间的照明状态,若原来打开,则关闭;若原来关闭,则打开。 G(ame) 开始一次游戏,查询最远的两个关灯房间的
距离。

对于100%的数据, N ≤100000, M ≤500000。

Solution


蒟蒻的我并不会动态树分治这种东西

发现我们实际上要求一棵虚树的直径。有一个结论是:两棵树通过加边形成的新树的直径的两个端点一定是原两直径四个端点中的两个(绕,讨论一下共C(4,2)=6种情况
考虑用资瓷插入删除的splay维护这个东西。我们按dfs序为关键字,每次splay的时候向上合并俩儿子+自身共三棵树的信息。感觉这样是没有毛病的

然后我发现这道题好像并没有-1的点。。
这份代码交在洛谷上有70’,其中TLE的20‘可以通过rmq求lca搞定,剩下10’本寀基并不知道错在了哪里○| ̄|_弃疗

update


今天写了30min的线段树过了是什么鬼Σ(っ °Д °;)っ
大体思路是一致的,注意到我们并不需要对序列进行什么奇妙的操作,因此一棵线段树就可以解决问题了。

Code

线段树


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int N=200005;

struct edge {int x,y,next;} e[N*2];
struct line {int x,y,len;} p[N<<2];

int pos[N],dfn[N],fa[N],size[N],bl[N],dep[N];
int ls[N],edCnt,tot;

bool v[N];

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

void add_edge(int x,int y) {
	e[++edCnt]=(edge) {x,y,ls[x]}; ls[x]=edCnt;
	e[++edCnt]=(edge) {y,x,ls[y]}; ls[y]=edCnt;
}

void dfs1(int now) {
	size[now]=1;
	for (int i=ls[now];i;i=e[i].next) {
		if (e[i].y==fa[now]) continue;
		dep[e[i].y]=dep[now]+1; fa[e[i].y]=now;
		dfs1(e[i].y); size[now]+=size[e[i].y];
	}
}

void dfs2(int now,int up) {
	pos[now]=++pos[0]; dfn[pos[0]]=now;
	bl[now]=up; int mx=0;
	for (int i=ls[now];i;i=e[i].next) {
		if (e[i].y!=fa[now]&&size[e[i].y]>size[mx]) mx=e[i].y;
	}
	if (!mx) return ;
	dfs2(mx,up);
	for (int i=ls[now];i;i=e[i].next) {
		if (e[i].y!=fa[now]&&e[i].y!=mx) dfs2(e[i].y,e[i].y);
	}
}

int get_lca(int x,int y) {
	for (;bl[x]!=bl[y];) {
		if (dep[bl[x]]<dep[bl[y]]) std::swap(x,y);
		x=fa[bl[x]];
	}
	return (dep[x]<dep[y])?x:y;
}

int get_dis(int x,int y) {
	if (!x||!y) return 0;
	return dep[x]+dep[y]-2*dep[get_lca(x,y)];
}

line merge(line a,line b) {
	if (!a.x&&!a.y) return b;
	if (!b.x&&!b.y) return a;
	int x1=a.x,x2=b.x,y1=a.y,y2=b.y;
	line ret=(line) {x1,y1,a.len};
	if (get_dis(x1,x2)>ret.len) {
		ret.len=get_dis(x1,x2); ret.x=x1,ret.y=x2;
	}
	if (get_dis(x1,y2)>ret.len) {
		ret.len=get_dis(x1,y2); ret.x=x1,ret.y=y2;
	}
	if (get_dis(x2,y1)>ret.len) {
		ret.len=get_dis(x2,y1); ret.x=x2,ret.y=y1;
	}
	if (get_dis(x2,y2)>ret.len) {
		ret.len=get_dis(x2,y2); ret.x=x2,ret.y=y2;
	}
	if (get_dis(y1,y2)>ret.len) {
		ret.len=get_dis(y1,y2); ret.x=y1,ret.y=y2;
	}
	return ret;
}

void modify(int now,int tl,int tr,int x) {
	if (tl==tr) {
		if (v[x]) p[now]=(line) {0,0,0},tot--;
		v[x]^=1;
		if (v[x]) p[now]=(line) {dfn[x],dfn[x],0},tot++;
		return ;
	}
	int mid=(tl+tr)>>1;
	if (x<=mid) modify(now<<1,tl,mid,x);
	else modify(now<<1|1,mid+1,tr,x);
	p[now]=merge(p[now<<1],p[now<<1|1]);
}

int main(void) {
	freopen("data.in","r",stdin);
	freopen("myp.out","w",stdout);
	int n=read(); tot=0;
	rep(i,2,n) add_edge(read(),read());
	dfs1(dep[1]=1); dfs2(1,1);
	rep(i,1,n) modify(1,1,n,pos[i]);
	for (int T=read(),opt,x;T--;) {
		for (opt=getchar();opt!='G'&&opt!='C';opt=getchar()) ;
		if (opt=='G') {
			if (tot==1) puts("0");
			else if (tot==0) puts("-1");
			else printf("%d\n", p[1].len);
		} else {
			x=read();
			modify(1,1,n,pos[x]);
		}
	}
	return 0;
}

splay


#pragma GCC optimize(3)
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int N=200005;

struct edge {int y,next;} e[N*2];
struct line {int x,y,len;} ;
struct treeNode {int son[2],fa; line p;} t[N];

int dep[N],pos[N],bl[N],size[N],fa[N];
int ls[N],root,edCnt,n;

bool v[N];

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

void add_edge(int x,int y) {
	e[++edCnt]=(edge) {y,ls[x]}; ls[x]=edCnt;
	e[++edCnt]=(edge) {x,ls[y]}; ls[y]=edCnt;
}

int get_lca(int x,int y) {
	for (;bl[x]!=bl[y];) {
		if (dep[bl[x]]<dep[bl[y]]) std:: swap(x,y);
		x=fa[bl[x]];
	}
	return (dep[x]<dep[y])?x:y;
}

int get_dis(int x,int y) {
	if (!(x*y)) return 0;
	int lca=get_lca(x,y);
	return dep[x]+dep[y]-dep[lca]*2;
}

line merge(line a,line b) {
	int x1=a.x,x2=b.x,y1=a.y,y2=b.y;
	line ret=(line) {x1,y1,a.len};
	if (get_dis(x1,x2)>ret.len) {
		ret.len=get_dis(x1,x2); ret.x=x1,ret.y=x2;
	}
	if (get_dis(x1,y2)>ret.len) {
		ret.len=get_dis(x1,y2); ret.x=x1,ret.y=y2;
	}
	if (get_dis(x2,y1)>ret.len) {
		ret.len=get_dis(x2,y1); ret.x=x2,ret.y=y1;
	}
	if (get_dis(x2,y2)>ret.len) {
		ret.len=get_dis(x2,y2); ret.x=x2,ret.y=y2;
	}
	if (get_dis(y1,y2)>ret.len) {
		ret.len=get_dis(y1,y2); ret.x=y1,ret.y=y2;
	}
	return ret;
}

void push_up(int x) {
	t[x].p=(line) {x,x,0};
	if (x>n) t[x].p=(line) {0,0,0};
	if (t[x].son[0]) t[x].p=merge(t[x].p,t[t[x].son[0]].p);
	if (t[x].son[1]) t[x].p=merge(t[x].p,t[t[x].son[1]].p);
}

void rotate(int x) {
	int y=t[x].fa; int z=t[y].fa;
	int k=t[y].son[1]==x;
	t[x].fa=z; t[z].son[t[z].son[1]==y]=x;
	t[y].son[k]=t[x].son[!k]; t[t[x].son[!k]].fa=y;
	t[y].fa=x; t[x].son[!k]=y;
	push_up(y); push_up(x);
}

void splay(int x,int goal=0) {
	for (;t[x].fa!=goal;) {
		int y=t[x].fa; int z=t[y].fa;
		if (z!=goal) {
			if ((t[z].son[1]==y)^(t[y].son[1]==x)) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
	if (!goal) root=x;
}

int get_pre(int x) {
	splay(x);
	int y=t[x].son[0];
	for (;t[y].son[1];) y=t[y].son[1];
	// splay(y);
	return y;
}

int get_nex(int x) {
	splay(x);
	int y=t[x].son[1];
	for (;t[y].son[0];) y=t[y].son[0];
	// splay(y);
	return y;
}

void del(int x) {
	int pre=get_pre(x);
	int nex=get_nex(x);
	splay(pre); splay(nex,pre);
	t[t[nex].son[0]].fa=0;
	t[t[nex].son[0]].p=(line) {t[nex].son[0],t[nex].son[0],0};
	t[nex].son[0]=0; push_up(nex);
	splay(nex);
}

void ins(int x) {
	int y=root,fa=0;
	for (;y;) fa=y,y=t[y].son[pos[x]>pos[y]];
	t[x].fa=fa; t[fa].son[pos[x]>pos[fa]]=x;
	t[x].p=(line) {x,x,0};
	splay(x);
}

void dfs1(int now) {
	size[now]=1;
	for (int i=ls[now];i;i=e[i].next) {
		if (e[i].y==fa[now]) continue;
		fa[e[i].y]=now; dep[e[i].y]=dep[now]+1;
		dfs1(e[i].y); size[now]+=size[e[i].y];
	}
}

void dfs2(int now,int up) {
	bl[now]=up; pos[now]=++pos[0];
	int mx=0;
	for (int i=ls[now];i;i=e[i].next) {
		if (e[i].y!=fa[now]&&size[e[i].y]>size[mx]) mx=e[i].y;
	}
	if (!mx) return ;
	dfs2(mx,up);
	for (int i=ls[now];i;i=e[i].next) {
		if (e[i].y!=fa[now]&&e[i].y!=mx) dfs2(e[i].y,e[i].y);
	}
}

int main(void) {
	freopen("data.in","r",stdin);
	freopen("myp.out","w",stdout);
	n=read(); int tot=n;
	rep(i,2,n) add_edge(read(),read());
	dfs1(dep[1]=1); dfs2(1,1);
	pos[n+1]=-1; t[n+1].fa=n+2;
	pos[n+2]=n+2; t[n+2].son[0]=n+1; root=n+2;
	rep(i,1,n) ins(i),v[i]=true;
	for (int T=read(),opt;T--;) {
		for (opt=getchar();opt!='G'&&opt!='C';opt=getchar()) ;
		if (opt=='C') {
			int x=read();
			if (v[x]) del(x),tot--;
			v[x]^=1;
			if (v[x]) ins(x),tot++;
		} else {
			if (tot==0) puts("-1");
			else if (tot==1) puts("0");
			else printf("%d\n", t[root].p.len);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值