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

CodeForces-877E题解
本文介绍了一道CodeForces平台上的题目——K-DanilandaPart-timeJob(E题)的解决方案。该题涉及在一棵带权树上进行节点状态翻转与查询操作,利用DFS序和线段树进行高效处理。

            

K - Danil and a Part-time Job

 CodeForces - 877E

 

 

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.

 

 

         

     题意:给定一棵树以及每个节点表示灯的开关状态

     共有两种操作:

     pow x 表示将以x为根的子树上的所有灯的状态翻转 

     get  x 表示查询以x为根的子树上所有开着的灯的个数并输出

 

     思路:对于树的任何一个子树来说,它上面各个节点dfs序将是一段连续的整数。

     那么,我们就可以根据dfs序将这棵树上各个节点的信息用线段树来维护。

    (实际上,dfs序思想并不少见,比如树链剖分就是使用了类似的思想)

     

     在维护线段树时,留意一下懒标记的下移。

      

     代码仅供参考:

     

#include <bits/stdc++.h>
#define maxn 200010 
using namespace std; 
int siz[maxn],n,tid[maxn],rank[maxn],val[maxn]; 
vector<int>tree[maxn]; 
int sets[maxn*4],tlit[maxn*4],tsiz[maxn*4]; 

void prepare()//预处理 
{ 
 memset(tlit,0,sizeof(tlit));
 memset(tsiz,0,sizeof(tsiz)); 
 memset(sets,0,sizeof(sets)); 
 memset(val,0,sizeof(val)); 
 memset(siz,0,sizeof(siz)); 
 memset(tid,0,sizeof(tid)); 
 memset(rank,0,sizeof(rank)); 
} 

void buildtree(int now,int l,int r)//建立线段树 其中tlit表示亮着的节点数 tsiz表示总数 
{ 
 if(l==r) 
 { 
  tlit[now]=val[rank[l]]; 
  tsiz[now]++; 
  return; 
 } 
 
 int mid=(l+r)/2; 
 buildtree(now*2,l,mid); 
 buildtree(now*2+1,mid+1,r); 
 tlit[now]=tlit[now*2]+tlit[now*2+1]; 
 tsiz[now]=tsiz[now*2]+tsiz[now*2+1]; 
}
 
void dfs1(int u)//计算子树的节点数保存在siz中 
{ 
 int i; 
 siz[u]=1; 
 for(i=0;i<tree[u].size();i++) 
 { 
  int v=tree[u][i]; 
  dfs1(v); 
  siz[u]+=siz[v];
 } 
} 

int tim=0; 
void dfs2(int u) //计算DFS序保存在tid中 
{ 
 tid[u]=++tim; 
 rank[tid[u]]=u; 
 for(int i=0;i<tree[u].size();i++) 
 { 
  int v=tree[u][i]; 
  dfs2(v); 
 } 
} 

void init()//初始化 
{ 
 int i; 
 scanf("%d",&n); 
 for(i=2;i<=n;i++) 
 { 
  int v; 
  scanf("%d",&v); 
  tree[v].push_back(i); 
 } 
 for(i=1;i<=n;i++)
 scanf("%d",&val[i]); 
 dfs1(1); 
 dfs2(1); 
 buildtree(1,1,n);
} 

void pushdown(int o) //懒标记下移 
{ 
 int l,r; 
 l=o*2;r=o*2+1; 
 if(sets[o]) 
 { 
  sets[l]+=sets[o]; 
  sets[l]=sets[l]%2; 
  tlit[l]=tsiz[l]-tlit[l]; 
  sets[r]+=sets[o]; 
  sets[r]=sets[r]%2; 
  tlit[r]=tsiz[r]-tlit[r]; 
  sets[o]=0; 
  tlit[o]=tlit[l]+tlit[r];
 } 
}

void update(int now,int l,int r,int ql,int qr)//线段树区间修改 
{ 
 if(ql<=l&&qr>=r)
 { 
  sets[now]++; 
  sets[now]=sets[now]%2; 
  tlit[now]=tsiz[now]-tlit[now]; 
  return; 
 } 
 pushdown(now); 
 int mid=(l+r)/2; 
 if(ql<=mid)update(now*2,l,mid,ql,qr); 
 if(qr>mid)update(now*2+1,mid+1,r,ql,qr); 
 tlit[now]=tlit[now*2]+tlit[now*2+1]; 
} 
 
int query(int now,int l,int r,int ql,int qr)//线段树区间查询 
{ 
 if(ql<=l&&qr>=r) 
 { 
  return tlit[now];
 }
 pushdown(now); 
 int mid=(l+r)/2,a1=0,a2=0; 
 if(ql<=mid)a1=query(now*2,l,mid,ql,qr); 
 if(qr>mid)a2=query(now*2+1,mid+1,r,ql,qr); 
 return a1+a2;
}

void maintain(int u)
{ 
 int l,r; 
 l=tid[u];r=l+siz[u]-1;
 update(1,1,n,l,r); 
}

void getans(int u)
{ 
 int l,r; 
 l=tid[u];r=l+siz[u]-1; 
 cout<<query(1,1,n,l,r)<<endl;
}

int main() 
{ 
 int i,q; 
 string st; 
 prepare(); 
 init(); 
 scanf("%d",&q); 
 for(i=1;i<=q;i++) 
 { 
  char st[10]; 
  int a; 
  scanf("%s",st); 
  scanf("%d",&a); 
  if(st[0]=='g')getans(a); 
  if(st[0]=='p')maintain(a);
 }
 return 0;
} 

 

 

    

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值