Query on The Trees

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6447    Accepted Submission(s):
2547

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.

 

题意

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

  1. 如果x和y不在同一棵树上,则在x,y之间连一条边
  2. 如果x和y在同一棵树上,并且x!=y,则把x换为树根,并把y和其父亲分离
  3. 如果x和y在同一棵树上,则x到y的路径上所有的点权值加上w
  4. 如果x和y在同一棵树上,则输出x到y路径上的最大值

code

LCT —— 神奇的数据结构

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ;
int ch[N][],fa[N],val[N],add[N],rev[N],mx[N],head[N];
int st[N],top,n,m,tot;
struct Edge{
int to,nxt;
}e[N<<]; inline int read() {
int x = ,f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar()) x = x * + ch - '';
return x * f;
}
void add_edge(int u,int v) {
e[++tot].to = v,e[tot].nxt = head[u],head[u] = tot;
}
void pushup(int x) {
mx[x] = max(max(mx[ch[x][]],mx[ch[x][]]),val[x]);
}
void pushdown(int x) {
int l = ch[x][],r = ch[x][];
if (rev[x]) {
rev[l] ^= ;rev[r] ^= ;
swap(ch[x][],ch[x][]);
rev[x] ^= ;
}
if (add[x]) {
if (l) add[l] += add[x],mx[l] += add[x],val[l] += add[x];
if (r) add[r] += add[x],mx[r] += add[x],val[r] += add[x];
add[x] = ;
}
}
bool isroot(int x) {
return ch[fa[x]][]!=x && ch[fa[x]][]!=x;
}
inline int son(int x) {
return ch[fa[x]][]==x;
}
void rotate(int x) {
int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
if (!isroot(y)) ch[z][c] = x;fa[x] = z;
ch[x][!b] = y;fa[y] = x;
ch[y][b] = a;if (a) fa[a] = y;
pushup(y);pushup(x);
}
void splay(int x) {
top = ;st[++top] = x;
for (int i=x; !isroot(i); i=fa[i]) st[++top] = fa[i];
while (top) pushdown(st[top--]);
while (!isroot(x)) {
int y = fa[x];
if (!isroot(y)) {
if (son(x)==son(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
}
void access(int x) {
for (int t=; x; t=x,x=fa[x]) {
splay(x);ch[x][] = t;pushup(x);
}
}
void makeroot(int x) {
access(x);splay(x);rev[x] ^= ;
}
void link(int x,int y) {
makeroot(x);fa[x] = y;
}
void cut(int x,int y) {
makeroot(x);access(y);splay(y);
ch[y][] = fa[ch[y][]] = ;pushup(y);
}
int find(int x) {
access(x);splay(x);
while (ch[x][]) x = ch[x][];
return x;
}
void update(int x,int y,int z) {
makeroot(x);access(y);splay(y);
add[y] += z;mx[y] += z;val[y] += z;
}
int query(int x,int y) {
makeroot(x);access(y);splay(y);
return mx[y];
}
int main() {
while (scanf("%d",&n) != EOF) {
for (int i=; i<=n; ++i)
head[i] = add[i] = rev[i] = fa[i] = ch[i][] = ch[i][] = ;
mx[] = -1e9;tot = ;
for (int a,b,i=; i<n; ++i) {
a = read();b = read();
add_edge(a,b);add_edge(b,a);
}
for (int i=; i<=n; ++i) mx[i] = val[i] = read();
st[++top] = ;
for (int k=; k<=top; ++k) {
int u = st[k];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v != fa[u]) {
fa[v] = u;st[++top] = v;
}
}
}
m = read();
while (m--) {
int opt = read(),x = read(),y = read(),w;
if (opt==) {
if (find(x) == find(y)) puts("-1");
else link(x,y);
}
else if (opt==) {
if (find(x) != find(y) || x==y) puts("-1");
else cut(x,y);
}
else if (opt==) {
w = x;x = y;y = read();
if (find(x) != find(y)) puts("-1");
else update(x,y,w);
}
else {
if (find(x) != find(y)) puts("-1");
else printf("%d\n",query(x,y));
}
}
puts("");
}
return ;
}

HDU4010 Query on The Trees (LCT动态树)的更多相关文章

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

  2. HDU4010 Query on The Trees(LCT)

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

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

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

  5. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  6. HDOJ 4010 Query on The Trees LCT

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

  7. LCT 动态树 模板

    洛谷:P3690 [模板]Link Cut Tree (动态树) /*诸多细节,不注意就会调死去! 见注释.*/ #include<cstdio> #include<iostream ...

  8. [HNOI2010]弹飞绵羊 (平衡树,LCT动态树)

    题面 题解 因为每个点都只能向后跳到一个唯一的点,但可能不止一个点能跳到后面的某个相同的点, 所以我们把它抽象成一个森林.(思考:为什么是森林而不是树?) 子节点可以跳到父节点,根节点再跳就跳飞了. ...

  9. Fzu Problem 2082 过路费 LCT,动态树

    题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528    Submit: 1654Time Limit ...

随机推荐

  1. springIOC+Mysql+springmvc事务测试题总结

    1.关于http1.1和1.0的长连接和短连接 两个都支持长连接和短连接 http1.0默认为短连接,也就是说,浏览器和服务器每进行一次HTTP操作,就建立一次连接,任务结束就中断连接 http1.1 ...

  2. Mybatis基础配置及增删查改操作

    一.简介 平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类. 不仅如此,访问不同的表,还会 ...

  3. js的加密和解密

    最近在研究js的加密和解密的问题,上网上搜出来很多方法,不过不知道到底哪一个会比较管用.这里是今天找到的一些关于base64加密解密的js代码,已经经过试验,可以使用,不过网上很多加密解密的工具,这种 ...

  4. ios 设置导航栏背景色

    //设置导航栏背景色 如果上面的不好用 就用下面的 [self.navigationController.navigationBar setBackgroundImage:[UIImage image ...

  5. Locust安装教程与使用

    Locust安装教程与使用官网地址:https://github.com/locustio/locust 如果是python3+以上的环境,需要下载locust项目源码进行安装 因Centos7.2环 ...

  6. PostgreSQL缓存

    目录[-] pg_buffercache pgfincore pg_prewarm dstat Linux ftools 使用pg_prewarm预加载关系/索引: pgfincore 输出: 怎样刷 ...

  7. JS编程规范指南

    原文:github.com/ryanmcdermott/clean-code-javascript 说明:本文翻译自 github 上的一个项目,只取部分精华. 一.变量 用有意义且常用的单词命名 / ...

  8. 使用Timer组件制作计时器

    实现效果: 知识运用: Timer组件的interval属性 //获取或设置Timer组件Tick事件发生的时间间隔 public int Interval {get;set} NumericUpDo ...

  9. Windows上PostgreSQL安装配置教程

    Windows上PostgreSQL安装配置教程 这篇文章主要为大家详细介绍了Windows上PostgreSQL安装配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 PostgreSQL的 ...

  10. ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点

    Web层用Structs2的action Service层用Spring的IoC和aop以及JdbcTemplate或者Transaction事务(创建对象及维护对象间的关系) Dao层用Hibern ...