【题解】Luogu CF343D Water Tree
原题传送门:CF343D Water Tree
这道题要用树链剖分,我博客里有对树链剖分的详细介绍
这明显是弱智题
树剖套珂朵莉树多简单啊
前置芝士:珂朵莉树
窝博客里对珂朵莉树的介绍
没什么好说的自己看看吧
先树剖一下
第一种操作,把一个点的子树全部变成1
因为做了树剖,所以珂朵莉树区间赋值就可以了
第二种操作就重链往上跳,重链全变成0,珂朵莉树也能完成
第三种操作直接split一下就行
这道题真的很简单,细节看代码(珂朵莉树实际就是一种暴力)
因为数据随机,所以有可能会有点慢
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define N 500005
using namespace std;
inline int read()
{
int f=1,x=0;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
return f*x;
}
struct edge{
int to,next;
}e[N<<1];
int head[N],cnt=0;
inline void add(register int u,register int v)
{
e[++cnt]=(edge){v,head[u]};
head[u]=cnt;
}
int n,m;
int size[N],l[N],son[N],dep[N],fa[N],top[N],tot=0;
inline void dfs1(register int x)
{
size[x]=1;
for(register int i=head[x];i;i=e[i].next)
{
int v=e[i].to;
if(!dep[v])
{
dep[v]=dep[x]+1;
fa[v]=x;
dfs1(v);
size[x]+=size[v];
if(size[v]>size[son[x]])
son[x]=v;
}
}
}
inline void dfs2(register int x,register int t)
{
l[x]=++tot;
top[x]=t;
if(son[x])
dfs2(son[x],t);
for(register int i=head[x];i;i=e[i].next)
{
int v=e[i].to;
if(v!=fa[x]&&v!=son[x])
dfs2(v,v);
}
}
struct node
{
int l,r;
mutable int v;
node(int L, int R=-1, int V=0):l(L), r(R), v(V) {}
bool operator<(const node& o) const
{
return l < o.l;
}
};
set<node> s;
#define IT set<node>::iterator
IT split(register int pos)
{
IT it = s.lower_bound(node(pos));
if (it != s.end() && it->l == pos)
return it;
--it;
int L = it->l, R = it->r;
int V = it->v;
s.erase(it);
s.insert(node(L, pos-1, V));
return s.insert(node(pos, R, V)).first;
}
inline void assign_val(register int l,register int r,register int val)
{
IT itr = split(r+1),itl = split(l);
s.erase(itl, itr);
s.insert(node(l, r, val));
}
inline void query(register int pos)
{
IT itpos = split(pos);
printf("%d\n",itpos->v);
}
inline void cal(register int pos)
{
int fpos=top[pos];
while(fpos!=1)
{
assign_val(l[fpos],l[pos],0);
pos=fa[fpos],fpos=top[pos];
}
assign_val(l[1],l[pos],0);
}
int main()
{
n=read();
for(register int i=1;i<n;++i)
{
int u=read(),v=read();
add(u,v),add(v,u);
}
dep[1]=fa[1]=1;
dfs1(1);
dfs2(1,1);
s.insert(node(0,500000+233));
int m=read();
while(m--)
{
int opt=read(),pos=read();
if(opt==1)
assign_val(l[pos],l[pos]+size[pos]-1,1);
else if(opt==2)
cal(pos);
else
query(l[pos]);
}
return 0;
}
【题解】Luogu CF343D Water Tree的更多相关文章
- CF343D Water Tree 树链剖分
问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> usi ...
- CF343D Water Tree
题目链接 题目翻译(摘自洛谷) 疯狂科学家Mike培养了一颗有根树,由n个节点组成.每个节点是一个要么装满水要么为空的贮水容器. 树的节点用1~n编号,其中根节点为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/ ...
- 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 #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 ...
- Water Tree(树链剖分+dfs时间戳)
Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...
- xtu summer individual 6 F - Water Tree
Water Tree Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
随机推荐
- CentOS6.5安装zookeeper-3.4.5(单机)
1.下载 下载链接:http://archive.apache.org/dist/zookeeper/ 本文下载版本:zookeeper-3.4.5.tar.gz 2.安装 安装目录:/usr/loc ...
- MySQL--9存储引擎
存储引擎:存储数据.查询数据的一种技术. 关系型数据库中数据是以表的形式存储的,所以存储引擎也叫表类型.
- centos7 cpanm安装,及perl模块安装
1. cpan安装 yum安装 yum install perl-App-cpanminus.noarch 注意:安装完成后,root及非root用户都可以使用cpanm安装模块,root用户直接用c ...
- Python基础(三)Mysql数据库安装及使用
在python下使用mysql需要: 1.安装mysql 2.安装python pymysql包(pymysql包支持py3 跟mysqldb用法差不多) 一.安装mysql mysql下载地址:h ...
- c#之枚举,结构体
1.枚举 2.结构体 例子1: using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- 004-全局应用程序类Global.asax
服务器对象:Request.Response.Server.Session.Application.Cookie //功能1:为服务器对象注册Start.End处理 protected void Ap ...
- leetCoder-wordBreak判断能否分词
题目 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determ ...
- word2vector 资料
http://blog.csdn.net/garfielder007/article/details/51345201 https://cs224d.stanford.edu/lecture_note ...
- C#之Action的实际应用例子
public class DemoAction{ public Action action; public Action<int> action1; public Action<in ...
- Visual Assist 10.9.2248 破解版(支持VS2017)
[1]下载安装包 下载地址:https://download.csdn.net/download/qq_20044811/10597708 [2]安装与破解方法 第一步:关闭VS所有打开窗体 第二步: ...