题目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. table超过30个字段如何处理呢? bootstrap

    样式: @media (max-width: 768px) { .table-supplier { width: 100%; height: 100%; margin-bottom: 12.75px; ...

  2. angularjs字符串插值($interpolate)

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...

  3. Http Clinet使用

    Http Client是个apache下的一个开源包,用于使用http协议访问服务的java代码编写. Http Client的主要功能: (1)实现了所有 HTTP 的方法(GET,POST,PUT ...

  4. NSDate简单的使用

    NSDateFormatter *dateFormtter=[[NSDateFormatter alloc] init]; [dateFormtter setDateFormat:@"yyy ...

  5. JAVA语言对比C++语言的几个优点和自身的关键特性

    之所以写这篇博客不是为了比较JAVA语言和C++语言到底孰优孰劣,而是从语言本身出发进行对比总结程序编码设计上的差别.因水平有限,某些内容可能太过片面,欢迎指正和讨论. JAVA语言在设计上很好地借鉴 ...

  6. secure_file_priv 配置项对数据导入导出的影响

    secure_file_priv  mysqld 用这个配置项来完成对数据导入导出的限制. 例如我们可以通过 select * from tempdb.t into outfile '/home/my ...

  7. linux中查找命令find、locate、whereis、which、type区别

    linux中查找命令find.locate.whereis.which.type区别 1. find Java代码 find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件.与查询数据库(/ ...

  8. YII CDbCriteria总结

    $criteria = new CDbCriteria; //$criteria->alias = 't';  //查询该表 $criteria->addInCondition('id', ...

  9. BZOJ 1061 志愿者招募

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 思路:可以用不等式的改装变成费用流. 将不等式列出,如果有负的常数,那么就从等式连向T,如果是 ...

  10. IC封装形式COF介绍

    其实这个真不太懂,没有太多接触也没有比较好的资料,只能简单的了解一下了. 什么是卷带式覆晶薄膜封装 COF(Chip on film) COF是一种 IC 封装技术,是运用软性基板电路(flexibl ...