Codechef FGTREE Forgotten Tree 9

Link
考虑维护一个栈,满足栈中的某个点在它前一个点的右子树内,同时维护每个点子树内的最小编号\(lz_u\)
考虑按照编号一次往栈中加点,假如我们现在要加入点\(u\),如果栈顶的元素在\(u\)的子树内就将其弹出栈。
注意此时栈底往上存的是\(u\)的左儿子往右儿子跳的链,因此对于栈顶\(v\),我们只需要\(\text{query(}v,l_v,u-1\text{)}\)即可判断\(v\)是否在\(u\)子树内。
最后一个被弹掉的点就是\(u\)的左儿子。

#include<cstdio>
#include<cstring>
const int N=107;
int n,stk[N],top,lz[N],fa[N];char str[4];
int read(){int x;scanf("%d",&x);return x;}
int query(int x,int l,int r){return printf("Q %d %d %d\n",x,l,r),fflush(stdout),scanf("%s",str),str[0]=='Y';}
int main()
{
    for(int t=read();t;--t)
    {
	n=read(),memset(lz+1,0,4*n),top=0;
	for(int i=1,ls;i<=n;stk[++top]=i++)
	{
	    for(ls=0;top&&query(stk[top],lz[stk[top]],i-1);--top) ls=stk[top],fa[stk[top]]=stk[top-1];
	    if(lz[i]=i,ls) fa[ls]=i,lz[i]=lz[ls];
	}
	while(top>1) fa[stk[top]]=stk[top-1],--top;
	fa[stk[1]]=-1,printf("A ");
	for(int i=1;i<=n;++i) printf("%d ",fa[i]);
	puts(""),fflush(stdout);
    } 
}

猜你喜欢

转载自www.cnblogs.com/cjoierShiina-Mashiro/p/12905476.html