代码

#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=1e9+;
const int maxn=;
int val[maxn];
int N,Q;
vector<int> G[maxn];
struct lct
{
lct *fa,*son[];
int rev,v,mv,add;
};
struct LCT
{
lct data[maxn];
lct *null;
void init(int Size=maxn-) //初始化
{
null=data; //null指向首元素
for(int i=;i<=Size;i++)
{
data[i].son[]=data[i].son[]=data[i].fa=null;
data[i].v=data[i].mv=val[i];
data[i].rev=data[i].add=;
}
null->v=null->mv=-INF;
}
void dfs(int u,int fa)
{
data[u].fa=data+fa;
int Size=G[u].size();
for(int i=;i<Size;i++)
{
int v=G[u][i];
if(v==fa) continue;
dfs(v,u);
}
}
void push_add(lct* x,int add)
{
if(x==null) return;
x->v+=add;
x->add+=add;
x->mv+=add;
}
void push_rev(lct* x)
{
if(x==null) return;
x->rev=!x->rev;
swap(x->son[],x->son[]);
}
void pushdown(lct* x)
{
if(x->add!=)
{
push_add(x->son[],x->add);
push_add(x->son[],x->add);
x->add=;
}
if(x->rev)
{
push_rev(x->son[]);
push_rev(x->son[]);
x->rev=;
}
}
void pushup(lct* x)
{
if(x==null) return;
x->mv=x->v;
x->mv=max(x->mv,x->son[]->mv);
x->mv=max(x->mv,x->son[]->mv);
}
bool Same(lct* x,lct* &y) //判断x和x的父亲是否在同一树里
{
return (y=x->fa)!=null&&(y->son[]==x||y->son[]==x);
}
void Rotate(lct* x,int d) //翻转
{
lct* y=x->fa; //x的父亲
y->son[d^]=x->son[d];
if(x->son[d]!=null) x->son[d]->fa=y; //x的子节点的父亲指向y
x->fa=y->fa; //连接
if(y->fa->son[]==y) x->fa->son[]=x;
else if(y->fa->son[]==y) x->fa->son[]=x;
x->son[d]=y;
y->fa=x;
}
void Splay(lct* x)
{
pushdown(x); //清除标记
lct* y;
while(Same(x,y)) //没有到树的最顶点
{
pushdown(y);
pushdown(x);
Rotate(x,y->son[]==x); //翻转
pushup(y);
pushup(x);
}
}
lct* Access(lct* u) //打通路径,返回的是根
{
lct *v=null;
for(;u!=null;u=u->fa)
{
Splay(u);
u->son[]=v;
pushup(v=u);
}
return v;
}
lct* GetRoot(lct* x) //得到根
{
for(x=Access(x);pushdown(x),x->son[]!=null;x=x->son[]) pushup(x);
return x;
}
void MakeRoot(lct* x) //使x成为根
{
Access(x);
Splay(x);
push_rev(x);
}
void Link(lct* x,lct* y) //连接两个点
{
MakeRoot(x);
x->fa=y;
Access(x);
}
void Cut(lct* x,lct* y) //断开两个点
{
MakeRoot(x);
Access(y);
Splay(y);
y->son[]->fa=null;
y->son[]=null;
}
void Add(lct* x,lct* y,int add)
{
MakeRoot(x);
push_add(Access(y),add);
}
int MaxVal(lct* x,lct* y)
{
MakeRoot(x);
Access(y);
Splay(y);
return y->mv;
}
}A;
int main()
{
while(scanf("%d",&N)!=EOF)
{
for(int i=;i<=N;i++) G[i].clear();
int x,y;
for(int i=;i<N;i++)
{
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
val[]=;
for(int i=;i<=N;i++) scanf("%d",&val[i]);
A.init(N);
A.dfs(,);
int type,w;
scanf("%d",&Q);
while(Q--)
{
scanf("%d",&type);
if(type==) scanf("%d",&w);
scanf("%d%d",&x,&y);
bool same=(A.GetRoot(A.data+x)==A.GetRoot(A.data+y));
if(type==)
{
if(same) printf("-1\n");
else A.Link(A.data+x,A.data+y);
}
else if(type==)
{
if(x==y||!same) printf("-1\n");
else A.Cut(A.data+x,A.data+y);
}
else if(type==)
{
if(!same) printf("-1\n");
else A.Add(A.data+x,A.data+y,w);
}
else
{
if(!same) printf("-1\n");
else printf("%d\n",A.MaxVal(A.data+x,A.data+y));
}
}
printf("\n");
}
return ;
}

hdu4010-Query on The Trees(lct分裂合并加值查询最大值)的更多相关文章

  1. HDU4010 Query on The Trees(LCT)

    人生的第一道动态树,为了弄懂它的大致原理,需要具备一些前置技能,如Splay树,树链剖分的一些概念.在这里写下一些看各种论文时候的心得,下面的代码是拷贝的CLJ的模板,别人写的模板比较可靠也方便自己学 ...

  2. HDU4010 Query on The Trees (LCT动态树)

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  3. HDOJ 4010 Query on The Trees LCT

    LCT: 分割.合并子树,路径上全部点的点权添加一个值,查询路径上点权的最大值 Query on The Trees Time Limit: 10000/5000 MS (Java/Others)   ...

  4. hdu4010 Query On The Trees

    Problem Description We have met so many problems on the tree, so today we will have a query problem ...

  5. hdu 4010 Query on The Trees LCT

    支持:1.添加边 x,y2.删边 x,y3.对于路径x,y上的所有节点的值加上w4.询问路径x,y上的所有节点的最大权值 分析:裸的lct...rev忘了清零死循环了两小时... 1:就是link操作 ...

  6. 动态树(LCT):HDU 4010 Query on The Trees

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  7. [BZOJ4530][Bjoi2014]大融合 LCT + 启发式合并

    [BZOJ4530][Bjoi2014]大融合 试题描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是 ...

  8. Hdu 4010-Query on The Trees LCT,动态树

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  9. 【bzoj3091】城市旅行 LCT区间合并

    题目描述 输入 输出 样例输入 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 样例输出 16/3 6/1 题解 LCT区间合并 前三个 ...

随机推荐

  1. JFrame编程

    最基础的窗口 public test1() { setTitle("test1"); setSize(300,300); //设置窗口大小 setLocation(300,300) ...

  2. Java高级软件工程师面试考纲

    如果要应聘高级开发工程师职务,仅仅懂得Java的基础知识是远远不够的,还必须懂得常用数据结构.算法.网络.操作系统等知识.因此本文不会讲解具体的技术,笔者综合自己应聘各大公司的经历,整理了一份大公司对 ...

  3. python字符串(移除空白,长度,索引,分割,切片,拼接,格式化输出)

    常用功能: 移除空白: >>> name = "meng" >>> name 'meng' >>> name.strip() ...

  4. tab切换jquery代码

    http://immmmm.com/jquery-tab-switch-code-improved.html html <div id="sidebar-tab">   ...

  5. [Redux] Extracting Presentational Components -- TodoApp

    Finally, I just noticed that the to-do app component doesn't actually have to be a class. I can turn ...

  6. android调试系列--使用ida pro调试so

    1.工具介绍 IDA pro: 反汇编神器,可静态分析和动态调试. 模拟机或者真机:运行要调试的程序. 样本:阿里安全挑战赛第二题:http://pan.baidu.com/s/1eS9EXIM 2. ...

  7. (转)CSS 禁止浏览器滚动条的方法

    1.完全隐藏 在里加入scroll="no",可隐藏滚动条:    这个我用的时候完全没效果,不知道是什么原因!不过好多人说这么用可以,大概是用的位置不一样吧   2.在不需要时隐 ...

  8. React-nwb的使用

    一.查看nwb的版本 nwb -v 二.创建一个react项目 nwb new react-app react-demo 三.启动项目 nwb serve

  9. sdsdd

    while(scanf("%d",&n)!=EOF) { res=-; level(tmp,n,res,); printf("%d/n",res); }

  10. lnmp安装fileinfo扩展

    1.错误: PHP Fileinfo extension must be installed/enabled to use Intervention Image. 2.原因: 缺少 fileinfo扩 ...