LCT:

分割、合并子树,路径上全部点的点权添加一个值,查询路径上点权的最大值

Query on The Trees

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 2582    Accepted Submission(s): 1208

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

There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them efficiently. Wish you have fun! 


 
Input
There are multiple test cases in our dataset. 

For each case, the first line contains only one integer N.(1 ≤ N ≤ 300000) The next N‐1 lines each contains two integers x, y which means there is an edge between them. It also means we will give you one tree initially. 

The next line will contains N integers which means the weight Wi of each node. (0 ≤ Wi ≤ 3000) 

The next line will contains an integer Q. (1 ≤ Q ≤ 300000) The next Q lines will start with an integer 1, 2, 3 or 4 means the kind of this operation. 

1. Given two integer x, y, you should make a new edge between these two node x and y. So after this operation, two trees will be connected to a new one. 

2. Given two integer x, y, you should find the tree in the tree set who contain node x, and you should make the node x be the root of this tree, and then you should cut the edge between node y and its parent. So after this operation, a tree will be separate
into two parts. 

3. Given three integer w, x, y, for the x, y and all nodes between the path from x to y, you should increase their weight by w. 

4. Given two integer x, y, you should check the node weights on the path between x and y, and you should output the maximum weight on it. 
 
Output
For each query you should output the correct answer of it. If you find this query is an illegal operation, you should output ‐1. 

You should output a blank line after each test case.
 
Sample Input
5
1 2
2 4
2 5
1 3
1 2 3 4 5
6
4 2 3
2 1 2
4 2 3
1 3 5
3 2 1 4
4 1 4
 
Sample Output
3
-1
7
Hint
We define the illegal situation of different operations:
In first operation: if node x and y belong to a same tree, we think it's illegal.
In second operation: if x = y or x and y not belong to a same tree, we think it's illegal.
In third operation: if x and y not belong to a same tree, we think it's illegal.
In fourth operation: if x and y not belong to a same tree, we think it's illegal.
 
Source
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=330000; int ch[maxn][2],pre[maxn],key[maxn];
int add[maxn],rev[maxn],Max[maxn];
bool rt[maxn]; void update_add(int r,int d)
{
if(!r) return ;
key[r]+=d;
add[r]+=d;
Max[r]+=d;
} void update_rev(int r)
{
if(!r) return ;
swap(ch[r][0],ch[r][1]);
rev[r]^=1;
} void push_down(int r)
{
if(add[r])
{
update_add(ch[r][0],add[r]);
update_add(ch[r][1],add[r]);
add[r]=0;
}
if(rev[r])
{
update_rev(ch[r][0]);
update_rev(ch[r][1]);
rev[r]=0;
}
} void push_up(int r)
{
Max[r]=max(max(Max[ch[r][0]],Max[ch[r][1]]),key[r]);
} void Rotate(int x)
{
int y=pre[x],kind=(ch[y][1]==x);
ch[y][kind]=ch[x][!kind];
pre[ch[y][kind]]=y;
pre[x]=pre[y];
pre[y]=x;
ch[x][!kind]=y;
if(rt[y]) rt[y]=false,rt[x]=true;
else ch[pre[x]][ch[pre[x]][1]==y]=x;
push_up(y);
} void P(int r)
{
if(!rt[r]) P(pre[r]);
push_down(r);
} void Splay(int r)
{
P(r);
while(!rt[r])
{
int f=pre[r],ff=pre[f];
if(rt[f]) Rotate(r);
else if((ch[ff][1]==f)==(ch[f][1]==r)) Rotate(f),Rotate(r);
else Rotate(r),Rotate(r);
}
push_up(r);
} int Access(int x)
{
int y=0;
for(;x;x=pre[y=x])
{
Splay(x);
rt[ch[x][1]]=true; rt[ch[x][1]=y]=false;
push_up(x);
}
return y;
} bool judge(int u,int v)
{
while(pre[u]) u=pre[u];
while(pre[v]) v=pre[v];
return u==v;
} void mroot(int r)
{
Access(r);
Splay(r);
update_rev(r);
} void lca(int &u,int &v)
{
Access(v); v=0;
while(u)
{
Splay(u);
if(!pre[u]) return ;
rt[ch[u][1]]=true;
rt[ch[u][1]=v]=false;
push_up(u);
u=pre[v=u];
}
} void link(int u,int v)
{
if(judge(u,v))
{
puts("-1");
return ;
}
mroot(u);
pre[u]=v;
} void cut(int u,int v)
{
if(u==v||!judge(u,v))
{
puts("-1");
return ;
}
mroot(u);
Splay(v);
pre[ch[v][0]]=pre[v];
pre[v]=0;
rt[ch[v][0]]=true;
ch[v][0]=0;
push_up(v);
} void Add(int u,int v,int w)
{
if(!judge(u,v))
{
puts("-1"); return ;
}
lca(u,v);
update_add(ch[u][1],w);
update_add(v,w);
key[u]+=w;
push_up(u);
} void query(int u,int v)
{
if(!judge(u,v))
{
puts("-1");
return ;
}
lca(u,v);
printf("%d\n",max(max(Max[v],Max[ch[u][1]]),key[u]));
} struct Edge
{
int to,next;
}edge[maxn*2]; int Adj[maxn],Size=0; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} void dfs(int u)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(pre[v]!=0) continue;
pre[v]=u;
dfs(v);
}
}
int n; int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=0;i<n+10;i++)
{
pre[i]=0; ch[i][0]=ch[i][1]=0;
rev[i]=0; add[i]=0; rt[i]=true;
} for(int i=0;i<n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
pre[1]=-1; dfs(1); pre[1]=0; for(int i=1;i<=n;i++)
{
scanf("%d",key+i);
Max[i]=key[i];
} int q;
scanf("%d",&q);
while(q--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int x,y;
scanf("%d%d",&x,&y);
link(x,y);
}
else if(op==2)
{
int x,y;
scanf("%d%d",&x,&y);
cut(x,y);
}
else if(op==3)
{
int x,y,w;
scanf("%d%d%d",&w,&x,&y);
Add(x,y,w);
}
else if(op==4)
{
int x,y;
scanf("%d%d",&x,&y);
query(x,y);
}
}
putchar(10);
}
return 0;
}

