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 ...
随机推荐
- ASP.NET Misconfiguration: Debug Information
Abstract: Debugging messages help attackers learn about the system and plan a form of attack. Explan ...
- java spring hibernate
spring 如果配置让MultiActionController里的方法返回ModelAndView跳转到页面上 ****************************************** ...
- 基于log4net的支持动态文件名、按日期和大小自动分割文件的日志组件
最近处理一个日志功能,用log4net的配置不能完全满足要求,所以在其基础上简单封装了一下,支持以下功能: 1 零配置 内置默认配置,引用dll后不需要添加或修改任何配置文件也可以使用 2 动态指定文 ...
- Logstash 父子关系 配置
最近在使用Lostash的过程中遇到了一个问题:在一个log文件里包含两类数据,而且两类数据之间存在父子关系,那如何使用lostash的configuration实现这个需求呢 思路: 首先定义父事件 ...
- 关于tp.5.0角色管理导致的创建角色登陆报错问题解决!
今天用tp 5.0的时候,遇到一个问题,就是在利用超级管理员创建管理员角色时,角色账号密码登陆报错的问题 解决方法如下 htaccess文件修改如下 <IfModule mod_rewrite. ...
- [原创.数据可视化系列之六]使用openlyaers进行公网地图剪切
进行地图开发的过程中,我一般使用天地图或者微软的地图作为地图,因为这两种地图的经纬度偏差最小,基本可以满足用户需求,比如: 不用说,都是全部地图,这也是最常用的一种方法. 但是用户说,我只看大连的地图 ...
- 初学c# -- 学习笔记(四)
想想,数据库先用mysql了,这玩意小且安装方便.前面web学了些,现在学winform的.数据库先看看便捷的mysql. 下载了一个mysql5.7版的,装上居然找不到密码,重装3.4回,找不到,说 ...
- 永久解决火狐浏览器出现的flash版本更新问题
最近使用的火狐浏览器又出问题了,老是出现flash版本太旧,以至于在火狐的版本出现“该版本存在安全隐患,应当更新”的字样.对于火狐浏览器,我很早就开始使用了,flash版本更新的问题,我也有我的一些小 ...
- Android Studio开发基础之自定义View组件
一般情况下,不直接使用View和ViewGroup类,而是使用使用其子类.例如要显示一张图片可以用View类的子类ImageView,开发自定义View组件可分为两个主要步骤: 一.创建一个继承自an ...
- C#实现JSON序列化与反序列化
1.使用 JavaScriptSerializer类实现序列化 namespace: System.Web.Script.Serialization eg: // 序列化 private string ...