这题真的就是个倍增求lca囧
不过卡空间。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 3000001
#define ll long long
#define inf 0x3f3f3f3f
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,fa[N][21],dep[N];
char op[5];
inline int lca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
int d=dep[x]-dep[y];
for(int i=0;(1<<i)<=d;++i)
if(d>>i&1) x=fa[x][i];
if(x==y) return x;
for(int i=20;i>=0;--i)
if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int main(){
// freopen("a.in","r",stdin);
int m=read();n=1;
while(m--){
scanf("%s",op+1);
if(op[1]=='A'){
int x=read();++n;fa[n][0]=x;dep[n]=dep[x]+1;
for(int i=1;i<=n;++i){
if(!fa[n][i-1]) break;
fa[n][i]=fa[fa[n][i-1]][i-1];
}continue;
}int k=read()-1,x=read();
while(k--){
int y=read();if(x!=1) x=lca(x,y);
}printf("%d\n",x);
}return 0;
}