Description
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.
Sample 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
Sample Output
2
0
0
1
2
1
1
0
题目大意:
有一棵树,每个结点都有一个灯,给定每个灯的初始状态(开或关),有q次操作,操作有两种:
1、pow v,将v结点所代表的子树(包括v结点)的所有灯都改变状态(开变关,关变开)。
2、get v,输出v结点所代表的子树(包括v结点)中亮着的灯的数量。
核心思想:
dfs序+线段树区间更新lazy。
dfs序:实现原树的子树和线段树区间的相互映射。
线段树区间更新lazy:提高区间的更新效率。
代码如下:
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=2e5+20;
int a[N];//存灯的初始状态
int head[N],cs;//用于邻接表
int cd,in[N],out[N],num[N];//用于dfs序
char s[20];
//用于邻接表
struct node{
int y,ne;
}side[N];
//邻接表存边
void add(int x,int y)
{
side[cs].y=y;
side[cs].ne=head[x];
head[x]=cs++;
return;
}
//dfs序
void dfs(int x)
{
in[x]=++cd;
num[cd]=x;
for(int i=head[x];i!=-1;i=side[i].ne)
dfs(side[i].y);
out[x]=cd;
return;
}
//线段树
struct Node{
int l,r,lazy,sum;//lazy表示是否需要反转子区间状态
}tr[N<<2];
void pushup(int m)
{
tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum;
return;
}
void pushdown(int m)
{
int t=tr[m].lazy;
int l=tr[m].l;
int r=tr[m].r;
int mid=(l+r)>>1;
if(t)
{
tr[m<<1].lazy^=1;
tr[m<<1|1].lazy^=1;
tr[m<<1].sum=mid-l+1-tr[m<<1].sum;
tr[m<<1|1].sum=r-mid-tr[m<<1|1].sum;
tr[m].lazy=0;
}
return;
}
void build(int m,int l,int r)
{
tr[m].l=l;
tr[m].r=r;
tr[m].lazy=0;
if(l==r)
{
//线段树区间根据num数组映射原树的子树
tr[m].sum=a[num[tr[m].l]];
return;
}
int mid=(l+r)>>1;
build(m<<1,l,mid);
build(m<<1|1,mid+1,r);
pushup(m);
return;
}
void update(int m,int l,int r)
{
if(tr[m].l==l&&tr[m].r==r)
{
tr[m].lazy^=1;
tr[m].sum=r-l+1-tr[m].sum;
return;
}
pushdown(m);
int mid=(tr[m].l+tr[m].r)>>1;
if(r<=mid)
update(m<<1,l,r);
else if(l>mid)
update(m<<1|1,l,r);
else
{
update(m<<1,l,mid);
update(m<<1|1,mid+1,r);
}
pushup(m);
return;
}
int query(int m,int l,int r)
{
if(tr[m].l==l&&tr[m].r==r)
return tr[m].sum;
pushdown(m);
int mid=(tr[m].l+tr[m].r)>>1;
if(r<=mid)
return query(m<<1,l,r);
if(l>mid)
return query(m<<1|1,l,r);
return query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
}
int main()
{
int n,x,y;
//初始化
cs=0;
for(int i=0;i<N;i++)
head[i]=-1;
//输入
scanf("%d",&n);
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
add(x,i);
}
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
//dfs序
cd=0;
dfs(1);
//建树
build(1,1,n);
//m次操作
int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%s%d",s,&x);
if(s[0]=='g')
printf("%d\n",query(1,in[x],out[x]));
else
update(1,in[x],out[x]);
}
return 0;
}