简单的树链剖分+线段树

#include<bits\stdc++.h>
using namespace std;
#define pb push_back
#define lson root<<1,l,midd
#define rson root<<1|1,midd+1,r
const int M=5e5+;
vector<int>g[M];
int tree[M<<],lazy[M<<],top[M],son[M],fa[M],sz[M],dfn[M],to[M],deep[M],cnt,n;
void dfs1(int u,int f){
sz[u]=;
fa[u]=f;
deep[u]=deep[f]+;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(v!=f){
dfs1(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]])
son[u]=v;
}
}
}
void dfs2(int u,int t){
dfn[u]=++cnt;
to[cnt]=u;
top[u]=t;
if(!son[u])
return ;
dfs2(son[u],t);
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(v!=fa[u]&&v!=son[u])
dfs2(v,v);
}
}
void pushdown(int root){
tree[root<<]=tree[root<<|]=lazy[root]-;
lazy[root<<]=lazy[root<<|]=lazy[root];
lazy[root]=; }
void update(int L,int R,int w,int root,int l,int r){
if(L<=l&&r<=R){
tree[root]=w;
lazy[root]=w+;
return ;
}
if(lazy[root])
pushdown(root);
int midd=(l+r)>>;
if(L<=midd)
update(L,R,w,lson);
if(R>midd)
update(L,R,w,rson);
}
void Update(int u,int v,int w){
while(top[u]!=top[v]){
if(deep[top[u]]<deep[top[v]])
swap(u,v);
update(dfn[top[u]],dfn[u],w,,,n);
u=fa[top[u]];
}
if(deep[u]<deep[v])
swap(u,v);
update(dfn[v],dfn[u],w,,,n);
}
int query(int pos,int root,int l,int r){
if(l==r){
return tree[root];
}
if(lazy[root])
pushdown(root);
int midd=(l+r)>>;
if(pos<=midd)
return query(pos,lson);
if(pos>midd)
return query(pos,rson);
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].pb(v);
g[v].pb(u);
}
dfs1(,);
dfs2(,);
int m;
scanf("%d",&m);
while(m--){
int op,u;
scanf("%d%d",&op,&u);
if(op==){
update(dfn[u],dfn[u]+sz[u]-,,,,n);
}
else if(op==){
Update(,u,);
}
else{
printf("%d\n",query(dfn[u],,,n));
}
}
return ;
}

最新文章

  1. FILE文件流的中fopen、fread、fseek、fclose的使用
  2. 自动化运维,远程交互从服务器A上ssh到服务器B上,然后执行服务器B上的命令。
  3. option配置
  4. UVa 10870 (矩阵快速幂) Recurrences
  5. Getting started with new I/O (NIO)--reference
  6. Android高效的应用程序开发工具集1---ant构建一个简单的Android工程
  7. 详细介绍Java虚拟机(JVM)
  8. Hyper-V虚拟机故障导致数据文件丢失的数据恢复全过程
  9. javascrpt_数组学习
  10. 【视频】Entity Framework Core 2.* 入门教程
  11. firewall端口放行
  12. Go条件语句、switch和循环语句
  13. 自定义MVC框架之工具类-图像处理类
  14. 两种方法获取MyBatis刚刚插入的id
  15. [Linux] ubuntu各目录含义
  16. 如何修改被hosts.deny禁止访问的IP
  17. 一、用Delphi10.3 创建一条JSON数据
  18. 2015306 白皎 《网络攻防》Exp5 MSF基础应用
  19. vue指令与组件
  20. C++头文件预编译与命名空间使用方法

热门文章

  1. (转载)Tomcat 报错 (The tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config is mi)
  2. SQL基础教程(第2版)第4章 数据更新:练习题
  3. 2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem
  4. Java学习十八
  5. Ubuntu16.04编译tensorflow的C++接口
  6. Python批量重命名文件
  7. bfs--奇怪的电梯P1135
  8. 17.3.12----OS模块ya
  9. linux的/dev内容介绍
  10. 参考JDK1.8源码,自己写一个类似于ArrayList的动态数组