bzoj2959 长跑 LCT+并查集

29 篇文章 0 订阅
13 篇文章 0 订阅

#Description

某校开展了同学们喜闻乐见的阳光长跑活动。为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动。一时间操场上熙熙攘攘,摩肩接踵,盛况空前。
  为了让同学们更好地监督自己,学校推行了刷卡机制。
  学校中有n个地点,用1到n的整数表示,每个地点设有若干个刷卡机。
  有以下三类事件:
  1、修建了一条连接A地点和B地点的跑道。
  2、A点的刷卡机台数变为了B。
  3、进行了一次长跑。问一个同学从A出发,最后到达B最多可以刷卡多少次。具体的要求如下:
  当同学到达一个地点时,他可以在这里的每一台刷卡机上都刷卡。但每台刷卡机只能刷卡一次,即使多次到达同一地点也不能多次刷卡。
  为了安全起见,每条跑道都需要设定一个方向,这条跑道只能按照这个方向单向通行。最多的刷卡次数即为在任意设定跑道方向,按照任意路径从A地点到B地点能刷卡的最多次数。
Input

输入的第一行包含两个正整数n,m,表示地点的个数和操作的个数。
  第二行包含n个非负整数,其中第i个数为第个地点最开始刷卡机的台数。
  接下来有m行,每行包含三个非负整数P,A,B,P为事件类型,A,B为事件的两个参数。
  最初所有地点之间都没有跑道。
  每行相邻的两个数之间均用一个空格隔开。表示地点编号的数均在1到n之间,每个地点的刷卡机台数始终不超过10000,P=1,2,3。

对于100%的数据,m<=5n,任意时刻,每个地点的刷卡机台数不超过10000。N<=1.5×105
#Solution

考虑怎么做无加边的情况。我们发现一个双连通分量中的所有点都能互达,因此只需要缩点然后树链剖分就可以了

现在多了加边操作。我们用LCT维护维护双连通分量的过程。如果一条边连接了两个连通的块,那么我们把这条链提出来,把除深度最浅的点以外的所有点指向最浅的点,并把权值转移到最浅点上。

于是乎两个并查集+一个LCT就能做了。细节比较多,注意全部加上getfhater就行了 囧
我的代码并不能跑过bzoj的数据,可能是我写挂了也可能是我写挂了,看看就行
#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;

namespace LCT {
	struct treeNode {int son[2],fa,is_root,sum,v,rev;} t[N];
	int fa[N];

	int get_father(int x) {
		if (fa[x]==x) return x;
		return fa[x]=get_father(fa[x]);
	}

	void push_down(int x) {
		if (!t[x].rev) return ; t[x].rev=0;
		std:: swap(t[x].son[0],t[x].son[1]);
		if (t[x].son[0]) t[t[x].son[0]].rev^=1;
		if (t[x].son[1]) t[t[x].son[1]].rev^=1;
	}

	void push_up(int x) {
		if (!x) return ;
		t[x].sum=t[x].v;
		if (t[x].son[0]) t[x].sum+=t[t[x].son[0]].sum;
		if (t[x].son[1]) t[x].sum+=t[t[x].son[1]].sum;
	}

	void rotate(int x) {
		if (t[x].is_root) return ;
		int y=t[x].fa=get_father(t[x].fa);
		int z=t[y].fa=get_father(t[y].fa); int k=t[y].son[1]==x;
		t[x].fa=z;
		if (t[x].son[!k]) t[t[x].son[!k]].fa=y;
		t[y].son[k]=t[x].son[!k];
		t[y].fa=x; t[x].son[!k]=y;
		if (t[y].is_root) {
			t[y].is_root=0;
			t[x].is_root=1;
		} else t[z].son[t[z].son[1]==y]=x;
		push_up(y); push_up(x);
	}

	void remove(int x) {
		if (!t[x].is_root) {
			t[x].fa=get_father(t[x].fa);
			remove(t[x].fa);
		}
		push_down(x);
	}

	void splay(int x) {
		remove(x);
		for (;!t[x].is_root;) {
			int y=t[x].fa=get_father(t[x].fa); int z=t[y].fa=get_father(t[y].fa);
			if (!t[y].is_root) {
				if ((t[z].son[1]==y)^(t[y].son[1]==x)) rotate(x);
				else rotate(y);
			}
			rotate(x);
		}
	}

	void access(int x) {
		int y=0;
		while (x) {
			splay(x);
			t[t[x].son[1]].is_root=1;
			t[t[x].son[1]=y].is_root=0;
			push_up(x);
			y=x; x=t[x].fa=get_father(t[x].fa);
		}
	}

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

	void link(int x,int y) {
		mroot(x); access(y); splay(y);
		t[x].fa=y;
	}

	void dfs(int st,int x) {
		if (x!=st) t[st].v+=t[x].v;
		fa[get_father(x)]=get_father(st);
		if (t[x].son[0]) dfs(st,t[x].son[0]);
		if (t[x].son[1]) dfs(st,t[x].son[1]);
		t[x].son[0]=t[x].son[1]=0;
	}
} ;

int fa[N],val[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;
}

int get_father(int x) {
	if (fa[x]==x) return x;
	return fa[x]=get_father(fa[x]);
}

bool merge(int x,int y) {
	x=get_father(x); y=get_father(y);
	if (x==y) return false;
	fa[x]=y; return true;
}

int main(void) {
	freopen("data.in","r",stdin);
	// freopen("graph.out","w",stdout);
	int n=read(),m=read();
	rep(i,1,n) {
		LCT:: t[i].v=LCT:: t[i].sum=read();
		fa[i]=LCT:: fa[i]=i; LCT:: t[i].is_root=1;
	}
	int ggg=0;
	for (;m--;) {
		int opt=read(),x=read(),y=read();
		if (opt==3&&x==3&&y==6) {
			ggg++;
		}
		if (opt==1) {
			if (merge(x,y)) {
				x=LCT:: get_father(x);
				y=LCT:: get_father(y);
				LCT:: link(x,y);
			} else {
				x=LCT:: get_father(x);
				y=LCT:: get_father(y);
				LCT:: mroot(x);
				LCT:: access(y);
				LCT:: splay(x);
				LCT:: dfs(x,x);
			}
		} else if (opt==3) {
			if (get_father(x)!=get_father(y)) {
				puts("-1"); continue;
			} x=LCT:: get_father(x); y=LCT:: get_father(y);
			LCT:: mroot(x);
			LCT:: access(y);
			LCT:: splay(x);
			printf("%d\n", LCT:: t[x].sum);
		} else if (opt==2) { x=LCT:: get_father(x);
			LCT:: mroot(x);
			LCT:: t[x].v=y;
			LCT:: push_up(x);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值