思路:

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. Java画图程序设计

    本文讲述一个画图板应用程序的设计,屏幕抓图如下: 『IShape』 这是所有图形类(此后称作模型类)都应该实现接口,外部的控制类,比如画图板类就通过这个接口跟模型类“交流”.名字开头的I表示它是一个接 ...

  2. Native wifi API使用

    写于博客园,自己迁过来: 一.WlanOpenHandle打开一个客户端句柄 DWORD WINAPI WlanOpenHandle( __in DWORD dwClientVersion, __re ...

  3. UGUI代码分析

    1.canvas,screen模式和world模式区别:screen的绑定摄像机与canvas相对位置不会改变不可改变canvas,world模式下有个event camera相对位置可改变,canv ...

  4. go 静态web服务器

    package main import ( "net/http" ) type helloHandler struct{} func (h *helloHandler) Serve ...

  5. 什么是SCADA Viewer

    SCADA Viewer 什么是SCADA Viewer SCADA Viewer是一个基于Web的软件框架(基于Web的HMI/SCADA/M2M工业和楼宇自动化,支持Modbus,BACnet,O ...

  6. mongo管理工具

    启动 D:\Program Files\MongoDB\Server\3.4\bin\mongod.exe --dbpath d:\data\db 还原 D:\Program Files\MongoD ...

  7. Camtasia Studio屏幕录像安装与破解

    Camtasia Studio汉化版是一款功能强大的屏幕录像工具,能在任何颜色模式下轻松地记录屏幕动作,包括影像.音效.鼠标移动轨迹.解说声音等.Camtasia Studio具有强大的视频播放和视频 ...

  8. scala 学习之:list span 用法

    Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements the ...

  9. C# 解析json

    在接口开发的过程中经常通过接口获取数据返回是json格式字符串. 但是返回的字符串可能比较复杂,可能不止一种类型的数据. 例如: { "resultCode": "0&q ...

  10. Android Studio开发基础之自定义View组件

    一般情况下,不直接使用View和ViewGroup类,而是使用使用其子类.例如要显示一张图片可以用View类的子类ImageView,开发自定义View组件可分为两个主要步骤: 一.创建一个继承自an ...