模板题 Problem I Link Cut Tree
@(XSY)[LCT]
Description
你们这么轻松A了前两题,要是AK了我可就惨了,所以这道题一定要是难(shui)题。那出什么难题呢?有了,这样吧,第一题是数,第二题是树,那我就出个同时含有数和树的题不就好了(废话,什么题没有数)。好,那就好办了。看,这里有一个n个节点且节点带权的树,然后我会要你去执行一些操作,处理一些询问。操作和询问有以下4种: 1、‘1 x y’表示将节点x与节点y用一条边相连接,若节点x与节点y本来已经是连通的话,则输出-1表示操作不合法; 2、‘2 x y’表示将当以节点x为根时,节点y与它的父亲节点相连接的边删除,若节点x与节点y本来已经是不连通的话,则输出-1表示操作不合法; 3、‘3 w x y’表示将节点x到节点y路径上的所有点点权加上w,若节点x与节点y本来已经是不连通的话,则输出-1表示操作不合法; 4、‘4 x y’表示请你输出节点x到节点y路径上的所有点中最大的点权,若节点x与节点y本来已经是不连通的话,则输出-1表示操作不合法。 终于有道比较难的题了,这下你们应该不能在1个小时内做出来了吧。什么,你们还有3个小时……
Input
第一行一个整数n表示树的节点个数。 接下来n-1行每行两个整数x、y,表示初始状态节点x与节点y间有一条边。 接下来一行n个整数,第i个数表示节点i的初始点权。 接下来一行一个整数m,表示操作数。 接下来m行,每行以‘1 x y’或‘2 x y’ 或‘3 w x y’ 或‘4 x y’的形式描述操作与询问。
Output
对于每个合法询问,输出一行一个整数表示最大点权。 对于每个非法询问或操作,输出一行一个整数-1表示非法。
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
对于50%的数据:1<=n,m<=1000。 对于100%的数据:1<=n,m<=3*10^5,0<=初始点权,w<=3000。
Solution
终于写了一次真的LCT...以前写的都是假的, 时间复杂度根本就不对...
这一题是极好的模板题, 既有update操作, 也有pushdown操作. pushdown操作只有在splay之前才需要, 而update操作则要特别注意, 要用到的地方包括: splay中的rotate, access操作和cut操作. 总而言之, 就是只要是修改到了辅助树中一个点的儿子节点, 就要对其本身进行update.
#include <cstdio>
#include <cctype>
#include <algorithm>
namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
sgn *= -1;
while(isdigit(c))
a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
}
struct linkCutTree
{
struct node
{
node *pre, *suc[2];
int isRoot, rev;
struct data
{
int w, mx, tag;
inline void add(int a)
{
w += a, mx += a, tag += a;
}
}infr;
inline node()
{
pre = suc[0] = suc[1] = NULL;
isRoot = 1, rev = 0;
infr.w = infr.mx = infr.tag = 0;
}
inline int getRelation()
{
if(pre == NULL)
return -1;
return this == pre->suc[1];
}
inline void pushdown()
{
if(! isRoot)
pre->pushdown();
if(rev)
reverse();
if(suc[0] != NULL)
suc[0]->infr.add(infr.tag);
if(suc[1] != NULL)
suc[1]->infr.add(infr.tag);
infr.tag = 0;
}
inline void update()
{
infr.mx = infr.w;
if(suc[0] != NULL)
infr.mx = std::max(infr.mx, suc[0]->infr.mx);
if(suc[1] != NULL)
infr.mx = std::max(infr.mx, suc[1]->infr.mx);
}
inline void reverse()
{
std::swap(suc[0], suc[1]);
rev ^= 1;
if(suc[0] != NULL)
suc[0]->rev ^= 1;
if(suc[1] != NULL)
suc[1]->rev ^= 1;
}
};
node *nd;
inline void init(int n)
{
nd = new node[n];
}
inline void rotate(node *u)
{
node *pre = u->pre, *prepre = pre->pre;
int k = u->getRelation();
if(u->suc[k ^ 1] != NULL)
u->suc[k ^ 1]->pre = pre;
pre->suc[k] = u->suc[k ^ 1];
u->suc[k ^ 1] = pre;
u->pre = prepre;
if(pre->isRoot)
pre->isRoot = 0, u->isRoot = 1;
else
prepre->suc[pre->getRelation()] = u;
pre->pre = u;
pre->update(), u->update();
}
inline void splay(node *u)
{
u->pushdown();
while(! u->isRoot)
{
if(! u->pre->isRoot)
rotate(u->pre->getRelation() == u->getRelation() ? u->pre : u);
rotate(u);
}
}
inline void access(node *u)
{
splay(u);
if(u->suc[1] != NULL)
u->suc[1]->isRoot = 1, u->suc[1] = NULL, u->update();
while(u->pre != NULL)
{
node *pre = u->pre;
splay(pre);
if(pre->suc[1] != NULL)
pre->suc[1]->isRoot = 1;
pre->suc[1] = u;
u->isRoot = 0;
u->update();
splay(u);
}
}
inline void makeRoot(node *u)
{
access(u);
u->rev ^= 1;
}
inline node* findRoot(node *u)
{
access(u);
while(u->suc[0] != NULL)
u = u->suc[0];
access(u); //find root完一定要access一下啊
return u;
}
inline void cut(node *u, node *v)
{
makeRoot(u);
access(v);
access(v);
v->suc[0]->isRoot = 1, v->suc[0]->pre = NULL, v->suc[0] = NULL;
v->update();
}
inline void link(node *u, node *v)
{
makeRoot(u);
access(v);
access(v);
u->pre = v;
access(u);
}
inline void modify(node *u, node *v, int a)
{
makeRoot(u);
access(v);
access(v);
v->infr.add(a);
}
inline int query(node *u, node *v)
{
makeRoot(u);
access(v);
access(v);
return v->infr.mx;
}
}org;
int main()
{
#ifndef ONLINE_JUDGE
freopen("LCT.in", "r", stdin);
freopen("LCT.out", "w", stdout);
#endif
using namespace Zeonfai;
int n = getInt();
org.init(n + 1);
static int *tmp = new int[n << 1];
for(int i = 2; i < n << 1; i += 2)
tmp[i - 1] = getInt(), tmp[i] = getInt();
for(int i = 1; i <= n; ++ i)
org.nd[i].infr.add(getInt());
for(int i = 2; i < n << 1; i += 2)
org.link(org.nd + tmp[i - 1], org.nd + tmp[i]);
delete[] tmp;
int m = getInt();
for(int i = 0; i < m; ++ i)
{
int opt = getInt(), u = getInt(), v = getInt();
if(opt == 1)
{
org.makeRoot(org.nd + u);
if(org.findRoot(org.nd + v) == org.nd + u)
puts("-1");
else
org.link(org.nd + u, org.nd + v);
}
else if(opt == 2)
{
org.makeRoot(org.nd + u);
if(org.findRoot(org.nd + v) != org.nd + u)
puts("-1");
else
org.cut(org.nd + u, org.nd + v);
}
else if(opt == 3)
{
int tmp = getInt();
org.makeRoot(org.nd + v);
if(org.findRoot(org.nd + tmp) != org.nd + v)
puts("-1");
else
org.modify(org.nd + v, org.nd + tmp, u);
}
else if(opt == 4)
{
org.makeRoot(org.nd + u);
if(org.findRoot(org.nd + v) != org.nd + u)
puts("-1");
else
printf("%d\n", org.query(org.nd + u, org.nd + v));
}
}
}
模板题 Problem I Link Cut Tree的更多相关文章
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
随机推荐
- shell脚本,通过一个shell程序计算n的阶乘。
[root@localhost ~]# cat jiechen.sh #!/bin/bash #设计一个shell程序计算n的阶乘,要求: #.从命令行接收参数n; #.在程序开始后立即判断n的合法性 ...
- C# IsNullOrEmpty与IsNullOrWhiteSpace
IsNullOrEmpty:非空非NULL判断 IsNullOrWhiteSpace:非空非NULL非空格判断 后者优于前者 if (!string.IsNullOrWhiteSpace(valueE ...
- 【dp】拔河比赛
01背包:感谢ZCK大佬 题目描述 学校举行拔河比赛,所有的人被分成了两组,每个人必须(且只能够)在其中的一组,要求两个组的人数相差不能超过1,且两个组内的所有人体重加起来尽可能地接近. 输入 输入中 ...
- PAT Basic 1019
1019 数字黑洞 给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字.一直重复这样做,我们很快会停在有“数字 ...
- 算法学习记录-图(DFS BFS)
图: 目录: 1.概念 2.邻接矩阵(结构,深度/广度优先遍历) 3.邻接表(结构,深度/广度优先遍历) 图的基本概念: 数据元素:顶点 1.有穷非空(必须有顶点) 2.顶点之间为边(可空) 无向图: ...
- goland 快键键整理及注册
https://my.oschina.net/lemos/blog/1358731 http://idea.lanyus.com/
- document文档碎片
var arrText = ["1","2","3","4","5","6",& ...
- 在web中绘制图像 -- canvas篇
汗,不小心点击发布了.其实正在编辑中...... HTML Canvas是现代浏览器中非常棒的绘图技术,Canvas可以绘制图形,操作图片,制作游戏,创建动画等:Canvas是很容易使用的,下面我们来 ...
- PHP 获取文件名和扩展名的方法
dirname(path) path: 代表你的文件路径,必须为绝对路径,可以使用__FILE__, 表示列出当前文件的绝对路径,包含文件名 函数会返回当前文件的上一级路径,也就是除了文件名称的路径 ...
- HDU 5441 Travel
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Descrip ...