CodeForces - 877E--Danil and a Part-time Job(DFS序列+线段树之lazy)

本文详细解析了Codeforces上的一道经典题目,涉及DFS序和线段树的应用,通过实例讲解了如何处理区间修改和翻转问题,展示了具体的代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/877/E

Danil and a Part-time Job

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.

题意很明确了吧,就是一个dfs序+线段树的题目,关于dfs序有不懂的可以看这篇博客https://blog.csdn.net/qq_44786250/article/details/100046030

和博客里面的例题  苹果树  不同的地方就是苹果树进行的是单点修改,而这道题则是区间修改,当然也是翻转的问题,这个区间修改就比较有意思了,我们需要一个lazy标记,用来对区间修改进行标记

其实这里有一个规律就是   当你对某个区间进行了奇数次修改,那么这个区间内的状态就会发生反转;当你对这个区间进行了偶数次修改,那么这个区间的状态并不会发生改变;

我们可以对修改次数进行累加,然后判断他的奇偶性;也可以利用二进制的性质,进行取非运算;我采用的是后一种做法

void pd(int k){//
	if(pp[k].lazy==0)
		return ;
	int mid=(pp[k].l+pp[k].r)>>1;
	pp[k<<1].sum=mid-pp[k].l+1-pp[k<<1].sum;
	pp[k<<1|1].sum=pp[k].r-mid-pp[k<<1|1].sum;
	pp[k<<1].lazy^=1;
	pp[k<<1|1].lazy^=1;
	pp[k].lazy=0;
}

关于这个lazy下传 这里需要多说几句

如果是0则不下传,是1则下传,这个好想;还有就是sum的改变,区间长度减掉原sum,这个也好想但是放一块就不好想了。。。

另一点需要注意的就是建树之后,节点的初状态的输入

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=2e5+10;
//存图+dfs序  
struct bian{
	int to;
	int next;
}a[mm<<1];
int head[mm<<1];
int cnt=0;
void add(int x,int y){
	a[cnt].to=y;
	a[cnt].next=head[x];
	head[x]=cnt++;
}
int in[mm],out[mm];
int tim=0;
void dfs(int x,int pre){
	in[x]=++tim;
	for(int i=head[x];~i;i=a[i].next){
		int tt=a[i].to;
		if(tt==pre)continue;
		dfs(tt,x);
	}
	out[x]=tim;
}

int book[mm];
//线段树 
struct node{
	int l,r;
	int sum;
	int lazy;//标记是否需要下传  0不下传,1下传
}pp[mm<<2];

void up(int k){
	pp[k].sum=pp[k<<1].sum+pp[k<<1|1].sum;
}

void build(int k,int l,int r){
	pp[k].l=l;
	pp[k].r=r;
	pp[k].lazy=pp[k].sum=0;
	if(l==r){	
		return ;
	}
	int mid=(l+r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	up(k);
}

void pd(int k){//
	if(pp[k].lazy==0)
		return ;
	int mid=(pp[k].l+pp[k].r)>>1;
	pp[k<<1].sum=mid-pp[k].l+1-pp[k<<1].sum;
	pp[k<<1|1].sum=pp[k].r-mid-pp[k<<1|1].sum;
	pp[k<<1].lazy^=1;
	pp[k<<1|1].lazy^=1;
	pp[k].lazy=0;
}

void change(int k,int l,int r){
	if(pp[k].l>=l&&pp[k].r<=r){
		pp[k].sum=pp[k].r-pp[k].l+1-pp[k].sum;//
		pp[k].lazy^=1;
		return ;
	}
	pd(k);
	int mid=(pp[k].l+pp[k].r)>>1;
	if(l<=mid)change(k<<1,l,r);
	if(r>mid)change(k<<1|1,l,r);
	up(k);
}

int query(int k,int l,int r){
	if(pp[k].l>=l&&pp[k].r<=r)
		return pp[k].sum;
	pd(k);
	int mid=(pp[k].l+pp[k].r)>>1;
	int res=0;
	if(l<=mid)res+=query(k<<1,l,r);
	if(r>mid)res+=query(k<<1|1,l,r);
	return res;
}

int n,m;
int x;
char op[5];
int main()
{
	mem(head,-1);//不要忘了 
	scanf("%d",&n);	
	for(int i=2;i<=n;i++){
		scanf("%d",&x);
		add(x,i);
		add(i,x);
	}
	dfs(1,-1);
	
	build(1,1,n);
	for(int i=1;i<=n;i++){
		scanf("%d",&book[i]);
		if(book[i]) 
			change(1,in[i],in[i]);
	} 
		
	scanf("%d",&m);
	while(m--){
		scanf("%s%d",op,&x);
		if(op[0]=='g'){
			printf("%d\n",query(1,in[x],out[x]));
		}
		else {
			change(1,in[x],out[x]);
		}	
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值