P4312 [COCI 2009] OTOCI / 极地旅行社
思路
LCT维护和的板子
注意findroot的时候要先access一下,修改点权之前要先splay到根
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct Node{
int fa,son[2],sum,val,inv;
}SPT[600000];
int n,q;
bool isrl(int o){
return o==SPT[SPT[o].fa].son[1];
}
bool isroot(int o){
return o!=SPT[SPT[o].fa].son[0]&&o!=SPT[SPT[o].fa].son[1];
}
void pushup(int o){
SPT[o].sum=SPT[o].val+SPT[SPT[o].son[0]].sum+SPT[SPT[o].son[1]].sum;
}
void pushdown(int o){
if(SPT[o].inv){
SPT[SPT[o].son[0]].inv^=1;
SPT[SPT[o].son[1]].inv^=1;
swap(SPT[o].son[0],SPT[o].son[1]);
SPT[o].inv=0;
}
}
void rorate(int o){
if(isroot(o))
return;
int f=SPT[o].fa;
int g=SPT[f].fa;
int which=isrl(o);
pushdown(f);
pushdown(o);
if(!isroot(f))
SPT[g].son[SPT[g].son[1]==f]=o;
SPT[o].fa=g;
SPT[f].son[which]=SPT[o].son[which^1];
SPT[SPT[o].son[which^1]].fa=f;
SPT[o].son[which^1]=f;
SPT[f].fa=o;
pushup(f);
pushup(o);
}
void allpush(int o){
if(!isroot(o))
allpush(SPT[o].fa);
pushdown(o);
}
void splay(int o){
allpush(o);
for(int f;!isroot(o);rorate(o)){
if(!isroot(f=SPT[o].fa))
rorate((isrl(f)==isrl(o))?f:o);
}
}
void access(int o){
for(int y=0;o;o=SPT[y=o].fa)
splay(o),SPT[o].son[1]=y,pushup(o);
}
void makeroot(int o){
access(o);
splay(o);
SPT[o].inv^=1;
pushdown(o);
}
int findroot(int o,int islink=0){
access(o);
splay(o);
pushdown(o);
while(SPT[o].son[0])
pushdown(o=SPT[o].son[0]);
return o;
}
bool link(int x,int y){
makeroot(x);
if(findroot(y,1)!=x){
SPT[x].fa=y;
return true;
}
return false;
}
int split(int x,int y){
makeroot(x);
access(y);
splay(y);
return y;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&SPT[i].val);
scanf("%d",&q);
char opt[20];
int x,y;
for(int i=1;i<=q;i++){
scanf("%s %d %d",opt,&x,&y);
if(opt[0]=='b'){
if(link(x,y))
printf("yes\n");
else
printf("no\n");
}
else if(opt[0]=='p'){
splay(x);
SPT[x].val=y;
}
else{
if(findroot(x)!=findroot(y)){
printf("impossible\n");
}
else{
int t=split(x,y);
printf("%d\n",SPT[t].sum);
}
}
}
return 0;
}
P4312 [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 ...
- 【题解】 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 & ...
- 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 323 Solved: 218[Submit][Status][Discuss ...
- BZOJ2843: 极地旅行社
2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 90 Solved: 56[Submit][Status] Descripti ...
- BZOJ 2843: 极地旅行社( LCT )
LCT.. ------------------------------------------------------------------------ #include<cstdio> ...
- [bzoj2843&&bzoj1180]极地旅行社 (lct)
双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...
随机推荐
- CXF整合Sping与Web容器
1.创建HelloWorld 接口类 package com.googlecode.garbagecan.cxfstudy.helloworld; import javax.jws.WebMethod ...
- profile和bashrc四种的区别
Linux下profile和bashrc四种的区别 12160阅读 0评论 /etc/profile./etc/bashrc.~/.bash_profile.~/.bashrc很容易混淆,他们之间有什 ...
- python特殊函数__str__、__repr__和__len__
1.__str__ 首先介绍__str__ class Students(object): def __init__(self, *args): self.names = args # def __s ...
- js中级小知识4
1.针对表单 form input select textarea type="radio/checkbox/passdord/button/submit/reset/ ...
- Linux 命令 which whereis locate find
which: 查询某指令的完整路径 $ which [-a] command -a: 将所有在PATH目录中可以找到的指令均列出. 注意:只搜索PATH下的路径. whereis: 只搜索几个特定目录 ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- Django之中间件&信号&缓存&form上传
中间件 1.中间件是什么? 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用, ...
- Django之Cookie、Session、CSRF、Admin
Django之Cookie.Session.CSRF.Admin Cookie 1.获取Cookie: 1 2 3 4 5 6 request.COOKIES['key'] request.get ...
- 点击刷新验证码所需要的onclick函数
<img src="__APP__/Public/verify" onclick="this.src=this.src+'?'+Math.random()" ...
- [js]作用域链查找规则获取值和设置值
作用域链查找规则获取值和设置值 <script> /** 1.作用域链查找规则 私有作用域出现的一个变量不是私有的,则往上一级作用域查找,上级作用域没有则继续向上级查找,一直找到windo ...