题目1:BZOJ 2049 洞穴勘测

 #include <bits/stdc++.h>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; const int oo = 0x3f3f3f3f; struct SplayTree{
static const int N = + ; int top, st[N];
int fa[N], c[N][], sum[N], mx[N], val[N];
bool rev[N]; bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(x));
}
} void pushup(int x){
int l = L(x), r = R(x); sum[x] = sum[l] + sum[r] + val[x];
mx[x] = max(val[x], max(mx[l], mx[r]));
} void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ;
if(!isroot(y))
c[z][(L(z) == y) ^ ] = x;
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = y;
} 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], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{ void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x; x = Splay.fa[x];
}
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[x] = ;
} int findroot(int x){
Access(x); Splay.splay(x);
int y = x;
while(Splay.L(y)) y = Splay.L(y); return y;
}
}lct; int n, m, x, y;
char str[]; int main(){
scanf("%d%d", &n, &m);
for(int i = ; i <= m; ++ i){
scanf("%s%d%d", str, &x, &y); if(str[] == 'C') lct.link(x, y);
else if(str[] == 'D') lct.cut(x, y);
else{
if(lct.findroot(x) == lct.findroot(y)) printf("Yes\n");
else printf("No\n");
}
} return ;
}

BZOJ 2049

题目2: BZOJ 1180 OTOCI

 #include <bits/stdc++.h>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; typedef long long ll;
const int N = + ;
const int oo = 0x3f3f3f3f; struct SplayTree{
int fa[N], c[N][];
ll val[N], mx[N], sum[N];
bool rev[N];
int top, st[N]; inline bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(x));
}
} void pushup(int x){
int l = L(x), r = R(x); sum[x] = sum[l] + sum[r] + val[x];
mx[x] = max(val[x], max(mx[l], mx[r]));
} void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ;
if(!isroot(y)){
c[z][(L(z) == y) ^ ] = x;
}
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = 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], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{
void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x;
Splay.pushup(x);
x = Splay.fa[x];
}
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[x] = ;
} int findroot(int x){
Access(x); Splay.splay(x); int y = x; while(Splay.L(y)){
y = Splay.L(y);
} return y;
}
}lct; int n, m, x, y;
char str[]; int main(){
scanf("%d", &n);
for(int i = ; i <= n; ++ i){
scanf("%lld", &Splay.val[i]);
Splay.sum[i] = Splay.val[i];
}
scanf("%d", &m);
for(int i = ; i <= m; ++ i){
scanf("%s%d%d", str, &x, &y); if(str[] == 'b'){
if(lct.findroot(x) == lct.findroot(y))
puts("no");
else{
puts("yes"); lct.link(x, y);
}
}
else if(str[] == 'e'){
if(lct.findroot(x) != lct.findroot(y)){
puts("impossible");
}
else{
lct.makeroot(x); lct.Access(y); Splay.splay(y);
printf("%lld\n", Splay.sum[y]);
}
}
else{
lct.makeroot(x); Splay.val[x] = y; Splay.pushup(x);
}
} return ;
}

BZOJ 1180

题目3:BZOJ 3282 TREE

 #include <bits/stdc++.h>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; const int N = + ; struct SplayTree{
int fa[N], c[N][], val[N], sum[N];
bool rev[N];
int top, st[N]; inline bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushup(int x){
int l = L(x), r = R(x); sum[x] = sum[l] ^ sum[r] ^ val[x];
}
void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(x));
}
}
void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ; if(!isroot(y)){
c[z][(L(z) == y) ^ ] = x;
}
fa[y] = x; fa[x] = z; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = 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], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{
void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x;
Splay.pushup(x);
x = Splay.fa[x];
}
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} int findroot(int x){
Access(x); Splay.splay(x); int y = x; while(Splay.L(y)) y = Splay.L(y); return y;
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[x] = ;
}
}lct; int n, m;
int x, y, z; int main(){
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++ i){
scanf("%d", &Splay.val[i]);
Splay.sum[i] = Splay.val[i];
}
for(int i = ; i <= m; ++ i){
scanf("%d%d%d", &z, &x, &y); switch(z){
case :{
lct.makeroot(x); lct.Access(y); Splay.splay(y);
printf("%d\n", Splay.sum[y]);
break;
}
case :{
if(lct.findroot(x) != lct.findroot(y)){
lct.link(x, y);
}
break;
}
case :{
if(lct.findroot(x) == lct.findroot(y)){
lct.cut(x, y);
}
break;
}
case :{
lct.Access(x); Splay.splay(x); Splay.val[x] = y;
Splay.pushup(x);
break;
}
}
} return ;
}

BZOJ 3282

