代码

#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. Java 8 新特性终极版

    声明:本文翻译自Java 8 Features Tutorial – The ULTIMATE Guide,翻译过程中发现并发编程网已经有同学翻译过了:Java 8 特性 – 终极手册,我还是坚持自己 ...

  2. px 和 em换算

    常用px,pt,em换算表 pt (point,磅):是一个物理长度单位,指的是72分之一英寸. px (pixel,像素):是一个虚拟长度单位,是计算机系统的数字化图像长度单位,如果px要换算成物理 ...

  3. android面试题之七

    三十六.请解释下在单线程模型中Message.Handler.Message Queue.Looper之间的关系. 简单的说,Handler获取当前线程中的looper对象,looper用来从存放Me ...

  4. 《第一行代码》学习笔记2-Android开发特色

    1.四大组件:活动(Activity),服务(Service),广播接收器(Broadcast Receiver),内容提供器(Content Provider). Activity:应用中看得到的东 ...

  5. DBHerperl类

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.D ...

  6. .net 判断日期格式yyyy-MM-dd hh:MM:ss的正则表达式

    加上引用: using System.Text.RegularExpressions; /// <summary> /// 检查字符串是否是日期格式        /// </sum ...

  7. .net 实现注册邮箱验证激活

    没事上网当了个注册邮箱验证激活的代码,用起来感觉还不错,特意和大家要一起分享一下 下面是主要实现代码: uing System.Net.Mail; public partial class jquer ...

  8. oracle数据库ID自增长

    使用sequence --创建sequenceCREATE SEQUENCE emp_sequence      INCREMENT BY 1  -- 每次加几个      START WITH 1  ...

  9. MYSQLI DEMO

    1.Select // DEMO mysqli连接方式参考 $db = new mysqli("localhost:3306", "root", "& ...

  10. crontab经验

    1.基本格式  第1列分钟1-59  第2列小时1-23(0表示子夜)  第3列日1-31  第4列月1-12  第5列星期0-6(0表示星期天)  第6列要运行的命令 2.关于日志 (1)基本日志位 ...