【题解】 Luogu P4312 / SP4155 [COCI 2009] OTOCI / 极地旅行社
原题地址:P4312 [COCI 2009] OTOCI / 极地旅行社/SP4155 OTOCI - OTOCI
lct入门难度的题,十分弱智(小蒟蒻说lct是什么,能吃吗?)
bridge操作判联通,用find,不同的话link一下
penguins修改点权,把这个点旋转到树根暴力修改并pushup
excursion先判联通,如果联通输出点权之和
操作就是这样,简单有点像P2147 [SDOI2008]洞穴勘测
代码:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define N 300005
using namespace std;
inline int read()
{
int f=1,x=0;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
return f*x;
}
inline void Swap(register int &a,register int &b)
{
a^=b^=a^=b;
}
int n,m,v[N];
struct Link_cut_tree{
int top,c[N][2],fa[N],q[N],rev[N],sum[N];
inline void pushup(register int x)
{
sum[x]=sum[c[x][0]]+sum[c[x][1]]+v[x];
}
inline void pushdown(int x)
{
int l=c[x][0],r=c[x][1];
if(rev[x])
{
rev[l]^=1;
rev[r]^=1;
rev[x]^=1;
Swap(c[x][0],c[x][1]);
}
}
inline bool isroot(int x)
{
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
inline void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
if(c[y][0]==x)
l=0;
else
l=1;
r=l^1;
if(!isroot(y))
{
if(c[z][0]==y)
c[z][0]=x;
else
c[z][1]=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);
}
inline void splay(int x)
{
top=1;
q[top]=x;
for(register int i=x;!isroot(i);i=fa[i])
q[++top]=fa[i];
for(register int i=top;i;--i)
pushdown(q[i]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
{
if((c[y][0]==x)^(c[z][0]==y))
rotate(x);
else
rotate(y);
}
rotate(x);
}
pushup(x);
}
inline void access(int x)
{
for(register int t=0;x;t=x,x=fa[x])
{
splay(x);
c[x][1]=t;
pushup(x);
}
}
inline void makeroot(int x)
{
access(x);
splay(x);
rev[x]^=1;
}
inline int find(int x)
{
access(x);
splay(x);
while(c[x][0])
x=c[x][0];
return x;
}
inline void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
inline void cut(int x,int y)
{
split(x,y);
if(c[y][0]==x)
{
c[y][0]=0;
fa[x]=0;
}
}
inline void link(int x,int y)
{
makeroot(x);
fa[x]=y;
}
}T;
int main()
{
n=read();
for(register int i=1;i<=n;++i)
{
v[i]=read();
T.sum[i]=v[i];
}
m=read();
while(m--)
{
char c[10];
scanf("%s",c);
int x=read(),y=read();
if(c[0]=='b')
{
if(T.find(x)==T.find(y))
puts("no");
else
{
puts("yes");
T.link(x,y);
}
}
else if(c[0]=='p')
{
T.access(x);
T.splay(x);
v[x]=y;
T.pushup(x);
}
else
{
if(T.find(x)!=T.find(y))
puts("impossible");
else
{
T.split(x,y);
printf("%d\n",T.sum[y]);
}
}
}
return 0;
}
lct真简单
【题解】 Luogu P4312 / SP4155 [COCI 2009] OTOCI / 极地旅行社的更多相关文章
- [luogu]P4312 [COCI 2009] OTOCI / 极地旅行社(LCT)
P4312 [COCI 2009] OTOCI / 极地旅行社 题目描述 不久之前,Mirko建立了一个旅行社,名叫"极地之梦".这家旅行社在北极附近购买了N座冰岛,并且提供观光服 ...
- 洛谷P4312 [COCI 2009] OTOCI / 极地旅行社(link-cut-tree)
题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务. 当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mi ...
- [洛谷P4312][COCI 2009] OTOCI / 极地旅行社
题目大意:有$n(n\leqslant3\times10^4)$个点,每个点有点权,$m(m\leqslant3\times10^5)$个操作,操作分三种: $bridge\;x\;y:$询问节点$x ...
- P4312 [COCI 2009] OTOCI / 极地旅行社
思路 LCT维护和的板子 注意findroot的时候要先access一下,修改点权之前要先splay到根 代码 #include <cstdio> #include <algorit ...
- BZOJ 1180 [CROATIAN 2009]OTOCI // BZOJ 2843 极地旅行社 // Luogu P4321 [COCI 2009] OTOCI / 极地旅行社 (LCA板题)
emmm-标题卡着长度上限- LCT板题-(ε=ε=ε=┏(゜ロ゜;)┛) CODE #include <cctype> #include <cmath> #include & ...
- bzoj 2223 [Coci 2009]PATULJCI
[Coci 2009]PATULJCI Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1286 Solved: 553[Submit][Status ...
- 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 323 Solved: 218[Submit][Status][Discuss ...
- BZOJ_2223_[Coci 2009]PATULJCI_主席树
BZOJ_2223_[Coci 2009]PATULJCI_主席树 Description Input 10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 ...
- [题解] Luogu P5446 [THUPC2018]绿绿和串串
[题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...
随机推荐
- React对比Vue(05 生命周期的对比)
先来vue的吧,先上图,生命周期就好比一个人重出生到青少年再到青年再到中年在到老年到死亡的一个过程,不同的过程做不同的事情. 好了,上代码 beforeCreate :数据还没有挂载呢,只是一个空壳 ...
- PHP判断ip地址是否合法
1.获取真正ip地址 function get_ip(){ //判断服务器是否允许$_SERVER if(isset($_SERVER)){ if(isset($_SERVER[HTTP_X_FORW ...
- json_encode转义中文问题
默认情况下php的 json_decode 方法会把特殊字符进行转义,还会把中文转为Unicode编码形式. 这使得数据库查看文本变得很麻烦.所以我们需要限制对于中文的转义. 对于PHP5.4+版本, ...
- Javascript-do_while....
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- train_val.prototxt文件和deploy.prototxt文件开头的区别
1.开头不同 对train_val.prototxt文件来说,开头部分定义训练和测试的网络及参数 对deploy.prototxt文件来说,开头部分定义实际运用场景的配置文件,其参数不定义数据来源,仅 ...
- 2017高教杯数学建模B 题分析
B题原文 "拍照赚钱"是移动互联网下的一种自助式服务模式.用户下载APP,注册成为APP的会员,然后从APP上领取需要拍照的任务(比如上超市去检查某种商品的上架情况),赚取APP对 ...
- 向量体系结构(2)----SIMD指令集扩展和GPU
进行SIMD多媒体扩展的设计,源于一个很容易观察到的事实: 许多多媒体应用程序操作的数据类型比对32位处理器进行针对性优化的数据类型更窄一些. 图像三基色,都是8位.音频采样也都是8位和16位来表示. ...
- 重建二叉树POJ2255
重建二叉树 给定一棵二叉树的前序遍历和中序遍历的结果,求其后序遍历. 输入输入可能有多组,以EOF结束.每组输入包含两个字符串,分别为树的前序遍历和中序遍历.每个字符串中只包含大写字母且互不重复.输出 ...
- hdu3879 最大权闭合回路
题意: 有n个基站可以建立,然后m个团体会使用这些基站进行工作,地i个团体会适应Ai Bi 这两个基站, 如果建成收益Ci, 第j个基站花费Pj,求如何建立使得收益最大, 将每个团体看以一个点,然后 ...
- C. Primes or Palindromes?
prime numbers non greater than n is about . We can also found the amount of palindrome numbers with ...