思路:

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序加线段树)的更多相关文章

  1. 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/ ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  5. 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 ...

  6. Codeforces Round #200 (Div. 1)D. Water Tree

    简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

随机推荐

  1. 01HTTP服务&AJAX编程

    HTTP服务&AJAX编程 一.服务器         1. 什么是服务器? 能够提供某种服务的机器(计算机)称为服务器. 2.服务器的分类:              1.按系统分类:Lin ...

  2. C#实现控制Windows系统关机、重启和注销的方法:

    shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...

  3. [转]Redmine 配置163邮箱

    redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...

  4. xpath爬取网页评论,网址的的调用方法,数据库特殊字符的替换

    # -*- coding:utf-8-*-from lxml import etreeimport urllibimport jsonimport requestsimport MySQLdbid=0 ...

  5. Struts2 有关于无法正常的使用通配符

    今天使用struts 2.3.4版本,做了一个通配符的小测试,结果其他的Action都能正常的使用,但是使用通配符的Action不能正常的使用.网上找了很久,最后发现,貌似strust2.3版本以上的 ...

  6. hdu5452 Minimum Cut

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5452 题意:给你一个图和它的生成树,要你在树上删一条边,问你最少删多少条边使得图不联通(开始时图一定联 ...

  7. php调用阿里大鱼 接口curl

    function http_request($url, $data = null, $header = null, $method = 'GET') { //如果是Get传参,拼接字符串 if ($m ...

  8. Memento(备忘录)-对象行为型模式

    1.意图 在布破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将对象恢复到原先保存的状态. 2.别名 Token 3.动机 为了允许用户取消不确定的操作或从错误中 ...

  9. SASS学习笔记(1)

    序 之前稍微看过SASS的文档,但是由于工作中没有涉及,渐渐的搁置了.最近公司新招来一个热情似火的前端,不管什么技术,不管自己能不能hold住,都提出来用一用再说.这样对我也好,跟着这个哥们混妥妥的长 ...

  10. oracle定时器

    /* 每10秒钟执行一次 插入一条时间 */ -- 创建table create table tab_time( current_time timestamp ); -- 创建存储过程 create ...