Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher.
Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on.
Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks usingWorkforces personal messages.
There are two types of tasks:
- pow v describes a task to switch lights in the subtree of vertexv.
- get v describes a task to count the number of rooms in the subtree ofv, in which the light is turned on. Danil should send the answer to his boss usingWorkforces messages.
A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes throughv. In particular, the vertex v is in the subtree of v.
Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n - 1 space-separated integersp2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertexi.
The third line contains n space-separated integerst1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is1, if the light is turned on in vertex i and 0 otherwise.
The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks.
The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
For each task get v print the number of rooms in the subtree ofv, in which the light is turned on.
4 1 1 1 1 0 0 1 9 get 1 get 2 get 3 get 4 pow 1 get 1 get 2 get 3 get 4
2 0 0 1 2 1 1 0
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+10;
int tree[4*M],lazy[4*M],root=1,pre[4*M],cnt,tot,in[4*M],out[4*M],idx[4*M],node[4*M];
struct Edge
{
int u,v,next;
} edge[2*M];
void pushdown(int now,int l,int r)
{
int mid=(l+r)/2;
int lch=now*2,rch=now*2+1;
lazy[lch]+=lazy[now];
lazy[rch]+=lazy[now];
if(lazy[now]%2)
{
lazy[now]=0;
tree[lch]=(mid-l+1)-tree[lch];
tree[rch]=(r-mid)-tree[rch];
}
}
void update(int now,int l,int r,int al,int ar)
{
if(al>r||ar<l)
return;
if(l>=al&&r<=ar)
{
lazy[now]++;
tree[now]=(r-l+1)-tree[now];
return ;
}
pushdown(now,l,r);
int mid=(l+r)/2;
update(now*2,l,mid,al,ar);
update(now*2+1,mid+1,r,al,ar);
tree[now]=tree[now*2]+tree[now*2+1];
}
int query(int now,int l,int r,int al,int ar)
{
if(al>r||ar<l)
return 0;
if(l>=al&&r<=ar)
{
return tree[now];
}
pushdown(now,l,r);
int mid=(l+r)/2;
return query(now*2,l,mid,al,ar)+query(now*2+1,mid+1,r,al,ar);
}
void creat(int now,int l,int r,int x[])
{
lazy[now]=0;
if(l==r)
{
tree[now]=node[x[l]];
return ;
}
int mid=(l+r)/2;
creat(now*2,l,mid,x);
creat(now*2+1,mid+1,r,x);
tree[now]=tree[now*2]+tree[now*2+1];
}
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void dfs(int now,int fa)
{
in[now]=++tot;
idx[tot]=now;
for(int i=pre[now]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(v!=fa)
dfs(v,now);
}
out[now]=tot;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
cnt=0;
tot=0;
memset(pre,-1,sizeof(pre));
for(int i=2; i<=n; i++)
{
int tep;
scanf("%d",&tep);
add(tep,i);
add(i,tep);
}
for(int i=1; i<=n; i++)
scanf("%d",&node[i]);
dfs(1,1);
creat(1,1,n,idx);
int q;
scanf("%d",&q);
while(q--)
{
char op[10];
int nod;
scanf("%s%d",op,&nod);
if(op[0]=='g')
{
int l=in[nod];
int r=out[nod];
int ans=query(1,1,n,l,r);
printf("%d\n",ans);
}
else
{
int l=in[nod];
int r=out[nod];
update(1,1,n,l,r);
}
}
}
}