hdu4010-Query on The Trees(lct分裂合并加值查询最大值)
代码
#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分裂合并加值查询最大值)的更多相关文章
- HDU4010 Query on The Trees(LCT)
人生的第一道动态树,为了弄懂它的大致原理,需要具备一些前置技能,如Splay树,树链剖分的一些概念.在这里写下一些看各种论文时候的心得,下面的代码是拷贝的CLJ的模板,别人写的模板比较可靠也方便自己学 ...
- HDU4010 Query on The Trees (LCT动态树)
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...
- HDOJ 4010 Query on The Trees LCT
LCT: 分割.合并子树,路径上全部点的点权添加一个值,查询路径上点权的最大值 Query on The Trees Time Limit: 10000/5000 MS (Java/Others) ...
- hdu4010 Query On The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- hdu 4010 Query on The Trees LCT
支持:1.添加边 x,y2.删边 x,y3.对于路径x,y上的所有节点的值加上w4.询问路径x,y上的所有节点的最大权值 分析:裸的lct...rev忘了清零死循环了两小时... 1:就是link操作 ...
- 动态树(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 ...
- [BZOJ4530][Bjoi2014]大融合 LCT + 启发式合并
[BZOJ4530][Bjoi2014]大融合 试题描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是 ...
- 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 ...
- 【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区间合并 前三个 ...
随机推荐
- 【转】TI蓝牙BLE 协议栈代码学习
BLE就是低功率蓝牙.要着重了解两种设备: dual-mode双模设备:简单说就是向下兼容. single-mode单模设备:仅仅支持BLE. 关于开发主要讲的是单模设备,它可以只靠纽扣电池即可持 ...
- Struts2小结
Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1.在软件设计上Struts 2 ...
- HTML5新增的一些属性和功能之一
大致可以分为10个方面: HTML5表单元素和属性 表单2.0 视音频处理 canvas绘图 SVG绘图 地理定位 拖放技术 web work web storage web socket 一.新的i ...
- 【git 问题小说说】 git add时候报错:LF will be replaced by CRLF
本文来自:http://blog.csdn.net/loovejava/article/details/22114477 最近工作在window平台,不怎么使用命令行了所以导致很多命令都不熟悉啦 哈哈 ...
- 利用java反射机制 读取配置文件 实现动态类载入以及动态类型转换
作者:54dabang 在spring的学习过程之中,我们能够看出通过配置文件来动态管理bean对象的优点(松耦合 能够让零散部分组成一个总体,而这些总体并不在意之间彼此的细节,从而达到了真正的物理上 ...
- WHY IE AGAIN? - string.charAt(x) or string[x]?
近期今天在写一个"删除字符串中反复字符串"的函数,代码例如以下: 开门见山,重点 string.charAt(index) 取代 string[index] function re ...
- Android播放音频的两种方式
一种使用MediaPlayer,使用这种方式通常是播放比较长的音频,如游戏中的背景音乐. 代码如下: private MediaPlayer mPlayer = null; mPlayer = Med ...
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
转自:http://blog.csdn.net/meegomeego/article/details/39890385 layoutSubviews总结 ios layout机制相关方法 - (CGS ...
- Markdown 学习笔记: Basics
Markdown 学习笔记: Basics 原文:Basics. 了解Markdown格式化句法的要点 本页对如何使用Markdown提供了一个简单的概述.在"句法"页中对Mark ...
- IE6 浏览器常见兼容问题 大汇总(23个)
IE6以及各个浏览器常见兼容问题 大汇总 综述:虽然说IE6在2014年4月将被停止支持,但是不得不说的是,IE6的市场并不会随着支持的停止而立刻消散下去,对于WEB前端开发工程师来说,兼容IE6 兼 ...