D. Water Tree
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/343/problem/DDescription
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:
- Fill vertex v with water. Then v and all its children are filled with water.
- Empty vertex v. Then v and all its ancestors are emptied.
- Determine whether vertex v is filled with water at the moment.
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"); } }}