CF343D Water Tree 树链剖分
问题描述
题解
树剖,线段树维护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 树链剖分的更多相关文章
- 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 ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- acwing 23. 矩阵中的路径
习题地址 https://www.acwing.com/problem/content/description/21/ 题目描述请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路 ...
- CentOS下yum方式安装FFmpeg
FFmpeg一个完整的跨平台解决方案,用于记录,转换和流式传输音频和视频. 文档:https://www.ffmpeg.org/documentation.html FFmpeg安装 1.安装Nux ...
- 报错:Something is already running on port 8000.
在用react框架的时候,用cnpm run dev命令执行项目时,有时会出现这种错误, 这是因为你之前执行过该命令,但是没关闭,解决办法是打开任务管理器, 在进程中找到node.exe,右键关闭这个 ...
- CF582E Boolean Function(DP,状态压缩,FMT)
简单题. 我第二道自己做出来的 2900 没毛病,我没切过 2800 的题 lqy:"CF 评分 2800 是中等难度" 我活个啥劲啊 为了方便(同时压缩状态个数),先建出表达式树 ...
- spring-framework-core-ioc Container
阅读须知 实例化bean xml方式实例化bean 注解方式实例化bean java方式实例化bean ClassPathXmlApplication和AnnotationConfigApplicat ...
- IT兄弟连 HTML5教程 介绍HTML5给你认识 习题
1.关于HTML5说法正确的是:(C) A.HTML5只是对HTML4的一个简单升级 B.所有主流浏览器都支持HTML5 C.HTML5新增了离线缓存机制 D.HTML5主要是针对移动端进行了优化 2 ...
- [转]Apache Doris资料汇总
参考资料 https://www.toutiao.com/i6709706901770207748/?tt_from=weixin&utm_campaign=client_share& ...
- 死磕 java同步系列之Semaphore源码解析
问题 (1)Semaphore是什么? (2)Semaphore具有哪些特性? (3)Semaphore通常使用在什么场景中? (4)Semaphore的许可次数是否可以动态增减? (5)Semaph ...
- js使用工具将表单封装成json字符串传到后台,js截取字符串(学生笔记)
<script src="js/jquery.min.js"></script> <script src="https://cdn.boot ...
- JMeter处理form-data类型的接口
最近的需求中,有的接口入参是form-data类型的,除了用python多进程代码进行压测,考虑用Jmeter试试看,比对一下结果. 线程数设置的是50,循环次数为100,一共发送5000次请求. H ...