CodeForces - 343D Water Tree

这篇博客讨论了如何解决CodeForces上的343D问题——水树。问题涉及一棵有根树,初始时所有节点为空,提供三种操作:填充节点、清空节点和询问节点状态。博主分享了一种利用深度优先搜索(DFS)序号建立区间,并通过维护区间和及懒惰标记来优化清空操作的方法,从而解决查询效率问题。
摘要由CSDN通过智能技术生成

Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.

The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.

Mike wants to do the following operations with the tree:

1.Fill vertex v with water. Then v and all its children are filled with water.
2.Empty vertex v. Then v and all its ancestors are emptied.
3.Determine whether vertex v is filled with water at the moment.
Initially all vertices of the tree are empty.
Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.

Input
The first line of the input contains an integer n (1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the edges of the tree.

The next line contains a number q (1 ≤ q ≤ 500000) — the number of operations to perform. Each of the following q lines contains two space-separated numbers ci (1 ≤ ci ≤ 3), vi (1 ≤ vi ≤ n), where ci is the operation type (according to the numbering given in the statement), and vi is the vertex on which the operation is performed.

It is guaranteed that the given graph is a tree.

Output
For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.

Example
Input
5
1 2
5 1
2 3
4 2
12
1 1
2 3
3 1
3 2
3 3
3 4
1 2
2 4
3 1
3 3
3 4
3 5
Output
0
0
0
1
0
1
0
1
题意:
给你一棵有根树,1为根节点,初始的时候所有节点的值为0。然后有3种操作:
1 v 表示把v结点以及所有子树结点的值变为1。
2 v 表示把v结点以及所有祖先的结点值变为0。
3 v 询问v结点的值是多少。
思路:
建立一棵深搜树,按照深搜的序号建立区间。一开始清空的时候是暴力清空的,TLE了。TLE代码:

#include <bits/stdc++.h>

using namespace std;
#define lson i<<1
#define rson i<<1|1
const int MAXN = 5e5+7;
int n,m;
//*******线段树内容******
struct node
{
    int l,r;
    int flag,val;
} tree[MAXN<<2];

void push_down(int i)
{
    if(tree[i].
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值