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 ...
随机推荐
- Java 实现多线程的两种方式
1:继承Therad类2:实现Runnable 接口 1.继承Thread类实现多线程继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实 ...
- AngularJS 1.2.x 学习笔记(表单校验篇)
https://my.oschina.net/cokolin/blog/526911 摘要: 本文首发于 blog.csdn.net/vipshop_ebs/article/details/39472 ...
- vba 笔记
1.PlanWS5.Range("D5:E13").Copy 复制PlanWS5.Range("G5:H13").PasteSpecial Paste:=x ...
- python 获取一个列表有多少连续列表
python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:
- web初学之重定向与请求转发
重定向与请求转发的问题 (1)RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的,是属于请求对象的方法. (2 ...
- 优化 从Draw Calls到GC
原文出处: 慕容小匹夫的博客(@慕容小匹夫) 欢迎分享原创到伯乐头条 前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章 ...
- Web前端开发:为何选择MVVM而非MVC
在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的技术原因,MVC在Web前端开发中根本无法使用(对的,是无法,而不是不该) 在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的 ...
- freemarker
一.下载freemarker的jar包,到maven仓库下载 二.引入jar包,参考freemarker的手册写代码 1.Test.ftlh <!DOCTYPE html> <htm ...
- JSONObejct属性获取
package com.beijxing.TestMain; import java.io.File; import java.io.IOException; import org.apache.co ...
- Android Runtime
[Android Runtime] Every Android application runs in its own process, with its own instance of the Da ...