[洛谷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 ...
随机推荐
- VINS(九)Ceres Solver优化(未完待续)
使用Ceres Solver库处理后端优化问题,首先系统的优化函数为
- Visual studio 2010 TFS地址解析,让团队资源管理器不再显示IP地址
第一步: 找到名为hosts的配置文件(路径C:\Windows\System32\drivers\etc\hosts)用记事本打开并写入需要的配置,例如我用到的是TFS服务器的IP地址为192.16 ...
- springboot之RMI的使用
1.RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方法调用的 ...
- Linux下查看CPU信息[/proc/cpuinfo]
最近在研究linux系统负载的时候,接触到一些关于CPU信息查看的知识,和大家分享一下.通过对/proc/cpuinfo文件中的参数的分析,也学到了不少东西. 在linux操作系统中,CPU的信息在启 ...
- 「Python」conda与pip升级所有的包
conda: conda update --a pip: pip freeze --local | grep -v '^-e' | cut -d = -f 1 | xargs -n1 sudo pip ...
- apache+php+mysql开发环境搭建
一.Apache 因为Apache官网只提供源代码,如果要使用必须得自己编译,这里我选择第三方安装包Apache Lounge. 进入Apachelounge官方下载地址:http://w ...
- Ubuntu目录与权限
Ubuntu目录 / /bin /sbin /boot /etc /mnt /home d :directory - :file b :block 磁盘以块进行 l :link Ubuntu权限 U ...
- CSP201703-1:分蛋糕
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- 珍珠 Median Weight Bead 977
描述 There are N beads which of the same shape and size, but with different weights. N is an odd numbe ...
- Period :KMP
I - Period Problem Description For each prefix of a given string S with N characters (each character ...