Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路:
dfs序其实是很水的东西。 和树链剖分一样, 都是对树链的hash。
该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值。
该题需要注意的是:当我们对一棵子树全都赋值为1的时候, 我们要查询一下赋值前子树最小值是不是0, 如果是的话, 要让该子树父节点变成0, 否则变0的信息会丢失。
细节参见代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 500000+10;
int T,n,m,tot=0,in[maxn],out[maxn],minv[maxn<<2],setv[maxn<<2],pre[maxn];
vector<int> g[maxn];
void dfs(int u, int fa) {
in[u] = ++tot;
pre[u] = fa;
int len = g[u].size();
for(int i = 0; i < len; i++) {
int v = g[u][i];
if(v == fa) continue;
dfs(v, u);
}
out[u] = tot;
}
void pushup(int o) {
minv[o] = min(minv[o<<1], minv[o<<1|1]);
}
void pushdown(int l, int r, int o) {
if(setv[o] != -1) {
setv[o<<1] = setv[o<<1|1] = setv[o];
minv[o<<1] = minv[o<<1|1] = setv[o];
setv[o] = -1;
}
}
void build(int l, int r, int o) {
minv[o] = 0;
setv[o] = -1;
if(l == r) return ;
int mid = (l + r) >> 1;
build(l, mid, o<<1);
build(mid+1, r, o<<1|1);
}
void update(int L, int R, int v, int l, int r, int o) {
if(L <= l && r <= R) {
minv[o] = v;
setv[o] = v;
return ;
}
int mid = (l + r) >> 1;
pushdown(l, r, o);
if(L <= mid) update(L, R, v, l, mid, o<<1);
if(mid < R) update(L, R, v, mid+1, r, o<<1|1);
pushup(o);
}
int query(int L, int R, int l, int r, int o) {
if(L <= l && r <= R) {
return minv[o];
}
int mid = (l + r) >> 1;
pushdown(l, r, o);
int ans = INF;
if(L <= mid) ans = min(ans, query(L, R, l, mid, o<<1));
if(mid < R) ans = min(ans, query(L, R, mid+1, r, o<<1|1));
pushup(o);
return ans;
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
build(1, n, 1);
int q; scanf("%d", &q);
while(q--) {
int id, v; scanf("%d%d", &id, &v);
if(id == 1) {
int tmp = query(in[v], out[v], 1, n, 1);
update(in[v], out[v], 1, 1, n, 1);
if(!tmp && pre[v]) update(in[pre[v]], in[pre[v]], 0, 1, n, 1);
}
else if(id == 2) {
update(in[v], in[v], 0, 1, n, 1);
}
else {
printf("%d\n", query(in[v], out[v], 1, n, 1));
}
}
return 0;
}
Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)的更多相关文章
- Codeforces Round #200 (Div. 1)D. Water Tree dfs序
D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...
- 343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构
D. Water Tree Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树
D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #200 (Div. 1)D. Water Tree
简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树
题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
随机推荐
- LESS初探
1. 安装less $ npm install -g less 2. less文件编译成css文件 $ lessc styles.less styles.css <!DOCTYPE html&g ...
- Google V8编程详解(五)JS调用C++
http://blog.csdn.net/feiyinzilgd/article/details/8453230 最近由于忙着解决个人单身的问题,时隔这么久才更新第五章. 上一章主要讲了Google ...
- delphi中midas是什么
Delphi中MIDAS到底是什么呢?和他相关组件是什么呢? MIDAS(Multitiered Distributed Application Services)多层分布式应用服务. Del ...
- leetCode 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- oracle sql查询转义下划线
1,看以下结果 select * from test where login like '%CF_%'; LOGIN------------------------------------------ ...
- Spring Web应用的最大瑕疵
众所周知, 现在的Spring框架已经成为构建企业级Java应用事实上的标准了,众多的企业项目都构建在Spring项目及其子项目之上,特别是Java Web项目,很多都使用了Spring并且遵循着We ...
- python 继承中的super
python继承中子类访问父类的方法(包括__init__)主要有两种方法,一种是调用父类的未绑定方法,另一种是使用super(仅仅对于新式类),看下面的两个例子: #coding:utf-8 cla ...
- Android根据文件路径使用File类获取文件相关信息
Android通过文件路径如何得到文件相关信息,如 文件名称,文件大小,创建时间,文件的相对路径,文件的绝对路径等: 如图: 代码: public class MainActivity extends ...
- C++STL学习笔记_(1)vector知识
#include<iostream> using namespace std; #include "vector" //数组元素的 添加和删除 void main31( ...
- 你不知道的函数节流,提高你的JS性能!
浏览器的DOM计算处理非常耗费CPU时间,霸占内存,这对我们的开发来说是非常不友好的,,比如IE浏览器的onresize事件就可能在用户稍微拖动一下窗口时计算上千次,甚至更高频率直接让浏览器崩溃... ...