Summer Training day6 codeforces343D dfs序、线段树
来源:互联网 发布:java session的用法 编辑:程序博客网 时间:2024/06/08 04:58
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.
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.
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.
51 25 12 34 2121 12 33 13 23 33 41 22 43 13 33 43 5
00010101
套路题,由于进行的是子树上的操作,因此把整棵树用dfs搜索得到其dfs序(长度为2n,因为要记录每个点出栈和入栈的时间),然后构建一颗线段树。
我们记线段树上每个点如果为0的话代表它到根节点的链上是空的。
对于操作1,就相当于把v的子树全部设置为1,在这之前判断一下子树中有没有为0的点,如果有的话把v的父亲设置为0.
对于操作2,把v设置为0
对于操作3,判断v的子树里面有没有0即可。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 1e6;int head[MAXN];int cnt;struct edge{ int v; int next; int cost; }Es[MAXN<<1]; void init(){ cnt = 0; memset(head,-1,sizeof(head)); }inline void add_edge(int i,int j,int cost){ Es[cnt].v = j; Es[cnt].cost = cost; Es[cnt].next = head[i]; head[i] = cnt++; } const int INF = 1e8;int a[MAXN];int segtree[MAXN*4];int addmark[MAXN*4];void pushdown(int root,int L,int R){if(addmark[root]){int mid = (L + R) / 2;addmark[root*2] = addmark[root] ;addmark[root*2+1] = addmark[root] ;segtree[root*2] = addmark[root] ;segtree[root*2+1] = addmark[root] ;addmark[root] = 0;}}void pushup(int root){segtree[root] = (segtree[root * 2] == 2 && segtree[root * 2 + 1] == 2) ? 2 : 1;}void build(int root,int begin,int end){addmark[root] = 0;//设置延迟标记域 if(begin == end) { segtree[root] = a[begin]; } //递归得到 else { build(root*2,begin,(begin+end)/2); build(root*2+1,(begin+end)/2+1,end); pushup(root); }}bool query(int root,int begin,int end,int left,int right){ /*begin,end为当前结点所存储的区间 left,right为要查询的区间*/ if(left > end||right < begin) return 1; /*如果完全包含节点区间,那么直接返回*/ if(begin>=left&&end<=right) return segtree[root] == 2; pushdown(root,begin,end); /*不然有交叉的话*/bool a = query(root*2,begin,(begin+end)/2,left,right);bool b = query(root*2+1,(begin+end)/2+1,end,left,right);return a && b ;}void update(int root,int begin,int end,int ubegin,int uend,int val){ if(ubegin > end || uend < begin) return ;if(begin >= ubegin && end <= uend){addmark[root] = val;segtree[root] = val;return ;}pushdown(root,begin,end);int m = (begin+end)/2;update(root*2,begin,m,ubegin,uend,val);update(root*2+1,m+1,end,ubegin,uend,val); pushup(root);}int n,q;int idx = 0;int IN[MAXN],OUT[MAXN],pa[MAXN];void dfs(int x,int fa){pa[x] = fa;IN[x] = ++idx;for(int e = head[x];e != -1;e = Es[e].next){int v = Es[e].v;if(v != fa){dfs(v,x);}}OUT[x] = ++idx;}int main(){for(int i = 0;i < MAXN;i++) a[i] = 1;init();scanf("%d",&n);build(1,1,2*n);for(int i = 0;i < n-1;i++){int a,b;scanf("%d%d",&a,&b);add_edge(a,b,1);add_edge(b,a,1);}dfs(1,-1);scanf("%d",&q);while(q--){int op,v;scanf("%d%d",&op,&v);if(op == 1){bool flag = query(1,1,2*n,IN[v],OUT[v]);if(!flag) {update(1,1,2*n,IN[pa[v]],IN[pa[v]],1);update(1,1,2*n,OUT[pa[v]],OUT[pa[v]],1);}update(1,1,2*n,IN[v],OUT[v],2);}else if(op == 2){update(1,1,2*n,IN[v],IN[v],1);update(1,1,2*n,OUT[v],OUT[v],1);}else if(op == 3){printf("%d\n",query(1,1,2*n,IN[v],OUT[v]) ? 1: 0);}}return 0;}
- Summer Training day6 codeforces343D dfs序、线段树
- Summer Training day6 codeforces633G 线段树、bitset
- Summer Training day6 coseforces339D 线段树、位操作
- Summer Training day6 codeforces240F 线段树、回文串
- Summer Training day6 codeforces787D 最短路spfa,线段树建图
- Summer Training day6 codeforces 675D 二叉搜索树
- Summer training day6 codeforces19E 树链剖分,并查集
- Summer Training day6 codeforces 593D LCA+并查集
- Summer Training day6 codeforces292D并查集合并
- 【BNU Summer Training 2014.08.11】 Nested Segments (线段树,离散化)
- (线段树+dfs序)
- DFS序+线段树
- UESTC 2016 Summer Training #1 Div.2 J - Objects Panel (A) dfs、按条件遍历树、interesting
- HDU5692 线段树+dfs序
- 【线段树+dfs序】J
- 线段树2.0+dfs序
- Tri_integral Summer Training 6
- BUPT-SUMMER-TRAINING-搜索
- 不相交最大子数组和
- HDU1170四则运算
- 思维导图--信息系统开发与管理(1-9章)全
- 特征选择
- sublime快捷键
- Summer Training day6 codeforces343D dfs序、线段树
- jQuery .click()方法传参
- Python爬虫实战(2):自定义年月日国别抓取Bing壁纸
- codeforces739e绝对的干货题解!!!!!
- [微信小程序]页面跳转对象参数的传递方法
- DEP/ASLR 原理及攻击
- 一图了解网络分层模型即各层对应哪些协议
- 2017.6.6测试 题一 火柴
- reverse函数:反转容器内容