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 using Workforces personal messages.
There are two types of tasks:
pow v describes a task to switch lights in the subtree of vertex v.
get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages.
A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. 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.
Input
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 integers p2, p3, …, pn (1 ≤ pi < i), where pi is the ancestor of vertex i.
The third line contains n space-separated integers t1, t2, …, tn (0 ≤ ti ≤ 1), where ti is 1, 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.
Output
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
Example
Input
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
Output
2
0
0
1
2
1
1
0
The tree before the task pow 1.
The tree after the task pow 1.
对于树上以某个点为顶点的子树操作,就是dfs+线段树了。dfs序将上节点的编号变为连续的,用线段树去处理一系列的操作。
对于这个题,把以x为顶点的子树的灯都改变状态,改变状态两次就是原来的状态了。具体解释看代码
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=2e5+100;
struct node{
int l;
int r;
int lazy;
int sum;
}p[maxx<<2];
struct edge{
int to;
int next;
}e[maxx];
int head[maxx<<1],in[maxx],out[maxx],pre[maxx];
int a[maxx];
int n,m,tot,sign;
/*--------------事前准备-------------*/
inline void add(int u,int v)
{
e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
inline void init()
{
memset(head,-1,sizeof(head));
tot=0;sign=0;
}
/*------------dfs序---------------*/
inline void dfs(int u,int f)
{
in[u]=++sign;
pre[sign]=u;//!!!这里别忘了,我忘了wa到死。
for(int i=head[u];i!=-1;i=e[i].next)
{
int to=e[i].to;
if(to==f) continue;
dfs(to,u);
}
out[u]=sign;
}
/*----------------线段树---------------*/
inline void pushdown(int cur)
{
if(p[cur].lazy)//如果改变状态
{
p[cur<<1].sum=p[cur<<1].r-p[cur<<1].l+1-p[cur<<1].sum;//节点的值非1即0,改变状态后就变成了L-R+1-sum。
p[cur<<1|1].sum=p[cur<<1|1].r-p[cur<<1|1].l+1-p[cur<<1|1].sum;
p[cur<<1].lazy=!p[cur<<1].lazy;p[cur<<1|1].lazy=!p[cur<<1|1].lazy;//左右子树的lazy标记就变为之前状态的反状态。
p[cur].lazy=0;
}
}
inline void pushup(int cur)
{
p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum;
}
inline void build(int l,int r,int cur)
{
p[cur].l=l;
p[cur].r=r;
p[cur].lazy=0;
if(l==r)
{
p[cur].sum=a[pre[l]];
return ;
}
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
pushup(cur);
}
inline void update(int l,int r,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r)
{
p[cur].lazy=!p[cur].lazy;//改变状态
p[cur].sum=(R-L+1)-p[cur].sum;
return ;
}
pushdown(cur);
int mid=L+R>>1;
if(r<=mid) update(l,r,cur<<1);
else if(l>mid) update(l,r,cur<<1|1);
else
{
update(l,mid,cur<<1);
update(mid+1,r,cur<<1|1);
}
pushup(cur);
}
inline int query(int l,int r,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r) return p[cur].sum;
pushdown(cur);
int mid=L+R>>1;
if(r<=mid) return query(l,r,cur<<1);
else if(l>mid) return query(l,r,cur<<1|1);
else return query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1);
pushup(cur);
}
int main()
{
int x;char op[10];
while(~scanf("%d",&n))
{
init();
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
add(x,i);
add(i,x);
}
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
dfs(1,0);
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%s%d",op,&x);
if(op[0]=='g') printf("%d\n",query(in[x],out[x],1));
else update(in[x],out[x],1);
}
}
return 0;
}
努力加油a啊,(o)/~