[洛谷P4312][COCI 2009] OTOCI / 极地旅行社
题目大意:有$n(n\leqslant3\times10^4)$个点,每个点有点权,$m(m\leqslant3\times10^5)$个操作,操作分三种:
- $bridge\;x\;y:$询问节点$x$与节点$y$是否连通,若不连通则连一条边
- $penguins\;x\;y:$把节点$x$点权改为$y$
- $excursion\;x\;y:$询问$x->y$路径上点权和
题解:$LCT$直接维护即可
卡点:无
C++ Code:
#include <algorithm>
#include <cstdio>
#define maxn 30010
#define lc(rt) son[rt][0]
#define rc(rt) son[rt][1] int son[maxn][2], fa[maxn], tg[maxn];
int w[maxn], S[maxn];
inline bool get(int rt, int tg = 1) { return son[fa[rt]][tg] == rt; }
inline bool is_root(int rt) { return !(get(rt, 0) || get(rt)); }
inline void update(int rt) {
S[rt] = S[lc(rt)] + S[rc(rt)] + w[rt];
}
inline void pushdown(int rt) {
if (tg[rt]) {
tg[rt] ^= 1, tg[lc(rt)] ^= 1, tg[rc(rt)] ^= 1;
std::swap(lc(rt), rc(rt));
}
}
inline void rotate(int x) {
int y = fa[x], z = fa[y], b = get(x);
if (!is_root(y)) son[z][get(y)] = x;
fa[son[y][b] = son[x][!b]] = y, son[x][!b] = y;
fa[y] = x, fa[x] = z;
update(y), update(x);
}
inline void splay(int x) {
static int S[maxn], top;
S[top = 1] = x;
for (int y = x; !is_root(y); S[++top] = y = fa[y]) ;
for (; top; top--) pushdown(S[top]);
for (; !is_root(x); rotate(x)) if (!is_root(fa[x]))
get(x) ^ get(fa[x]) ? rotate(x) : rotate(fa[x]);
update(x);
}
inline void access(int x) { for (int t = 0; x; rc(x) = t, t = x, x = fa[x]) splay(x); }
inline void make_root(int rt) { access(rt), splay(rt), tg[rt] ^= 1; }
inline void link(int x, int y) { make_root(x), fa[x] = y; }
inline void split(int x, int y) { make_root(x), access(y), splay(y); }
inline void cut(int x, int y) { split(x, y), lc(y) = fa[x] = 0; }
inline int findroot(int x) {
access(x), splay(x);
while (lc(x)) x = lc(x), pushdown(x);
splay(x);
return x;
}
inline bool connected(int x, int y) {
split(x, y);
return x == findroot(y);
} int n, m;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", w + i);
S[i] = w[i];
}
scanf("%d", &m);
while (m --> 0) {
char op[15];
int x, y;
scanf("%s%d%d", op, &x, &y);
switch (*op) {
case 'b':
if (connected(x, y)) puts("no");
else {
puts("yes");
link(x, y);
}
break;
case 'p':
make_root(x);
w[x] = y;
update(x);
break;
case 'e':
if (connected(x, y)) {
split(x, y);
printf("%d\n", S[y]);
} else puts("impossible");
}
}
return 0;
}
[洛谷P4312][COCI 2009] OTOCI / 极地旅行社的更多相关文章
- 洛谷P4312 [COCI 2009] OTOCI / 极地旅行社(link-cut-tree)
题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务. 当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mi ...
- [luogu]P4312 [COCI 2009] OTOCI / 极地旅行社(LCT)
P4312 [COCI 2009] OTOCI / 极地旅行社 题目描述 不久之前,Mirko建立了一个旅行社,名叫"极地之梦".这家旅行社在北极附近购买了N座冰岛,并且提供观光服 ...
- P4312 [COCI 2009] OTOCI / 极地旅行社
思路 LCT维护和的板子 注意findroot的时候要先access一下,修改点权之前要先splay到根 代码 #include <cstdio> #include <algorit ...
- 【题解】 Luogu P4312 / SP4155 [COCI 2009] OTOCI / 极地旅行社
原题地址:P4312 [COCI 2009] OTOCI / 极地旅行社/SP4155 OTOCI - OTOCI lct入门难度的题,十分弱智(小蒟蒻说lct是什么,能吃吗?) bridge操作判联 ...
- BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)
emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...
- [洛谷P5190][COCI 2010] PROGRAM
题目大意:给你$k(k\leqslant10^6)$个数,$f(x)$表示$x$的约数在$k$个数中出现的次数,在这任何数都是$0$的约数.$m(m\leqslant10^6)$次询问,每次给出$l, ...
- [洛谷 P4612][COCI 2011-2012#7] Setnja
传送门 TM :setnja (1S256M) 一个人要散步去会见他的 N 个朋友(按给定的顺序会见).我们可以理解成他们都住在一个 很大的网格内,每个朋友住其中的一个单元格,所有人每一步都可以走到相 ...
- 洛谷 P2055 [ ZJOI 2009 ] 假期的宿舍 —— 二分图匹配
题目:https://www.luogu.org/problemnew/show/P2055 二分图匹配: 注意要连边的话对方必须有床! 代码如下: #include<iostream> ...
- 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 323 Solved: 218[Submit][Status][Discuss ...
随机推荐
- python的阶段复习
1.ABCD乘于9 = DCBA,求ABCD的值,且ABCD均互不相等 #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time :2017/12/26 ...
- 一文带你了解 Raft 一致性协议的关键点
此文已由作者孙建良授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Raft 协议的发布,对分布式行业是一大福音,虽然在核心协议上基本都是师继 Paxos 祖师爷(lampor ...
- windows环境下jmeter生成测试报告
1.要求 jmeter需要在3.0版本以上 jdk1.7以上 需要准备脚本文件,即jmx文件 2.进入cmd界面 3.进入jmeter的bin目录 cd:\xxxx\apache-jmeter-4.0 ...
- Selenium(Python)等待元素出现
1.显式等待 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdri ...
- 【转】UTF8字符串转换为汉字 c#,转自游戏开发主席
using System; /// <summary> /// UTF8字符串转换为汉字用的类 /// 转换如"\\u8d35"之类的字符串为对应的汉字 /// < ...
- 【Python 开发】第一篇:计算机基础
一.计算机基础 首先Python是一门编程语言 语言: 那什么是语言? 语言就是一种事物与另一种事物沟通的介质.所以说编程语言是程序员跟计算机沟通的介质. 什么是编程: 准确来说就是程序员用计算机所能 ...
- 【zabbix 监控】第一章 zabbix的安装配置
安装前准备 一.下载网络yum源: http://mirrors.163.com/.help/centos.html https://opsx.alibaba.com/mirror 1.首先备份/et ...
- linux c语言 fork() 和 exec 函数的简介和用法
linux c语言 fork() 和 exec 函数的简介和用法 假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<std ...
- Machine Learning分类:监督/无监督学习
从宏观方面,机器学习可以从不同角度来分类 是否在人类的干预/监督下训练.(supervised,unsupervised,semisupervised 以及 Reinforcement Learnin ...
- 重装win10后ubuntu引导项修复
问题描述:原来是在win7下装了ubuntu14的双系统,后台win7换win10,然后使用EasyBCD进行引导项修复时,不好使,报 error file: /boot/grub/i386-pc/n ...