1. 程式人生 > >【BZOJ4704】旅行 樹鏈剖分+可持久化線段樹

【BZOJ4704】旅行 樹鏈剖分+可持久化線段樹

-s 編號 不能 ets include 出發 oot %d rip

【BZOJ4704】旅行

Description

在Berland,有n個城堡。每個城堡恰好屬於一個領主。不同的城堡屬於不同的領主。在所有領主中有一個是國王,其他的每個領主都直接隸屬於另一位領主,並且間接隸屬於國王。一位領主可以擁有任意數量的下屬。這些城堡被一些雙向的道路連接。兩個城堡是連接的當且僅當他們的主人中一位直接隸屬於另一位。每一年,在Berland會發生以下兩件事中的一件:1.野蠻人攻擊了城堡c。這是城堡c第一次也會是最後一次被攻擊,因為野蠻人從來不攻擊同一座城堡超過一次。2.一個騎士從城堡a出發前往到城堡b。騎士從不重復經過同一座城堡,因此他的路線是唯一確定的。現在考慮第二類事件。由於從城堡a到b的路途遙遠,每個騎士會在他經過的某個城堡停下來休息一次。根據規則,騎士不能停留在第 y 年以後受到過攻擊的城堡中。所以,騎士選擇了途徑的第k個從第 y+1 年開始到現在(當時)沒有被攻擊過的城堡(不算城堡a和b)。你,偉大的歷史學家,知道Berland歷史上的所有m個事件。請你計算,每個騎士是在哪個城堡休息的。如果在從城堡a到城堡b的路上少於k座城堡,那麽你可以斷定有關這個騎士的記載是有誤的。

Input

第一行包含一個整數N,表示城堡數目。 第二行包含N個整數,依次表示編號為 1...N 的城堡領主的上級。特別的,國王沒有上級,故用 0 表示。 第三行包含一個整數M,表示事件數目。 接下來M行,每行描述一個事件: 若是第1類事件,則包含兩個整數,依次是1,ci; 若是第2類事件,則包含五個整數,依次是2,ai,bi,ki,yi。 1 ≤ N, M ≤ 10^5,1 ≤ ai, bi, ci, ki ≤ N,0 ≤ yi < i, 每個事件中 ai ≠ bi

Output

輸出若幹行,每行一個整數,依次表示每個騎士休息的城堡編號。若騎士不可能休息,則輸出 -1。

Sample Input

3
0 1 2
5
2 1 3 1 0
1 2
2 1 3 1 0
2 1 3 1 1
2 1 3 1 2

Sample Output

2
-1
-1
2

