1 /**************************************************************
2 Problem: 4448
3 User: __abcdef__
4 Language: C++
5 Result: Accepted
6 Time:6916 ms
7 Memory:90304 kb
8 ****************************************************************/
9
10 #include<iostream>
11 #include<cstdio>
12 #include<cstdlib>
13 #include<cstring>
14 #include<cmath>
15 #include<algorithm>
16 #include<queue>
17 #include<vector>
18 using namespace std;
19 typedef long long LL;
20 const int inf=1e9;
21 const int maxn=2*1e5+5;
22 int N,Q;
23 int dep[maxn],fa[maxn],p[maxn][32],sum[maxn][32],ANS[maxn];
24 vector<int> son[maxn],now;
25 struct questi{
26 int kin,from,to,c;
27 int num;
28 int id,ans1,ans2;
29 int tim;
30 }q[maxn];
31 inline bool cmp1(const questi & a,const questi & b){
32 return a.tim<b.tim||(a.tim==b.tim&&a.kin>b.kin);
33 }
34 inline bool cmp2(const questi & a,const questi & b){
35 return a.id<b.id;
36 }
37 inline void dfs(int rt){
38 for(int i=0;i<(int)son[rt].size();i++){
39 int y=son[rt][i];
40 dep[y]=dep[rt]+1;
41 p[y][0]=rt; sum[y][0]=1;
42 for(int k=1;k<=30;k++){
43 int zu=1<<k;
44 if(zu<=dep[y]){
45 p[y][k]=p[p[y][k-1]][k-1];
46 sum[y][k]=sum[y][k-1]+sum[p[y][k-1]][k-1];
47 }
48 else break;
49 }
50 dfs(y);
51 }
52 }
53 inline int LCA(int x,int y){
54 int ans=0;
55 if(dep[x]>dep[y]) swap(x,y);
56 int delta=dep[y]-dep[x];
57 for(int i=0;i<=30;i++){
58 int h=(1<<i); h=(h&delta);
59 if(h!=0){
60 ans+=sum[y][i];
61 y=p[y][i];
62 }
63 }
64 if(x==y) return ans;
65 for(int i=30;i>=0;i--){
66 if(p[y][i]!=p[x][i]){
67 ans+=(sum[y][i]+sum[x][i]);
68 y=p[y][i]; x=p[x][i];
69 }
70 }
71 ans+=sum[y][0]; ans+=sum[x][0];
72 return ans;
73 }
74 int dep2[maxn],siz[maxn],Son[maxn],id[maxn],top[maxn],tot;
75 inline void dfs1(int rt,int fath,int deep){
76 dep2[rt]=deep; siz[rt]=1; Son[rt]=0;
77 for(int i=0;i<(int)son[rt].size();i++){
78 int y=son[rt][i];
79 dfs1(y,rt,deep+1);
80 siz[rt]+=siz[y];
81 if(siz[y]>siz[Son[rt]]){
82 Son[rt]=y;
83 }
84 }
85 }
86 inline void dfs2(int rt,int tp){
87 top[rt]=tp; id[rt]=++tot;
88 if(Son[rt]!=0) dfs2(Son[rt],tp);
89 for(int i=0;i<(int)son[rt].size();i++){
90 int y=son[rt][i];
91 if(y!=Son[rt]) dfs2(y,y);
92 }
93 }
94 struct Tree{
95 int l,r;
96 int sum;
97 }T[maxn*8];
98 inline void build(int rt,int l,int r){
99 T[rt].l=l; T[rt].r=r;
100 if(l==r){
101 T[rt].sum=0;
102 return ;
103 }
104 int mid=(l+r)>>1;
105 build(rt<<1,l,mid); build(rt<<1|1,mid+1,r);
106 T[rt].sum=T[rt<<1].sum+T[rt<<1|1].sum;
107 }
108 inline void change(int rt,int pos,int v){
109 if(T[rt].l==T[rt].r){
110 T[rt].sum=v;
111 return ;
112 }
113 int mid=(T[rt].l+T[rt].r)>>1;
114 if(pos<=mid) change(rt<<1,pos,v);
115 else change(rt<<1|1,pos,v);
116 T[rt].sum=T[rt<<1].sum+T[rt<<1|1].sum;
117 }
118 inline int query(int rt,int l,int r){
119 if(l<=T[rt].l&&T[rt].r<=r){
120 return T[rt].sum;
121 }
122 int mid=(T[rt].l+T[rt].r)>>1;
123 int ans=0;
124 if(l<=mid) ans+=query(rt<<1,l,r);
125 if(mid+1<=r) ans+=query(rt<<1|1,l,r);
126 return ans;
127 }
128 inline int Yougth(int u,int v){
129 int tp1=top[u],tp2=top[v];
130 if(u==v) return query(1,id[u],id[v]);
131 int ans=0;
132 while(tp1!=tp2){
133 if(dep[tp1]<dep[tp2]){
134 swap(tp1,tp2);
135 swap(u,v);
136 }
137 ans+=query(1,id[tp1],id[u]);
138 u=fa[tp1]; tp1=top[u];
139 }
140 if(dep[u]>dep[v]) swap(u,v);
141 ans+=query(1,id[u],id[v]);
142 return ans;
143 }
144 int main(){
145 scanf("%d",&N);
146 for(int i=1;i<=N;i++){
147 scanf("%d",&fa[i]);
148 son[fa[i]].push_back(i);
149 }
150 scanf("%d",&Q);
151 for(int i=1;i<=Q;i++){
152 scanf("%d",&q[i].kin);
153 if(q[i].kin==1){
154 scanf("%d%d%d",&q[i].from,&q[i].to,&q[i].c);
155 q[i].tim=i-q[i].c-1;
156 q[i].id=i;
157 }
158 else{
159 scanf("%d",&q[i].num);
160 q[i].tim=i;
161 q[i].id=i;
162 }
163 }
164 sort(q+1,q+Q+1,cmp1);
165 for(int i=1;i<=N;i++){
166 if(fa[i]==0){
167 dep[i]=0; p[i][0]=-1; dfs(i);
168 dfs1(i,-1,1); dfs2(i,i);
169 break;
170 }
171 }
172 build(1,1,N);
173 for(int i=1;i<=Q;i++){
174 if(q[i].kin==2){
175 change(1,id[q[i].num],1);
176 }
177 else{
178 q[i].ans1=LCA(q[i].from,q[i].to)+1;
179 q[i].ans2=Yougth(q[i].from,q[i].to);
180 }
181 }
182 sort(q+1,q+Q+1,cmp2);
183 for(int i=1;i<=Q;i++){
184 if(q[i].kin==1){
185 printf("%d %d\n",q[i].ans1,q[i].ans2);
186 }
187 }
188 return 0;
189 }