题目4: HDU 4010 Query On The Trees

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#define L(x) c[x][0]
#define R(x) c[x][1] using namespace std; const int N = + ;
const int oo = 0x3f3f3f3f; int n, m, tot;
int first[N], nxt[N<<];
int u[N<<], v[N<<];
int que[N<<]; struct SplayTree{
int fa[N], c[N][], val[N], mx[N], add[N];
bool rev[N];
int top, st[N]; inline bool isroot(int x){
return L(fa[x]) != x && R(fa[x]) != x;
} void pushup(int x){
int l = L(x), r = R(x); mx[x] = max(mx[l], mx[r]);
mx[x] = max(mx[x], val[x]);
} void pushdown(int x){
int l = L(x), r = R(x); if(rev[x]){
rev[x] ^= ; rev[l] ^= ; rev[r] ^= ;
swap(L(x), R(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] = ;
}
} void rotate(int x){
int y = fa[x], z = fa[y], l, r; l = (L(y) == x) ^ ; r = l ^ ;
if(!isroot(y)){
c[z][L(z) == y ^ ] = x;
}
fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
c[y][l] = c[x][r]; c[x][r] = 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], z = fa[y]; if(!isroot(y)){
if(L(y) == x ^ L(z) == y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
}Splay; struct LinkCutTree{
void Access(int x){
int t = ; while(x){
Splay.splay(x);
Splay.R(x) = t;
t = x;
Splay.pushup(x);
x = Splay.fa[x];
}
}
void Add(int x, int y, int w){
makeroot(x); Access(y); Splay.splay(y);
Splay.add[y] += w; Splay.val[y] += w;
Splay.mx[y] += w;
}
int findroot(int x){
Access(x); Splay.splay(x); int y = x; while(Splay.L(y)){
y = Splay.L(y);
} return y;
} void makeroot(int x){
Access(x); Splay.splay(x); Splay.rev[x] ^= ;
} void cut(int x, int y){
makeroot(x); Access(y); Splay.splay(y);
Splay.L(y) = Splay.fa[Splay.L(y)] = ; Splay.pushup(y);
} void link(int x, int y){
makeroot(x); Splay.fa[x] = y; Splay.splay(x);
}
}lct; void insert(int s, int t){
++ tot;
u[tot] = s; v[tot] = t;
nxt[tot] = first[s];
first[s] = tot;
} void bfs(){
int head, tail;
head = tail = ;
que[head] = ;
Splay.fa[] = ; while(head <= tail){
int x = que[head]; for(int i = first[x]; i; i = nxt[i]){
if(v[i] != Splay.fa[x]){
Splay.fa[v[i]] = x;
que[++ tail] = v[i];
}
}
++ head;
}
} int main(){
int flag, x, y, z; while(~scanf("%d", &n)){
tot = ;
for(int i = ; i <= n; ++ i){
Splay.fa[i] = Splay.val[i] = Splay.add[i] = ;
Splay.rev[i] = false;
Splay.mx[i] = -oo;
Splay.c[i][] = Splay.c[i][] = ;
first[i] = ;
}
for(int i = ; i < n; ++ i){
scanf("%d%d", &x, &y);
insert(x, y); insert(y, x);
}
for(int i = ; i <= n; ++ i){
scanf("%d", &x);
Splay.val[i] = Splay.mx[i] = x;
}
bfs(); scanf("%d", &m);
for(int i = ; i <= m; ++ i){
scanf("%d", &flag);
switch(flag){
case :{
scanf("%d%d", &x, &y);
if(lct.findroot(x) == lct.findroot(y))
puts("-1");
else
lct.link(x, y);
break;
}
case :{
scanf("%d%d", &x, &y);
if(lct.findroot(x) != lct.findroot(y) || x == y)
puts("-1");
else lct.cut(x, y);
break;
}
case :{
scanf("%d%d%d", &z, &x, &y);
if(lct.findroot(x) != lct.findroot(y))
puts("-1");
else
lct.Add(x, y, z);
break;
}
case :{
scanf("%d%d", &x, &y);
if(lct.findroot(x) != lct.findroot(y))
puts("-1");
else{
lct.makeroot(x); lct.Access(y); Splay.splay(y);
printf("%d\n", Splay.mx[y]);
}
break;
}
}
}
puts("");
} return ;
}

HDU 4010

Link-Cut-Tree题目泛做(为了对应自己的课件)的更多相关文章

  1. K-D Tree题目泛做(CXJ第二轮)

    题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...

  2. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  3. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  4. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  5. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  6. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  7. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  8. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

  9. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  10. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

    一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...

随机推荐

  1. zsh-替换掉黑白的控制台

    官方地址:里面有详细的安装指南 http://ohmyz.sh/

  2. OpenGL ES 2.0 纹理映射

    纹理坐标用符点数表示,范围一般从0.0到1.0,在纹理坐标系中.纹理坐标系原点在左上侧,向右为S轴,向下为T轴.两个轴的取值范围都是0.0-1.0. 纹理映射 纹理映射:把一幅纹理图应用到相应的几何图 ...

  3. SMA2SATA、PCIe2SATA转换模块(也有叫:Sata Test Fixtures)

    SMA2SATA.PCIe2SATA测试夹具(Sata Test Fixtures) 去年制作SMA2SATA.PCIe2SATA适配器的过程早就想写出来,但一直没有时间,今天星期六有个空儿,简单整理 ...

  4. hdu5348 MZL's endless loop(欧拉回路)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's endless loop Time Limit: 3000/1500 ...

  5. utf-8的mysql表笔记

    链接数据库指定编码集jdbc:mysql://192.168.2.33:3306/mybase?useUnicode=true&characterEncoding=UTF-8 mysql默认链 ...

  6. jquery 实现 隐藏交替同时记住以前隐藏的样式

    /* * control menu show or hide(expand and collapse) */ var status = []; function menuOperation() { $ ...

  7. 在js中使用json

    在js中使用json var obj = {     "1" : "value1",     "2" : "value2" ...

  8. 列表:一个打了激素的数组 - 零基础入门学习Python010

    列表:一个打了激素的数组 让编程改变世界 Change the world by program 列表:一个打了激素的数组 有时候我们需要把一堆东西暂时存储起来,因为他们有某种直接或者间接的联系,我们 ...

  9. Lua绑定C++类

    原文:http://blog.csdn.net/chenee543216/article/details/12074771 以下是代码: Animal.h文件 #pragma once #ifndef ...

  10. sublime 插件 和free 注册码

    代码对齐: Alignment html代码补全:  Emmet CoffeeScript语法:  Better CoffeeScript css格式化:  CSS Format less语法:  L ...