hdu 4010 Lct动态链接树
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <memory>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <cassert>
#include <string>
#include <ctime>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <set>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define req(i,a,b) for(int i=a;i>=b;i--)
#define rp(i,a) for(int i=head[a];i+1;i=edge[i].next)
#define cl(a,b) memset(a,b,sizeof a);
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mod 10007
const int inf = ~0u >> 2;
const ll INF = (1LL << 62) - 1;
double eps = 1e-12;
const int N = 300005 + 5;
const int M = 300005;
int n, m, d;
class Lct {
public:
Lct() {}
~Lct() {}
struct Node {
int value, size, add, reverse, mx;
Node *parent, *child[2];
}_nil, *node[N];
public:
void Init() {
nil = new Node;
nil->child[0] = nil->child[1] = nil;
nil->size = 0;
nil->parent = nil;
nil->mx = -(inf << 1) - 2;
nil->add = 0;
nil->value = -(inf << 1) - 2;
nil->reverse = 0;
Root = nil;
//nil->
//Insert(-(inf<<1)-2);
//Insert((inf<<1)+1);
//Insert(0);
//Insert(n + 1);
}
bool IsRoot(Node *x) {
if (x == nil)
return true;
if (x->parent->child[0] == x || x->parent->child[1] == x)
return false;
return true;
}
void update(Node *x) {
/*if (x == nil)
return;*/
x->mx = max(x->value, max(x->child[0]->mx, x->child[1]->mx));
//x->size = x->child[0]->size + x->child[1]->size + 1;
}
void down(Node *x) {
if (x == nil)return;
if (x->reverse) {
if (x->child[0] != nil) {
x->child[0]->reverse ^= 1;
}
if (x->child[1] != nil) {
x->child[1]->reverse ^= 1;
}
swap(x->child[0], x->child[1]);
x->reverse = 0;
}
if (x->add) {
if (x->child[0] != nil) {
x->child[0]->value += x->add;
x->child[0]->mx += x->add;
x->child[0]->add += x->add;
}
if (x->child[1] != nil) {
x->child[1]->value += x->add;
x->child[1]->mx += x->add;
x->child[1]->add += x->add;
}
x->add = 0;
}
}
void Rotate(Node *x, int dir) {
if (IsRoot(x))return;
Node * p = x->parent;
if (p->parent->child[0] == p)
{
p->parent->child[0] = x;
}
else if (p->parent->child[1] == p) {
p->parent->child[1] = x;
}
x->parent = p->parent;
p->child[dir ^ 1] = x->child[dir];
p->child[dir ^ 1]->parent = p;
x->child[dir] = p;
p->parent = x;
update(p);
update(x);
/*if (Root == p) {
Root = x;
}*/
}
void Splay(Node *x) {
static Node *sta[N];
int top = 0;
sta[top++] = x;
for (Node *y = x; !IsRoot(y); y = y->parent)
sta[top++] = y->parent;
while (top)down(sta[--top]);
while (!IsRoot(x)) {
if (IsRoot(x->parent)) {
if (x->parent->child[0] == x) {
Rotate(x, 1);
}
else {
Rotate(x, 0);
}
}
else if (x->parent->parent->child[0] == x->parent) {
if (x->parent->child[0] == x) {
Rotate(x->parent, 1);
Rotate(x, 1);
}
else {
Rotate(x, 0);
Rotate(x, 1);
}
}
else {
if (x->parent->child[1] == x) {
Rotate(x->parent, 0);
Rotate(x, 0);
}
else {
Rotate(x, 1);
Rotate(x, 0);
}
}
update(x);
}
}
Node *NewNode(Node *x, int val) {
x = new Node;
x->parent = nil;
x->value = val;
x->size = 1;
x->add = 0;
x->mx = val;
x->reverse = 0;
x->child[0] = x->child[1] = nil;
return x;
}
void dfs(Node *u) {
if (u->child[0] != nil)
dfs(u->child[0]);
printf(" %d", u->value);
if (u->child[1] != nil)
dfs(u->child[1]);
}
//void Insert(int u,int val){
// if (Root == nil) {
// Root = NewNode(nil, val);
// return;
// }
// Node *x=Root;
// while (x->child[val>=x->value]!=nil) {
// x = x->child[val>=x->value];
// }
// Node *t = NewNode(x,val);
// node[u] = t;
// x->child[val >= x->value] = t;
// update(x);
// Splay(t, nil);
///* printf("dfs:");
// dfs(Root);
// printf("\n");*/
//}
//void Remove(int val) {
// int rnk = FindValRank(val);
// FindRankTo(rnk - 1, nil);
// FindRankTo(rnk + 1, Root);
// Root->child[1]->child[0] = nil;
// update(Root->child[1]);
// update(Root);
//}
//int FindValRank(int val) {
// Node *x = Root;
// int sum = 0;
// while (x->child[val >= x->value]!=nil) {
// if (x->value == val)
// return sum + 1 + x->child[0]->size;
// if (val >= x->value) {
// sum += x->child[0]->size+1;
// }
// x = x->child[val >= x->value];
// }
// return sum+1+x->child[0]->size;
//}
//void FindRankTo(int rnk, Node *y) {
// Node *x = Root;
// while (x!=nil) {
// if (rnk == x->child[0]->size+1)
// {
// Splay(x, y);
// return;
// }
// if (rnk > x->child[0]->size) {
// rnk -= x->child[0]->size + 1;
// x = x->child[1];
// }
// else
// x = x->child[0];
// }
//}
//int FindRoot(int x) {
//}
/*int FindRankVal(int k) {
FindRankTo(k, nil);
return Root->value;
}*/
Node *Access(Node *x) {
Node *y = x;
x = nil;
while (y != nil) {
Splay(y);
//y->child[1]->parent = y;
y->child[1] = x;
x->parent = y;
update(y);
x = y;
y = y->parent;
}
return x;
}
//Node *FindRoot(Node *x) {
// Access(x);
// Splay(x);
// while (x->child[0] != nil)x = x->child[0];
// Splay(x);
// return x;
//}
void Cut(Node *x) {
//ChangeRoot(x->parent);
/*Access(x);
Splay(x);
x->reverse ^= 1;*/
ChangeRoot(x);
x->child[0]->parent = nil;
x->child[0] = nil;
update(x);
}
void Link(Node *x, Node *y) {
//printf("%d %d#\n", x->value, y->value);
/*Access(x);
Splay(x);
x->reverse ^= 1;*/
ChangeRoot(x);
x->parent = y;
//Access(x);
//
}
void ChangeRoot(Node *x) {
Access(x);
Splay(x);
x->reverse ^= 1;
}
void AddVal(Node *x, Node *y, int val) {
ChangeRoot(y);
ChangeRoot(x);
//Node *u = Access(x);
//Node *u = BecomeRoot(x);
/*Cut(y->child[1]);
while (y->child[0] != x)y = y->child[0];
Cut(x->child[0]);*/
//Access(x);
x->value += val;
x->add += val;
x->mx += val;
}
int QueryMaxVal(Node *x, Node *y) {
ChangeRoot(y);
ChangeRoot(x);
//Node *u = Access(x);
//BecomeRoot(x);
return x->mx;
/*Cut(y->child[1]);
while(y->child[0])*/
}
bool IsSameTree(Node *u, Node *v) {
while (u->parent != nil)u = u->parent;
while (v->parent != nil)v = v->parent;
return u == v;
}
public:
int w[N];
Node *Root, *nil;
};
struct Seg {
int l, r, k, index;
bool operator < (const Seg & rhs)const {
return r < rhs.r;
}
}seg[M];
Lct lct;
//SplayTree spt;
int ans[M];
void printErr() {
printf("-1\n");
}
int nd1[N], nd2[N];
int main() {
int n;
while (scanf("%d",&n)!=-1) {
lct.Init();
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
//g[u].push_back(v);
nd1[i] = u;
nd2[i] = v;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &lct.w[i]);
lct.node[i] = lct.NewNode(lct.node[i], lct.w[i]);
}
for (int i = 1; i < n; i++)
lct.Link(lct.node[nd1[i]], lct.node[nd2[i]]);
/*for (int i = 1; i <= n; i++)
for (int j = 0; j < g[i].size(); j++)
lct.Link(lct.node[i], lct.node[g[i][j]]);*/
//for (int i = 1; i <= n; i++) {
// if (lct.IsRoot(lct.node[i])) {
// printf("Root :%d", i);
// lct.dfs(lct.node[i]);
// printf("\n");
// }
//}
scanf("%d", &m);
while (m--) {
int op;
scanf("%d", &op);
if (op == 1) {
int x, y;
scanf("%d%d", &x, &y);
if (lct.IsSameTree(lct.node[x], lct.node[y])) {
printErr();
continue;
}
lct.Link(lct.node[x], lct.node[y]);
}
if (op == 2) {
int x, y;
scanf("%d%d", &x, &y);
if (x==y||!lct.IsSameTree(lct.node[x], lct.node[y])) {
printErr();
continue;
}
lct.ChangeRoot(lct.node[x]);
lct.Cut(lct.node[y]);
}
if (op == 3) {
int w, x, y;
scanf("%d%d%d", &w, &x, &y);
if (!lct.IsSameTree(lct.node[x], lct.node[y])) {
printErr();
continue;
}
lct.AddVal(lct.node[x], lct.node[y], w);
}
if (op == 4) {
int x, y;
scanf("%d%d", &x, &y);
if (!lct.IsSameTree(lct.node[x], lct.node[y])) {
printErr();
continue;
}
printf("%d\n", lct.QueryMaxVal(lct.node[x], lct.node[y]));
}
}
printf("\n");
}
return 0;
}
hdu 4010 Lct动态链接树的更多相关文章
- Lct 动态链接树
通过树链剖分能了解轻重边 Acdreamer 的博客 http://blog.csdn.net/acdreamers/article/details/10591443 然后看杨哲大大的论文,能了解轻重 ...
- Hdu 2475-Box LCT,动态树
Box Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- BZOJ 3510 - 首都 「 $LCT$ 动态维护树的重心」
这题 FlashHu 的优化思路值得借鉴 前置引理 树中所有点到某个点的距离和中,到重心的距离和是最小的. 把两棵树通过某一点相连得到一颗新的树,新的树的重心必然在连接原来两棵树重心的路径上. 一棵树 ...
- 【BZOJ4025】二分图(LCT动态维护图连通性)
点此看题面 大致题意: 给你一张图以及每条边的出现时间和消失时间,让你求每个时间段这张图是否是二分图. 二分图性质 二分图有一个比较简单的性质,即二分图中不存在奇环. 于是题目就变成了:让你求每个时间 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- LuoguP2617 Dynamic Rankings (动态主席树学习理解)
题目地址 题目链接 题解 动态主席树的板子题.动态主席树其实和静态的有很大差别,虽然同样是n个根,但是节点并不能共用,每个根节点表示bit上的一段区间. 所以其实是个树套树的东西来着,外层是bit,内 ...
- HDU1199 动态线段树 // 离散化
附动态线段树AC代码 http://acm.hdu.edu.cn/showproblem.php?pid=1199 因为昨天做了一道动态线段树的缘故,今天遇到了这题没有限制范围的题就自然而然想到了动态 ...
- HDU5367 思维map // 动态线段树
地主毛毛有n座山,这些山在地主家门前排成一条直线.这些山一开始均有相同的高度. 每一天,毛毛都会要求花花开挖机把几座山挖掉一定高度,或者给一些山堆上一些高度.并且要求花花报告现在有多少座山属于“高山 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
随机推荐
- 一站式搞定Ubuntu共享环境配置
1. 添加linux用户 安装的开发用的虚拟机,一般不直接使用root账户,会新建一个普通用户,然后在/etc/sudoers添加上sudo的权限即可. 使用如下命令: sudo adduser -- ...
- SpringBoot整合Shiro自定义Redis存储
Shiro Shiro 主要分为 安全认证 和 接口授权 两个部分,其中的核心组件为 Subject. SecurityManager. Realms,公共部分 Shiro 都已经为我们封装好了,我们 ...
- Java 集合框架综述,这篇让你吃透!
一.集合框架图 简化图: 说明:对于以上的框架图有如下几点说明 1.所有集合类都位于java.util包下.Java的集合类主要由两个接口派生而出:Collection和Map,Collection和 ...
- 从udaf谈flink的state
1.前言 本文主要基于实践过程中遇到的一系列问题,来详细说明Flink的状态后端是什么样的执行机制,以理解自定义函数应该怎么写比较合理,避免踩坑. 内容是基于Flink SQL的使用,主要说明自定义聚 ...
- [Hadoop] mapper数量的控制
确定map任务数时依次优先参考如下几个原则: 1) 每个map任务使用的内存不超过800M,尽量在500M以下 比如处理256MB数据需要的时间为10分钟,内存为800MB,此时如果处理12 ...
- 简述 jpg png gif png-8 png-24 的区别
Gif 格式的特点: 1.透明性,GIF是一种布尔透明类型,既可以是全透明,又可以是不透明,但是不可以半透明: 2.动画 GIF支持动画. 3.无损耗性 GIF是一种无损耗的图像格式,也 ...
- JVM的方法执行引擎-模板表
Java的模板解析执行需要模板表与转发表的支持,而这2个表中的数据在HotSpot虚拟机启动时就会初始化.这一篇首先介绍模板表. 在启动虚拟机阶段会调用init_globals()方法初始化全局模块, ...
- golang []byte 和 string相互转换
原文链接:golang []byte和string相互转换 测试例子 package main import ( "fmt" ) func main() { str2 := &qu ...
- CentOS7 更改默认启动桌面(或命令行)模式
centos7以后是这样的,7以前就是别的版本了 1.systemctl get-default命令获取当前模式 2.systemctl set-default graphical.target 修改 ...
- python编译错误ValueError: Complex data not supported
今天在用python跑一个k-means算法与谱聚类算法对比的程序时,谱聚类的图不能出来,编译报错 后来,多亏了这位GitHub用户,找到了解决办法,是因为在运算过程中出现了复数,因此要进行强制转换 ...