题目大意:有$n(n\leqslant3\times10^4)$个点,每个点有点权,$m(m\leqslant3\times10^5)$个操作,操作分三种:

  1. $bridge\;x\;y:$询问节点$x$与节点$y$是否连通,若不连通则连一条边
  2. $penguins\;x\;y:$把节点$x$点权改为$y$
  3. $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 / 极地旅行社的更多相关文章

  1. 洛谷P4312 [COCI 2009] OTOCI / 极地旅行社(link-cut-tree)

    题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务. 当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mi ...

  2. [luogu]P4312 [COCI 2009] OTOCI / 极地旅行社(LCT)

    P4312 [COCI 2009] OTOCI / 极地旅行社 题目描述 不久之前,Mirko建立了一个旅行社,名叫"极地之梦".这家旅行社在北极附近购买了N座冰岛,并且提供观光服 ...

  3. P4312 [COCI 2009] OTOCI / 极地旅行社

    思路 LCT维护和的板子 注意findroot的时候要先access一下,修改点权之前要先splay到根 代码 #include <cstdio> #include <algorit ...

  4. 【题解】 Luogu P4312 / SP4155 [COCI 2009] OTOCI / 极地旅行社

    原题地址:P4312 [COCI 2009] OTOCI / 极地旅行社/SP4155 OTOCI - OTOCI lct入门难度的题,十分弱智(小蒟蒻说lct是什么,能吃吗?) bridge操作判联 ...

  5. BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)

    emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...

  6. [洛谷P5190][COCI 2010] PROGRAM

    题目大意:给你$k(k\leqslant10^6)$个数,$f(x)$表示$x$的约数在$k$个数中出现的次数,在这任何数都是$0$的约数.$m(m\leqslant10^6)$次询问,每次给出$l, ...

  7. [洛谷 P4612][COCI 2011-2012#7] Setnja

    传送门 TM :setnja (1S256M) 一个人要散步去会见他的 N 个朋友(按给定的顺序会见).我们可以理解成他们都住在一个 很大的网格内,每个朋友住其中的一个单元格,所有人每一步都可以走到相 ...

  8. 洛谷 P2055 [ ZJOI 2009 ] 假期的宿舍 —— 二分图匹配

    题目:https://www.luogu.org/problemnew/show/P2055 二分图匹配: 注意要连边的话对方必须有床! 代码如下: #include<iostream> ...

  9. 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree

    2843: 极地旅行社 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 323  Solved: 218[Submit][Status][Discuss ...

随机推荐

  1. IO 转换流

    package TestIo; import java.io.*; /** * 转换流 */ public class TestConvertStream { public static void m ...

  2. SpringBoot学习:整合shiro(rememberMe记住我后自动登录session失效解决办法)

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 定义一个拦截器,判断用户是通过记住我登录时,查询数据库后台自动登录,同时把用户放入ses ...

  3. nodeJs 安装 npm nodeModules package.json

    Nodejs   1.安装nodejs 从nodejs官网下载最新版本的node,设置环境变量这样就可以在cmd下直接用命令行操作npm 环境变量:path  d:/nodejs 查看本机node及n ...

  4. WEB中间件漏洞--IIS

    1.iis安装 版本 一直下一步,选上iis安装 端口修改 网站目录 通过网站发布目录(发布目录任意),新建index.html页面,可以正常访问 2.iis6.0解析漏洞 (1)文件名解析 IIS在 ...

  5. go通过第三方库 mahonia gbk 转utf8

    go get github.com/axgle/mahonia dec := mahonia.NewDecoder("GBK")ret:=dec.ConvertString(res ...

  6. [Clr via C#读书笔记]Cp13接口

    Cp13接口 类和接口继承 接口只提供签名,不提供实现:等效于契约:凡事能使用具名接口的地方都能够使用实现了的接口. 定义接口 定义很简单,FCL也提供了大量的现成接口供使用: 继承接口 类不能多继承 ...

  7. Click Once使用总结

    做了一个CS结构软件,有十几个用户使用的客户端,因为刚开始试用期间,要不断根据用户使用情况修正问题和添加新功能,所以频繁更新是不可避免的,暂时没有深入去研究软件更新,暂时采取的方式是用户通过FTP自行 ...

  8. day-18 滑动平均模型测试样例

    为了使训练模型在测试数据上有更好的效果,可以引入一种新的方法:滑动平均模型.通过维护一个影子变量,来代替最终训练参数,进行训练模型的验证. 在tensorflow中提供了ExponentialMovi ...

  9. node.js应用--转载

    最近,在向大学生们介绍 HTML5 的时候,我想要对他们进行问卷调查,并向他们显示实时更新的投票结果.鉴于此目的,我决定快速构建一个用于此目的的问卷调查应用程序.我想要一个简单的架构,不需要太多不同的 ...

  10. Struts2中Action各种转发类型

    Struts2:Action中result的各种转发类型: 内部请求转发dispatcher(默认值) redirect.redirectAction.plainText1.redirect是重定向到 ...