Codeforces 877E Danil and a Part-time Job (DFS序+线段树)

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:

  1. pow v describes a task to switch lights in the subtree of vertex v.
  2. 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

Note

The tree before the task pow 1.

The tree after the task pow 1.

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
/*

题目大意:书上维护两种操作,一种是查询子树中亮的点,
一种是把子树中包括根的全部点 亮暗反转。

很明显是DFS序建立线段树,
这道题我原先很抓狂的地方是,
我想利用dfs序形成的区间块建立线段树,
如何表示这个点是亮的呢?区间填充满?
那查询和怎么办?单点更新区间左端点?那反转操作怎么办?

通过参考,可以利用映射的方法(也是学到了)
把区间左端点映射到对应的节点序号上,
然后建树时直接利用影射把数据数组映射成树的叶节点集合。

这样思路就清晰多了,反转就是用区间总和相减,
因为每个点的反转都会引起树上层节点的变化,
通过把DFS序区间映射成左端点集合的,我第一次见,蛮好的题目。
*/

const int  maxn =5e5+5;
///链式前向星
struct node{int nxt,e;};
node edge[maxn];
int head[maxn],tot=0,ti;
void init(){memset(head,-1,sizeof(head));tot=ti=0;}
void add(int x,int y){edge[tot]=node{head[x],y};head[x]=tot++;}
///DFS序建立
int pl[maxn],pr[maxn],fa[maxn];///映射数组fa
void  dfs(int u,int pre)
{
    pl[u]=++ti;
    fa[ti]=u;///记录映射关系,利用映射关系来建立线段树
    for(int i=head[u];~i;i=edge[i].nxt)
    {
        int v=edge[i].e;
        if(v==pre) continue;
        dfs(v,u);
    }
    pr[u]=ti;
}
///ST树
int tree[maxn<<2],lazy[maxn<<2],a[maxn];
void pushup(lrt){tree[rt]=tree[rt<<1]+tree[rt<<1|1];}
void pushdown(lrt)
{
    int mid=l+r>>1;
    if(lazy[rt])
    {
        tree[rt<<1]=(mid-l+1)-tree[rt<<1];///反转
        tree[rt<<1|1]=(r-mid)-tree[rt<<1|1];///反转
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        lazy[rt]^=1;
    }
}
void build(lrt)
{
    lazy[rt]=0;int mid=l+r>>1;
    if(l==r) { tree[rt]=a[fa[l]];  return; }
    build(lson),build(rson),pushup(root);
}
void update(lrt,int L,int R)
{
    if(L<=l&&r<=R)
    {
        tree[rt]=(r-l+1)-tree[rt];
        lazy[rt]^=1;
        return ;
    }
    pushdown(root);
    int mid=l+r>>1;
    if(L<=mid) update(lson,L,R);
    if(mid<R) update(rson,L,R);
    pushup(root);
}

int query(lrt,int L,int R)
{
    if(L<=l&&r<=R) return tree[rt];
    pushdown(root);
    int mid=l+r>>1,ans=0;
    if(L<=mid)  ans+=query(lson,L,R);
    if(mid<R) ans+=query(rson,L,R);
    pushup(root);
    return ans;
}

int n,x,q;
char seq[100];

int main()
{
    init(); scanf("%d",&n);
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&x);
        add(i,x),add(x,i);
    }

    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    dfs(1,-1);///根据数据建立线段树

    n=ti;
    build(1,n,1);

    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%s%d",seq,&x);
        if(seq[0]=='g') printf("%d\n",query(1,n,1,pl[x],pr[x]));
        else  update(1,n,1,pl[x],pr[x]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值