1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cmath>
5 #include<algorithm>
6 #include<cstring>
7 #include<vector>
8 #include<queue>
9 using namespace std;
10 typedef long long LL;
11 const int maxn=100010;
12 inline int read(){
13 int x=0,f=1;char ch=getchar();
14 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
15 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
16 return x*f;
17 }
18 int N,M;
19 int dep[maxn],siz[maxn],fa[maxn],son[maxn],top[maxn],id[maxn];
20 int val[maxn];
21 int num;
22 char s[20];
23 vector<int> to[maxn];
24 inline void dfs1(int rt,int fath,int deep){
25 dep[rt]=deep; siz[rt]=1; son[rt]=0; fa[rt]=fath;
26 for(int i=0;i<to[rt].size();i++){
27 int y=to[rt][i];
28 if(y!=fa[rt]){
29 dfs1(y,rt,deep+1);
30 siz[rt]+=siz[y];
31 if(siz[son[rt]]<siz[y]){
32 son[rt]=y;
33 }
34 }
35 }
36 }
37 inline void dfs2(int rt,int tp){
38 top[rt]=tp;
39 id[rt]=++num;
40 if(son[rt]!=0) dfs2(son[rt],tp);
41 for(int i=0;i<to[rt].size();i++){
42 int y=to[rt][i];
43 if(y!=fa[rt]&&y!=son[rt]){
44 dfs2(y,y);
45 }
46 }
47 }
48
49 struct Tree{
50 int l,r,sum,lazy;
51 }tree[maxn*8];
52 inline void build(int rt,int l,int r){
53 tree[rt].l=l; tree[rt].r=r;
54 if(l==r){
55 tree[rt].sum=0;
56 tree[rt].lazy=0;
57 return ;
58 }
59 int mid=(l+r)>>1;
60 build(rt<<1,l,mid); build(rt<<1|1,mid+1,r);
61 tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
62 }
63 inline void update_son(int rt){
64 int d=tree[rt].lazy;
65 if(d!=0){
66 tree[rt<<1].sum+=(tree[rt<<1].r-tree[rt<<1].l+1)*d;
67 if(tree[rt<<1].sum<0) tree[rt<<1].sum=0;
68 if(tree[rt<<1].sum>(tree[rt<<1].r-tree[rt<<1].l+1))
69 tree[rt<<1].sum=(tree[rt<<1].r-tree[rt<<1].l+1);
70 tree[rt<<1|1].sum+=(tree[rt<<1|1].r-tree[rt<<1|1].l+1)*d;
71 if(tree[rt<<1|1].sum<0) tree[rt<<1|1].sum=0;
72 if(tree[rt<<1|1].sum>(tree[rt<<1|1].r-tree[rt<<1|1].l+1))
73 tree[rt<<1|1].sum=(tree[rt<<1|1].r-tree[rt<<1|1].l+1);
74 tree[rt<<1].lazy=tree[rt].lazy; tree[rt<<1|1].lazy=tree[rt].lazy;
75 tree[rt].lazy=0;
76 }
77 }
78 inline void change(LL rt,LL l,LL r,LL delta){
79 if(l<=tree[rt].l&&tree[rt].r<=r){
80 tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*delta;
81 if(tree[rt].sum<0) tree[rt].sum=0;
82 if(tree[rt].sum>(tree[rt].r-tree[rt].l+1)) tree[rt].sum=(tree[rt].r-tree[rt].l+1);
83 tree[rt].lazy=delta;
84 return ;
85 }
86 update_son(rt);
87 LL mid=(tree[rt].l+tree[rt].r)>>1;
88 if(l<=mid) change(rt<<1,l,r,delta);
89 if(mid+1<=r) change(rt<<1|1,l,r,delta);
90 tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
91 }
92
93 inline LL query(LL rt,LL l,LL r){
94 if(l<=tree[rt].l&&tree[rt].r<=r){
95 return tree[rt].sum;
96 }
97 update_son(rt);
98 LL ans=0;
99 LL mid=(tree[rt].l+tree[rt].r)>>1;
100 if(l<=mid) ans+=query(rt<<1,l,r);
101 if(mid+1<=r) ans+=query(rt<<1|1,l,r);
102 return ans;
103 }
104 inline void ASK(int u,int v){
105 int tp1=top[u],tp2=top[v];
106 int ans=0,tmpv=v;
107 if(u==-1){//卸载
108 ans=query(1,id[v],id[v]);
109 if(ans==1){
110 ans=query(1,id[v],id[v]+siz[v]-1);//子树权值之和
111 printf("%d\n",ans);
112 change(1,id[v],id[v]+siz[v]-1,-1);
113 }
114 else printf("0\n");
115 }
116 else{//安装
117 ans=query(1,id[v],id[v]);
118 if(ans==0){
119 while(tp1!=tp2){
120 if(dep[tp1]<dep[tp2]){
121 swap(tp1,tp2);
122 swap(u,v);
123 }
124 ans+=query(1,id[tp1],id[u]);
125 change(1,id[tp1],id[u],1);
126 u=fa[tp1],tp1=top[u];
127 }
128 if(dep[u]>dep[v]) swap(u,v);
129 ans+=query(1,id[u],id[v]);
130 ans=dep[tmpv]-ans;
131 change(1,id[u],id[v],1);
132 printf("%d\n",ans);
133 }
134 else printf("0\n");
135 }
136 }
137 int main(){
138 N=read();
139 for(int i=2,u;i<=N;i++){
140 u=read(); u++;
141 to[u].push_back(i); to[i].push_back(u);
142 }
143 dfs1(1,0,1); dfs2(1,1);
144 build(1,1,num);
145 M=read();
146 for(int i=1,x;i<=M;i++){
147 scanf("%s%d",s,&x);
148 x++;
149 if(s[0]==‘i‘){
150 ASK(1,x);
151 }
152 else if(s[0]==‘u‘){
153 ASK(-1,x);
154 }
155 }
156 return 0;
157 }
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("makedata.out","w",stdout);
srand(time(0));
int N,M;
N=rand()%10+10000;
cout<<N<<endl;
for(int i=1;i<=N-1;i++){
int to=rand()%i;
cout<<to<<" ";
}
cout<<endl;
M=rand()%10+5000;
cout<<M<<endl;
for(int i=1;i<=M;i++){
int kin=rand()%2;
if(kin==0) cout<<"install"<<" ";
else cout<<"uninstall"<<" ";
cout<<rand()%N<<endl;
}
return 0;
}