题意:

  给出一颗树,有4种操作:

1、如果x和y不在同一棵树上则在xy连边

2、如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离

3、如果x和y在同一棵树上则x到y的路径上所有的点权值+w

4、如果x和y在同一棵树上则输出x到y路径上的最大值

/*
本来一道很水的LCT,结果hdu的提交页面被我刷屏了。。。
还是too young too simple啊,刚开始不知道多组数据,提交了N次,然后又因为下面的赋值问题提交了N++次。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 300010
#define inf 1000000000
using namespace std;
int fa[N],son[N][],x[N],y[N],val[N],mx[N],tag[N],rev[N],st[N],n,m;
void pushdown(int x){
int l=son[x][],r=son[x][];
if(rev[x]){
swap(son[x][],son[x][]);
rev[l]^=;rev[r]^=;rev[x]^=;
}
if(tag[x]){
if(l){tag[l]+=tag[x];val[l]+=tag[x];mx[l]+=tag[x];}
if(r){tag[r]+=tag[x];val[r]+=tag[x];mx[r]+=tag[x];}
tag[x]=;
}
}
void pushup(int x){
int l=son[x][],r=son[x][];
mx[x]=max(val[x],max(mx[l],mx[r]));
}
bool isroot(int x){
return son[fa[x]][]!=x&&son[fa[x]][]!=x;
}
void rotate(int x){
int y=fa[x],z=fa[y],l,r;
if(son[y][]==x)l=;else l=;r=l^;
if(!isroot(y)){
if(son[z][]==y) son[z][]=x;
else son[z][]=x;
}
fa[x]=z;fa[y]=x;fa[son[x][r]]=y;
son[y][l]=son[x][r];son[x][r]=y;
pushup(y);pushup(x);
}
void splay(int x){
int top=;st[++top]=x;
for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
for(int i=top;i;i--) pushdown(st[i]);
while(!isroot(x)){
int y=fa[x],z=fa[y];
if(!isroot(y)){
if(son[z][]==y^son[y][]==x)rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
int t=;
while(x){
splay(x);
son[x][]=t;
pushup(x);
t=x;x=fa[x];
}
}
void makeroot(int x){
access(x);
splay(x);
rev[x]^=;
}
void join(int x,int y){
makeroot(x);
fa[x]=y;
}
void cut(int x,int y){
makeroot(x);
access(y);
splay(y);
//fa[son[y][0]]=son[y][0]=0;
//我可能学了假的c++,连从右到左赋值都不知道!!!
son[y][]=fa[son[y][]]=;
pushup(y);
}
int find(int x){
access(x);splay(x);
int y=x;
while(son[y][]) y=son[y][];
return y;
}
void work(){
mx[]=-inf;
for(int i=;i<n;i++)scanf("%d%d",&x[i],&y[i]);
for(int i=;i<=n;i++)scanf("%d",&val[i]),mx[i]=val[i];
for(int i=;i<n;i++)join(x[i],y[i]);
scanf("%d",&m);
int opt,x,y,w;
for(int i=;i<=m;i++){
scanf("%d",&opt);
if(opt==){
scanf("%d%d",&x,&y);
if(find(x)==find(y)){printf("-1\n");continue;}
join(x,y);
}
else if(opt==){
scanf("%d%d",&x,&y);
if(find(x)!=find(y)||x==y){printf("-1\n");continue;}
cut(x,y);
}
else if(opt==){
scanf("%d%d%d",&w,&x,&y);
if(find(x)!=find(y)){printf("-1\n");continue;}
makeroot(x);access(y);splay(y);
val[y]+=w;tag[y]+=w;mx[y]+=w;
}
else {
scanf("%d%d",&x,&y);
if(find(x)!=find(y)){printf("-1\n");continue;}
makeroot(x);access(y);splay(y);
printf("%d\n",mx[y]);
}
}
printf("\n");
}
int main(){
while(scanf("%d",&n)!=EOF){
memset(fa,,sizeof(fa));
memset(son,,sizeof(son));
memset(val,,sizeof(val));
memset(mx,,sizeof(mx));
memset(tag,,sizeof(tag));
memset(rev,,sizeof(rev));
work();
}
return ;
}

Query on The Trees(hdu 4010)的更多相关文章

  1. 动态树(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 ...

  2. HDOJ 4010 Query on The Trees LCT

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

  3. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  4. Disharmony Trees HDU - 3015

    Disharmony Trees HDU - 3015 One day Sophia finds a very big square. There are n trees in the square. ...

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

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

  6. S - Query on a tree HDU - 3804 线段树+dfs序

    S - Query on a tree HDU - 3804   离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...

  7. HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...

  8. HDU 4010 Query on The Trees(动态树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意:一棵树,四种操作: (1)若x和y不在一棵树上,将x和y连边: (2)若x和y在一棵树上, ...

  9. HDU 4010 Query on The Trees

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

随机推荐

  1. PBI DAX 中GroupBy

    平时工作中经常会遇到Group By 的情形,用sql 写group by 很容易,在PBI中可以这样写: SUMMARIZE(表名,GroupBy Key ,"聚合列命名",DI ...

  2. Xamarin.IOS binding库编译失败的解决办法

    报错:目标框架 Xamarin.iOS,Version=v1.0 未找到 复制 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professi ...

  3. 如何在腾讯云上安装Cloud Foundry

    Cloud Foundry是VMware推出的业界第一个开源PaaS云平台,它支持多种框架.语言.运行时环境.云平台及应用服务,使开发人员能够在几秒钟内进行应用程序的部署和扩展,无需担心任何基础架构的 ...

  4. JS对输入判断变化屏蔽中文输入法输入时连续触发事件的方法

    代码如下: //智能搜索提示 IntelligenceSearch: function IntelligenceSearch() { $('#keyWord').on('input', functio ...

  5. fckeditor的实例

                            第一步:去官网下载,删除多余的包 删除所有”_”开头的文件和文件夹   删除FCKeditor的目录下:   fckeditor.afp fckedit ...

  6. docker资源汇总

    https://github.com/hangyan/docker-resources/blob/master/README_zh.md   https://github.com/lightning- ...

  7. 【IDE_PyCharm】PyCharm中配置当鼠标悬停时快速提示方法参数

    方法一:通过在settings里面设置当鼠标至于方法之上时给出快速提示 方法二:按住Ctrl键,光标放在任意变量或方法上都会弹出该变量或方法的详细信息,点击鼠标还能跳转到变量或方法的定义处

  8. 用python实现自动玩21点小游戏

    1. 背景 前段时间发现一个论坛上(https://npupt.com/blackjack.php)有21点小游戏. 这个21点小游戏的规则是每个人开局都会获得随机点数,如果觉得点数小,可以继续摸牌. ...

  9. OpenWrt 路由器如何让 lan 口主机获得 ipv6 网络访问 -- 知乎

    本文转自知乎: OpenWrt 路由器如何让 lan 口主机获得 ipv6 网络访问? - mistforest的回答 - 知乎https://www.zhihu.com/question/29667 ...

  10. python中获取python版本号的方法【转】

    原文 python3 #!/usr/bin/python # 第1种方法 import platform print(platform.python_version()) >>> i ...