代码

#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. ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度

    Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...

  2. hdu 2254 奥运

    点击打开hdu 2254 思路: 矩阵乘法 分析: 1 题目给定一个有向图,要求t1-t2天内v1-v2的路径的个数 2 根据离散数学里面的可达矩阵的性质,我们知道一个有向图的邻接矩阵的前n次幂的和即 ...

  3. java 版本SQLHelper

    package com.jack.SQLHelper; import java.sql.*;import java.util.logging.*;import javax.swing.table.*; ...

  4. Hadoop集群运行JNI程序

    要在Hadoop集群运行上运行JNI程序,首先要在单机上调试程序直到可以正确运行JNI程序,之后移植到Hadoop集群就是水到渠成的事情. Hadoop运行程序的方式是通过jar包,所以我们需要将所有 ...

  5. C#委托的详细使用

    代码如下: public delegate void GreetingDelegate(string name);//定义委托,它定义了可以代表方法的类型 class Program { public ...

  6. JavaScript学习笔记:检测数组方法

    检查数组的方法 很多时候我们需要对JavaScript中数据类型(Function.String.Number.Undefined.Boolean和Object)做判断.在JavaScript中提供了 ...

  7. MyEclipse 安装activiti designer

    下载activiti designer 文件地址:http://activiti.org/designer/archived/ 注意:我的是myeclipse9.0,我下载的版本是:(当我下载高版本安 ...

  8. C# KeyValuePair<TKey,TValue>与Container

    KeyValuePair<TKey,TValue>  KeyValuePair<TKey,TValue>是一个结构体,相当于C#一个Map类型的对象,可以通过它记录一个键/值对 ...

  9. 简单描述一下XIB与Storyboards,简述它们的优缺点。

    参考答案: 我倾向于纯代码开发,因此所提供的参考答案可能具有一定的个人感情,不过还是给大家说说自己的想法. 优点: XIB:在编译前就提供了可视化界面,可以直接拖控件,也可以直接给控件添加约束,更直观 ...

  10. JasperReport使用心得

    1. JasperReport 报表文件视图化生成工具iReport. iReport做为一个生成JasperReport的视图工具,和我们是使用的大多数报表创建工具没有太大的差别,都是拖控件,搭出报 ...