BZOJ1180 [CROATIAN2009]OTOCI LCT
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ1180
本题和BZOJ2843一样。
BZOJ2843 极地旅行社 LCT
题意概括
题解
本题仍然是LCT模版题。
bridge:先判断A和B是否联通,然后连边即可。
penguins:直接把A搞到付诸树根,然后修改就可以了。
excursion:先判断是否联通,然后如果不联通,那么我们走类似于cut的过程,让a成为b的左子节点且为最左子节点,那么答案就是size[a]+val[b]
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=30005;
int n,m;
int fa[N],son[N][2],size[N],rev[N],val[N];
bool isroot(int x){
return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
void pushup(int x){
size[x]=val[x]+size[son[x][0]]+size[son[x][1]];
}
void pushdown(int x){
if (rev[x]){
rev[x]=0;
rev[son[x][0]]^=1;
rev[son[x][1]]^=1;
swap(son[x][0],son[x][1]);
}
}
void pushadd(int x){
if (!isroot(x))
pushadd(fa[x]);
pushdown(x);
}
int wson(int x){
return son[fa[x]][1]==x;
}
void rotate(int x){
if (isroot(x))
return;
int y=fa[x],z=fa[y],L=wson(x),R=L^1;
if (!isroot(y))
son[z][wson(y)]=x;
fa[x]=z,fa[y]=x,fa[son[x][R]]=y;
son[y][L]=son[x][R],son[x][R]=y;
pushup(y);
pushup(x);
}
void splay(int x){
pushadd(x);
for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])
if (!isroot(y))
rotate(wson(x)==wson(y)?y:x);
}
void access(int x){
int t=0;
while (x){
splay(x);
son[x][1]=t;
pushup(x);
t=x;
x=fa[x];
}
}
void rever(int x){
access(x);
splay(x);
rev[x]^=1;
}
void link(int x,int y){
rever(x);
fa[x]=y;
}
void cut(int x,int y){
rever(x);
access(y);
splay(y);
fa[x]=son[y][0]=0;
}
int find(int x){
access(x);
splay(x);
while (1){
pushdown(x);
if (son[x][0])
x=son[x][0];
else
break;
}
return x;
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%d",&val[i]);
size[i]=val[i],son[i][0]=son[i][1]=fa[i]=rev[i]=0;
}
scanf("%d",&m);
while (m--){
char op[10];
int a,b;
scanf("%s%d%d",op,&a,&b);
if (op[0]=='b'){
if (find(a)==find(b))
puts("no");
else
puts("yes"),link(a,b);
}
else if (op[0]=='p'){
rever(a);
val[a]=b;
pushup(a);
}
else {
if (find(a)==find(b)){
rever(a);
access(b);
splay(b);
printf("%d\n",size[son[b][0]]+val[b]);
}
else
puts("impossible");
}
}
return 0;
}
BZOJ1180 [CROATIAN2009]OTOCI LCT的更多相关文章
- BZOJ2843极地旅行社&BZOJ1180[CROATIAN2009]OTOCI——LCT
题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出“no”.否则输出“yes”,并且在 ...
- BZOJ 1180: [CROATIAN2009]OTOCI [LCT]
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 961 Solved: 594[Submit][S ...
- 【bzoj1180】[CROATIAN2009]OTOCI LCT
题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes”,并且在结点 ...
- BZOJ1180 [CROATIAN2009]OTOCI 【LCT】
题目 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出"no".否则输出&qu ...
- BZOJ1180: [CROATIAN2009]OTOCI
传送门 一遍AC,开心! $Link-Cut-Tree$最后一题 //BZOJ 1180 //by Cydiater //2016.9.18 #include <iostream> #in ...
- 1180: [CROATIAN2009]OTOCI(LCT)
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 1200 Solved: 747[Submit][ ...
- [bzoj1180][CROATIAN2009]OTOCI_LCT
OTOCI bzoj-1180 CROATIAN-2009 题目大意:给你n个离散的点,m个操作.支持:两点加边(保证还是森林),修改单点权值,询问两点是否联通,查询联通两点之间路径权值. 注释:$1 ...
- BZOJ_1180_[CROATIAN2009]_OTOCI_(LCT)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1180 三种操作: 1.询问x,y是否连通,如果不连通,建一条边x,y 2.把x节点的权值改为t ...
- BZOJ 1180: [CROATIAN2009]OTOCI
1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 989 Solved: 611[Submit][S ...
随机推荐
- Python排序算法之选择排序
选择排序 选择排序比较好理解,好像是在一堆大小不一的球中进行选择(以从小到大,先选最小球为例): 1. 选择一个基准球 2. 将基准球和余下的球进行一一比较,如果比基准球小,则进行交换 3. 第一轮过 ...
- MyBatis编写映射文件实现增删改操作 附说明及代码
1.看一下我们接口 package cn.bdqn.mybatis.dao; import org.apache.ibatis.annotations.Select; import cn.bdqn.m ...
- Tukey‘s test方法 异常值
如何计算异常值 异常值就是和其他样本数据有显著差异的值.这个词在统计学中经常用到,可以表示数据异常或测量错误.明白算异常值的方法,对于正确理解数据非常有用,而且会引出更精确的结论.以下介绍一个很简单的 ...
- Linux 重启网卡失败 Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
linux下重启网卡使用命令 : service network restart 时报错: [root@slave01 hadoop]# service network restart Startin ...
- js对当前时间的相关操作
链接:https://www.cnblogs.com/visi_zhangyang/p/3490122.html js中获得当前时间是年份和月份,形如:201208 //获取完整的日期 v ...
- vue学习之用 Vue.js + Vue Router 创建单页应用的几个步骤
通过vue学习一:新建或打开vue项目,创建好项目后,接下来的操作为: src目录重新规划——>新建几个页面——>配置这几个页面的路由——>给根实例注入路由配置 src目录重整 在项 ...
- pythonic语法
b="$".join(str(x) for x in range(10)) a= 2 if 5<2 else 3 print (a)#a是3
- spring cloud 学习
on going... 微服务势在必行,要开始学点相关的东西了,fighting!!! 注册中心 网关 负载均衡 限流 等等.
- ViewPager制作APP引导页+若干动画效果
ViewPager使用FragmentStatePagerAdapter做Adapter,引导页使用多Fragment形式. 见http://www.cnblogs.com/bmbh/p/567276 ...
- Java 二进制I/O处理
在Java中如何处理文本I/O 使用Scanner类读取文本数据,使用PrintWriter类写文本数据 例子: public class IO { public static void main(S ...