HDOJ 4010 Query on The Trees LCT的更多相关文章

  1. hdu 4010 Query on The Trees LCT

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

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

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

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

  4. 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 ...

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

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

  6. HDU 4010.Query on The Trees 解题报告

    题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...

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

    题意 给定一棵 \(n\) 个节点的树,每个点有点权.完成 \(m\) 个操作,操作四两种,连接 \((x,y)\) :提 \(x\) 为根,并断 \(y\) 与它的父节点:增加路径 \((x,y)\ ...

  8. HDU4010 Query on The Trees(LCT)

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

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

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

随机推荐

  1. SVM训练结果参数说明 训练参数说明 归一化加快速度和提升准确率 归一化还原

    原文:http://blog.sina.com.cn/s/blog_57a1cae80101bit5.html 举例说明 svmtrain -s 0 -?c 1000 -t 1 -g 1 -r 1 - ...

  2. (C++)已知String类的定义,实现其函数体

    CString类的定义如下: class CMyString{ public: CMyString(const char* pData=NULL); CMyString(const CMyString ...

  3. mysql5.5.15配置主从数据库

    1.编辑主库的my.cnf 在[mysqld]下添加如下配置 server-i=1 #一般默认为1,不需要修改(一般都以ip的后两位为server-id,保证全局的一致) read-only=0#主库 ...

  4. Unity3D开发之Mac OS 开发环境搭建 笔记

    http://www.cnblogs.com/zhaoqingqing/p/3383167.html 首先上几张图: 摸索了一上午,才搞定在模拟器中运行.至于在Iphone真机中运行,虽然有开发者证书 ...

  5. Js 的test方法

    Js 的test()方法 test() 方法用于检测一个字符串是否匹配某个模式. 定义和用法test() 方法用于检测一个字符串是否匹配某个模式. 如果字符串中有匹配的值返回 true ,否则返回 f ...

  6. JDBC-DAO经典模式 实现对数据库的增、删、改、查

    JDBC(Java Data Base Connection)的作用是连接数据库 先看下jdbc连接SQLServer数据库的简单例子 代码实现(FirstJDBC): package com.jdb ...

  7. Python 遍历dict

    遍历dict 由于dict也是一个集合,所以,遍历dict和遍历list类似,都可以通过 for 循环实现. 直接使用for循环可以遍历 dict 的 key: >>> d = { ...

  8. pomelo生命周期回调和组件加入

    一 生命周期回调 生命周期回调可以让开发人员在不同类型的server生命周期中进行详细操作. 提供的生命周期回调函数包含:beforeStartup,afterStartup,beforeShutdo ...

  9. Nginx如何保留真实IP和获取前端IP

    原理: squid,varnish以及nginx等,在做反向代理的时候,因为要代替客户端去访问服务器,所以,当请求包经过反向代理后,在代理服务器这里这个IP数据包的IP包头做了修改,最终后端web服务 ...

  10. Linux系统扩容根目录磁盘空间

    作者:非法小恋 一.使用背景 Linux根目录磁盘空间不够用了,当修改了虚拟机模版增加磁盘大小或者插入了一块新硬盘,但是发现系统里的大小还是没改变. 产生的原因是没有给磁盘格式化,没有增加分区. 二. ...