【BZOJ2843】极地旅行社 离线+树链剖分+树状数组

【BZOJ2843】极地旅行社

Description

不久之前,Mirko建立了一个旅行社,名叫“极地之梦”。这家旅行社在北极附近购买了N座冰岛,并且提供观光服务。当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间。Mirko的旅行社遭受一次重大打击,以至于观光游轮已经不划算了。旅行社将在冰岛之间建造大桥,并用观光巴士来运载游客。Mirko希望开发一个电脑程序来管理这些大桥的建造过程,以免有不可预料的错误发生。这些冰岛从1到N标号。一开始时这些
岛屿没有大桥连接,并且所有岛上的帝企鹅数量都是知道的。每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间。你的程序需要处理以下三种命令:
1."bridge A B"——在A与B之间建立一座大桥(A与B是不同的岛屿)。由于经费限制,这项命令被接受,当且仅当A与B不联通。若这项命令被接受,你的程序需要输出"yes",之后会建造这座大桥。否则,你的程序需要输出"no"。
2."penguins A X"——根据可靠消息,岛屿A此时的帝企鹅数量变为X。这项命令只是用来提供信息的,你的程序不需要回应。
3."excursion A B"——一个旅行团希望从A出发到B。若A与B连通,你的程序需要输出这个旅行团一路上所能看到的帝企鹅数量(包括起点A与终点B),若不联通,你的程序需要输出"impossible"。

Input

第一行一个正整数N,表示冰岛的数量。
第二行N个范围[0, 1000]的整数,为每座岛屿初始的帝企鹅数量。
第三行一个正整数M,表示命令的数量。接下来M行即命令,为题目描述所示。
1<=N<=30000,1<=M<=100000

Output

对于每个bridge命令与excursion命令,输出一行,为题目描述所示。

Sample Input

5
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5

Sample Output

4
impossible
yes
6
yes
yes
15
yes
15
16

题解:网上大部分人都是用LCT写的,感觉用LCT代码应该会比树剖短不少~但是树剖常数还是小啊~

先离线读入整个图,用并查集判一下连那些边,然后无脑树剖。

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=30010;
const int maxm=100010;
int n,m,cnt;
int to[maxn<<1],next[maxn<<1],v[maxn],f[maxn],head[maxn],s[maxn],dep[maxn],fa[maxn],top[maxn];
int op[maxm],qa[maxm],qb[maxm],siz[maxn],son[maxn],p[maxn];
int rd()
{
	int ret=0,f=1;	char gc=getchar();
	while(gc<'0'||gc>'9')	{if(gc=='-')f=-f;	gc=getchar();}
	while(gc>='0'&&gc<='9')	ret=ret*10+gc-'0',gc=getchar();
	return ret*f;
}
char str[10];
int find(int x)
{
	return (f[x]==x)?x:(f[x]=find(f[x]));
}
void add(int a,int b)
{
	to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
void dfs1(int x)
{
	siz[x]=1;
	for(int i=head[x];i!=-1;i=next[i])	if(!dep[to[i]])
	{
		dep[to[i]]=dep[x]+1,fa[to[i]]=x,dfs1(to[i]),siz[x]+=siz[to[i]];
		if(siz[to[i]]>siz[son[x]])	son[x]=to[i];
	}
}
void dfs2(int x,int tp)
{
	top[x]=tp,p[x]=++p[0];
	if(son[x])	dfs2(son[x],tp);
	for(int i=head[x];i!=-1;i=next[i])	if(to[i]!=fa[x]&&to[i]!=son[x])	dfs2(to[i],to[i]);
}
void updata(int x,int val)
{
	for(int i=x;i<=n;i+=i&-i)	s[i]+=val;
}
int query(int x)
{
	int i,ret=0;
	for(i=x;i;i-=i&-i)	ret+=s[i];
	return ret;
}
int ask(int x,int y)
{
	int ret=0;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]])	swap(x,y);
		ret+=query(p[x])-query(p[top[x]]-1),x=fa[top[x]];
	}
	if(dep[x]>dep[y])	swap(x,y);
	ret+=query(p[y])-query(p[x]-1);
	return ret;
}
int main()
{
	n=rd();
	int i;
	for(i=1;i<=n;i++)	v[i]=rd(),f[i]=i;
	m=rd();
	memset(head,-1,sizeof(head));
	for(i=1;i<=m;i++)
	{
		scanf("%s",str),qa[i]=rd(),qb[i]=rd();
		if(str[0]=='b')
		{
			op[i]=1;
			if(find(qa[i])==find(qb[i]))	op[i]=-1;
			else	f[f[qa[i]]]=f[qb[i]],add(qa[i],qb[i]),add(qb[i],qa[i]);
		}
		if(str[0]=='p')	op[i]=2;
		if(str[0]=='e')
		{
			op[i]=3;
			if(find(qa[i])!=find(qb[i]))	op[i]=-3;
		}
	}
	for(i=1;i<=n;i++)	if(!dep[i])	dep[i]=1,dfs1(i),dfs2(i,i);
	for(i=1;i<=n;i++)	updata(p[i],v[i]);
	for(i=1;i<=m;i++)
	{
		if(op[i]==-1)	printf("no\n");
		if(op[i]==-3)	printf("impossible\n");
		if(op[i]==1)	printf("yes\n");
		if(op[i]==2)	updata(p[qa[i]],qb[i]-v[qa[i]]),v[qa[i]]=qb[i];
		if(op[i]==3)	printf("%d\n",ask(qa[i],qb[i]));
	}
	return 0;
}
posted @ 2017-07-16 08:55  CQzhangyu  阅读(217)  评论(0编辑  收藏  举报