问题描述

LG-CF343D


题解

树剖,线段树维护0-1序列

yzhang:用珂朵莉树维护多好


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std; template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-'){fh=-1;ch=getchar(); }
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
} const int maxn=500007;
const int maxm=1000007; int n,T;
int Head[maxn],to[maxm],Next[maxm],tot; void add(int x,int y){
to[++tot]=y,Next[tot]=Head[x],Head[x]=tot;
} int size[maxn],son[maxn];
int fa[maxn],dep[maxn],top[maxn],New[maxn],pre[maxn],ind; void dfs1(int x,int f,int dp){
size[x]=1,fa[x]=f,dep[x]=dp;
int mx=-1;
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(y==f) continue;
dfs1(y,x,dp+1);size[x]+=size[y];
if(size[y]>mx) mx=size[y],son[x]=y;
}
} int dfn[maxn]; void dfs2(int x,int tp){
top[x]=tp,New[x]=++ind,pre[ind]=x,dfn[x]=ind;
if(!son[x]) return;
dfs2(son[x],tp);
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
dfn[x]=ind;
} #define lfc (x<<1)
#define rgc ((x<<1)|1)
#define mid ((l+r)>>1) int val[maxn<<2],lazy[maxn<<2]; int L,R,need; void pushdown(int x){
lazy[lfc]=lazy[rgc]=lazy[x];val[lfc]=val[rgc]=lazy[x]-1;
lazy[x]=0;
} void pushup(int x){
val[x]=val[lfc]+val[rgc];
} void change(int x,int l,int r){
if(L>r||R<l) return;
if(L<=l&&r<=R){
lazy[x]=need+1,val[x]=need;return;
}
if(lazy[x]) pushdown(x);
change(lfc,l,mid);change(rgc,mid+1,r);
pushup(x);
} int query(int x,int l,int r){
if(l==r) return val[x];
if(lazy[x]) pushdown(x);
int res;
if(L<=mid) res=query(lfc,l,mid);
else res=query(rgc,mid+1,r);
pushup(x);
return res;
} void cz1(){
int x;read(x);
L=New[x],R=dfn[x],need=1;
change(1,1,n);
} void cz2(){
int x,y=1;read(x);
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
L=New[top[x]],R=New[x],need=0;
change(1,1,n);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
L=New[x],R=New[y],need=0;
change(1,1,n);
} void cz3(){
int x;read(x);
L=New[x];
printf("%d\n",query(1,1,n));
} int main(){
read(n);
for(int i=1,x,y;i<n;i++){
read(x);read(y);
add(x,y);add(y,x);
}
dfs1(1,0,1);dfs2(1,1);
read(T);int op;
while(T--){
read(op);
if(op==1) cz1();
else if(op==2) cz2();
else cz3();
}
return 0;
}

CF343D Water Tree 树链剖分的更多相关文章

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

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

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

  3. Water Tree(树链剖分+dfs时间戳)

    Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit ...

  4. CodeForces 343D water tree(树链剖分)

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

  5. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  6. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  7. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  8. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

  9. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

随机推荐

  1. python第一次作业-Numpy练习

    1.创建一个边界值为1而内部都是0的数组,图例如下:[提示:]解此题可以先把所有值都设置为1,这是大正方形:其次,把边界除外小正方形全部设置为0.本题用到numpy的切片原理.多维数组同样遵循x[st ...

  2. IDEA2019 Maven项目出现Unable to import maven project: See logs for details解决办法

    问题1:Unable to import maven project: See logs for details 网上说,原因是IDEA2019和Maven3.6.2不兼容导致的,需要把Maven降级 ...

  3. WIN10X64LTSC2019中度精简版by双心

    WIN10X64LTSC2019中度精简版by双心https://www.cnblogs.com/liuzhaoyzz/p/11295032.html 一.前言:关于LTSC中度精简版的说明 一个MM ...

  4. Python连载43-current中的map函数、xml文件

    一.current中的map函数 1.map(fn,*iterable,timeout=None) (1)跟map函数相类似(2)函数需要异步执行(3)timeout代表超时时间 (4)map和sub ...

  5. React: React的复合组件

    一.介绍 不论Web界面是多么的复杂,它都是由一个个简单的组件组合起来实现的,既然会创建一个简单的组件,那么复杂的组件就有了下手的切入点了.现在来实现一个简单的复合组件.一个颜色面板,一共三部分组成. ...

  6. autojump--懒人利器

    只有打开过的目录 autojump 才会记录,所以使用时间越长,autojump 才会越智能. 可以使用 autojump 命令,或者使用短命令 j. 跳转到指定目录 j directoryName ...

  7. laravel中的表单请求类型和CSRF防护(六)

    laravel中为我们提供了绑定不同http请求类型的函数. Route::get('/test', function () {}); Route::post('/test', function () ...

  8. Knative 实战:一个微服务应用的部署

    作者 | 元毅 阿里云智能事业群高级开发工程师 在 Istio 中提供了一个 Bookinfo 的示例,用于演示微服务之间的调用,那么如何在 Knative 中部署这个示例呢?本文将会给大家介绍一下在 ...

  9. 常用regex正则表达式

    [单个]字符匹配 任意字符:表示由任意字符组成 \\:匹配一个'\' \n:匹配换行 \t:匹配制表符 [单个]字符集(可以从里面任选一个字符) [abc]:可以是字母abc中任意一个 [^abc]: ...

  10. oracle里面查询重复数据的方法

    一张person表,有id和name的两个字段,id是唯一的不允许重复,id相同则认为是重复的记录. select id from group by id having count(*) > 1 ...