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

本文共 3436 字,大约阅读时间需要 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"); } }}

 

转载于:https://www.cnblogs.com/qscqesze/p/4872060.html

你可能感兴趣的文章
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>
数据挖掘算法比赛 - 简单经验总结
查看>>
生成商户订单号/退款单号
查看>>
使用Android OpenGL ES 2.0绘图之六:响应触摸事件
查看>>
我们过去几年做对了哪些事
查看>>
ubuntu 16.04LTS
查看>>
javascript深入理解js闭包
查看>>
Oracle的安装
查看>>
Android Socket连接PC出错问题及解决
查看>>
Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
查看>>
第2天线性表链式存储
查看>>
python自动化测试-D11-学习笔记之一(yaml文件,ddt)
查看>>
mysql存储过程使用游标循环插入数据
查看>>
Ubuntu 12.04 添加新用户并启用root登录
查看>>
20145309信息安全系统设计基础第9周学习总结上
查看>>
c# 字段、属性get set
查看>>