博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #200 (Div. 1)D. Water Tree dfs序
阅读量:5902 次
发布时间:2019-06-19

本文共 3339 字,大约阅读时间需要 11 分钟。

D. Water Tree

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/343/problem/D

Description

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.

Bi​​,Ci​​,即此题的初始分值、每分钟减少的分值、dxy做这道题需要花费的时间。

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.

Sample 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

Sample Output

0 0 0 1 0 1 0 1

HINT

 

题意

给你一棵树,初始点权都是0,然后三个操作

1.将一个点以及他的儿子全部变成1

2.将一个点以及他的所有祖先全部变成0

3.查询一个点的权值

题解:

dfs序,然后两棵线段树维护就好了

注意先后顺序就行,如果你变成1的操作先于变成0的操作,那么肯定就1优先咯

反之亦然

代码:

#include
#include
#include
#include
using namespace std;#define maxn 1500050#define LL(x) (x<<1)#define RR(x) (x<<1|1)#define MID(a,b) (a+((b-a)>>1))vector
E[maxn];int id;int in[maxn];int out[maxn];void dfs(int x,int pre){ in[x]=++id; for(int i=0;i
>1; creat(l, mid, rt<<1); creat(mid+1, r, rt<<1|1); pushup(rt); } void modify(int l, int r, int x, int L, int R, int rt) { if(l <= L && r >= R) { lazy[rt] = x; sum[rt] = x;///!!! return; } pushdown(rt, R-L+1);///!!! int mid = (L+R)>>1; if(l <= mid) modify(l, r, x, L, mid, rt<<1); if(r > mid) modify(l, r, x, mid+1, R, rt<<1|1); pushup(rt); } int query(int l, int r, int x, int L, int R, int rt) { if(l <= L && r >= R) { return sum[rt]; } pushdown(rt, R-L+1);///!!! int mid = (L+R)>>1; int sum1 = 0,sum2 = 0; if(l <= mid) sum1 = query(l, r, x, L, mid, rt<<1); if(r > mid) sum2 = query(l, r, x, mid+1, R, rt<<1|1); pushup(rt); return max(sum1,sum2); }}seg1,seg2;int main(){ int n; scanf("%d",&n); for(int i=1;i
tmp2)printf("1\n"); else printf("0\n"); } }}

 

转载地址:http://kdkpx.baihongyu.com/

你可能感兴趣的文章
使用NTFS权限保护数据安全
查看>>
infortrend ESDS RAID6故障后的数据恢复方案
查看>>
【STM32 .Net MF开发板学习-23】DHT11温湿度传感器通信(下)
查看>>
extmail集群的邮件负载均衡方案 [lvs dns postfix]
查看>>
SCCM2012SP1---资产管理和远程管理
查看>>
Android Activity 之 startActivityForResult 的使用
查看>>
org.springframework.util 类 Assert的使用
查看>>
java提供类与cglib包实现动态代理
查看>>
flask上传多个文件,获取input中的数组
查看>>
更改UIView的背景
查看>>
JLNotebookView
查看>>
StackPanel
查看>>
SPUserResizableView
查看>>
UML类图示例
查看>>
sh ./ 执行区别
查看>>
宏定义(#ifndef+#define+#endif)的作用
查看>>
Prometheus安装部署以及配置
查看>>
Oracle存储过程大冒险-2存储过程常用语法
查看>>
taobao-pamirs-schedule-2.0源码分析——类设计
查看>>
10位程序员眼中的2007:寻找软件开…
查看>>