CodeForces877E Danil and a Part-time Job 线段树+DFS序

本文介绍了一种在树状结构上进行区间修改和单点查询的算法,通过使用深度优先搜索序(DFS序)将树转换为线性结构,并应用线段树进行优化,实现高效操作。文章详细解释了从树到线性结构的转换过程,以及如何利用线段树解决特定问题,避免了高时间复杂度的暴力操作。

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

题意:给你一棵树,每个结点都有一个灯,有两种操作:一是拨动某个结点对应子树的所有开关(若原来是开的就关上,若原来是关的就打开)。二是询问某个结点的状态(开还是关)。
思路:首先是操作某一结点与其对应子树,所以妥妥的dfs序。
关于dfs序(序嘛,肯定是序列的意思,那么肯定是一种线性结构),个人理解是把所有结点揪出来,然后把树形结构变成一个线性结构。然后对子树的操作就变成了对dfs序中某一区间的操作,然后因为暴力修改区间的操作时间复杂度太高(n2),所以我们需要对dfs序这个线性序列来建树,这样复杂度就成了(nlogn)。
然后这题就转化成了区间修改和单点查询了。简单的线段树模板。
但刚开始自己一直WA在test 14.找到了原因是:下传lazy标记的时候直接把当前结点的lazy赋值给左右子树的lazy了,实际上是不对的,应该是把子树lazy标记取反才对。
下面是AC代码。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 0x3f3f3f3f
#define cl(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=2e5+100;
int n,cnt;
int con[maxn],sta[maxn],End[maxn];//t是每个结点灯的状态
int now[maxn];//新序列
struct Side {
	int v,next;
} side[maxn<<1];
int head[maxn];
struct node {
	int l,r,sum,lazy;
} t[maxn<<2];
void init() {
	cnt=0;
	for(int i=1; i<=n; i++) {
		head[i]=-1;
		sta[i]=End[i]=now[i]=0;
	}
	cl(t,0);
}
void add(int u,int v) {
	cnt++;
	side[cnt].v=v;//这改错了。。。
	side[cnt].next=head[u];
	head[u]=cnt;
}
void dfs(int u,int pre) {
	cnt++;
	sta[u]=cnt;
	now[cnt]=u;//这里一定注意多个这个!!!
	for(int i=head[u]; i!=-1; i=side[i].next) {
		int v=side[i].v;
		if(v==pre) continue;
		dfs(v,u);
	}
	End[u]=cnt;
}

void pushup(int k) {
	t[k].sum=t[k<<1].sum+t[k<<1|1].sum;
}
void build(int k,int l,int r) {
	t[k].l=l,t[k].r=r,t[k].lazy=0;
	if(l==r) {
		t[k].sum=con[now[l]];
	} else {
		int mid=(l+r)>>1;
		build(k<<1,l,mid);
		build(k<<1|1,mid+1,r);
		pushup(k);
	}
}
void pushdown(int k) {
	if(t[k].lazy) {
		t[k<<1].sum=t[k<<1].r-t[k<<1].l+1-t[k<<1].sum;
		t[k<<1|1].sum=t[k<<1|1].r-t[k<<1|1].l+1-t[k<<1|1].sum;
		t[k<<1].lazy=(!t[k<<1].lazy); 
		t[k<<1|1].lazy=(!t[k<<1|1].lazy);
		t[k].lazy=0;
	}
}
void updata(int k,int l,int r) {
	if(l<=t[k].l&&t[k].r<=r) {
		t[k].sum=t[k].r-t[k].l+1-t[k].sum;
		if(t[k].l!=t[k].r)
			t[k].lazy=!t[k].lazy;
	} else {
		pushdown(k);
		int mid=(t[k].l+t[k].r)>>1;
		if(l<=mid) updata(k<<1,l,r);
		if(mid<r) updata(k<<1|1,l,r);
		pushup(k);
	}
}
int query(int k,int l,int r) {
	if(l<=t[k].l&&t[k].r<=r) {
		return t[k].sum;
	} else {
		pushdown(k);
		int mid=(t[k].l+t[k].r)>>1;
		int ans=0;
		if(l<=mid) ans+=query(k<<1,l,r);
		if(mid<r) ans+=query(k<<1|1,l,r);
		return ans;
	}
}
int main() {
	scanf("%d",&n);
	init();
	for(int i=2; i<=n; i++) {
		int p;
		scanf("%d",&p);
		add(p,i);
		add(i,p);
	}
	for(int i=1; i<=n; i++) {
		scanf("%d",&con[i]);
	}
	cnt=0;//存dfs序
	dfs(1,-1);
	build(1,1,n);
	int q;
	cin>>q;
	while(q--) {
		char s[10];
		int k;
		scanf("%s%d",s,&k);
		if(s[0]=='g') {
			printf("%d\n",query(1,sta[k],End[k]));
		} else {
			updata(1,sta[k],End[k]);
		}
	}
	return 0;
}

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
Note
The tree before the task pow 1.
The tree after the task pow 1.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值