題解:y年後沒有被攻擊過的城堡數量=總數-被攻擊過的城堡數量+y年前被攻擊過的城堡數量。這個用可持久化線段樹很容易維護。然後如何找第k個呢?我們沿著樹剖的路徑一直走,如果算上當前鏈後不足k個,那麽繼續看下一條鏈,否則在鏈上二分。因為二分只會進行一次,所以復雜度是$O(nlog^2n)$的。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=200010;
int n,m,cnt,tot,root,K,Y;
int to[maxn],next[maxn],head[maxn],fa[maxn],son[maxn],siz[maxn],dep[maxn],top[maxn],p[maxn],q[maxn],bit[maxn],rt[maxn];
int st[maxn],vis[maxn];
struct sag
{
	int ls,rs,siz;
}s[maxn*100];
void dfs1(int x)
{
	siz[x]=1;
	for(int i=head[x];i!=-1;i=next[i])
	{
		dep[to[i]]=dep[x]+1,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],q[p[0]]=x;
	if(son[x])	dfs2(son[x],tp);
	for(int i=head[x];i!=-1;i=next[i])	if(to[i]!=son[x])	dfs2(to[i],to[i]);
}
inline void updata(int x)
{
	for(int i=x;i<=n;i+=i&-i)	bit[i]++;
}
inline int getsum(int x)
{
	int i,ret=0;
	for(i=x;i;i-=i&-i)	ret+=bit[i];
	return ret;
}
void insert(int x,int &y,int l,int r,int a)
{
	y=++tot,s[y].siz=s[x].siz+1;
	if(l==r)	return ;
	int mid=(l+r)>>1;
	if(a<=mid)	s[y].rs=s[x].rs,insert(s[x].ls,s[y].ls,l,mid,a);
	else	s[y].ls=s[x].ls,insert(s[x].rs,s[y].rs,mid+1,r,a);
}
int query(int l,int r,int x,int a,int b)
{
	if(!x||(a<=l&&r<=b))	return s[x].siz;
	int mid=(l+r)>>1;
	if(b<=mid)	return query(l,mid,s[x].ls,a,b);
	if(a>mid)	return query(mid+1,r,s[x].rs,a,b);
	return query(l,mid,s[x].ls,a,b)+query(mid+1,r,s[x].rs,a,b);
}
inline int calc(int a,int b)
{
	return b-a+1-getsum(b)+getsum(a-1)+query(1,n,rt[Y],a,b);
}
int ask(int x,int y)
{
	st[0]=0;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]>dep[top[y]])
		{
			int tmp=calc(p[top[x]],p[x]);
			if(tmp>=K)
			{
				int l=p[top[x]],r=p[x]+1,mid;
				while(l<r)
				{
					mid=(l+r)>>1;
					if(calc(mid,p[x])>=K)	l=mid+1;
					else	r=mid;
				}
				return q[l-1];
			}
			else	K-=tmp,x=fa[top[x]];
		}
		else	st[++st[0]]=y,y=fa[top[y]];
	}
	if(dep[x]>dep[y])
	{
		int tmp=calc(p[y],p[x]);
		if(tmp>=K)
		{
			int l=p[y],r=p[x]+1,mid;
			while(l<r)
			{
				mid=(l+r)>>1;
				if(calc(mid,p[x])>=K)	l=mid+1;
				else	r=mid;
			}
			return q[l-1];
		}
		else	K-=tmp;
	}
	else
	{
		int tmp=calc(p[x],p[y]);
		if(tmp>=K)
		{
			int l=p[x],r=p[y],mid;
			while(l<r)
			{
				mid=(l+r)>>1;
				if(calc(p[x],mid)>=K)	r=mid;
				else	l=mid+1;
			}
			return q[r];
		}
		else	K-=tmp;
	}
	for(int i=st[0];i;i--)
	{
		y=st[i];
		int tmp=calc(p[top[y]],p[y]);
		if(tmp>=K)
		{
			int l=p[top[y]],r=p[y],mid;
			while(l<r)
			{
				mid=(l+r)>>1;
				if(calc(p[top[y]],mid)>=K)	r=mid;
				else	l=mid+1;
			}
			return q[r];
		}
		else	K-=tmp;
	}
	return -1;
}
inline void add(int a,int b)
{
	to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
inline 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;
}
int main()
{
	//freopen("bz4704.in","r",stdin);
	//freopen("bz4704.out","w",stdout);
	n=rd();
	int i,a,b;
	int M=0;
	memset(head,-1,sizeof(head));
	for(i=1;i<=n;i++)
	{
		fa[i]=rd();
		if(fa[i])	add(fa[i],i);
		else	root=i;
	}
	dep[root]=1,dfs1(root),dfs2(root,root);
	m=rd();
	for(i=1;i<=m;i++)
	{
		if(rd()==1)	a=rd(),vis[a]=i,updata(p[a]),insert(rt[i-1],rt[i],1,n,p[a]);
		else
		{
			M++;
			rt[i]=rt[i-1],a=rd(),b=rd(),K=rd(),Y=rd(),K+=(vis[a]<=Y);
			int tmp=ask(a,b);
			if(tmp==b)	printf("-1\n");
			else	printf("%d\n",tmp);
		}
	}
	return 0;
}//3 0 1 2 5 2 1 3 1 0 1 2 2 1 3 1 0 2 1 3 1 1 2 1 3 1 2 

【BZOJ4704】旅行 樹鏈剖分+可持久化線段樹