思路

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 / 极地旅行社的更多相关文章

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

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

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

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

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

    题目大意:有$n(n\leqslant3\times10^4)$个点,每个点有点权,$m(m\leqslant3\times10^5)$个操作,操作分三种: $bridge\;x\;y:$询问节点$x ...

  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. 【BZOJ-2843&1180】极地旅行社&OTOCI Link-Cut-Tree

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

  7. BZOJ2843: 极地旅行社

    2843: 极地旅行社 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 90  Solved: 56[Submit][Status] Descripti ...

  8. BZOJ 2843: 极地旅行社( LCT )

    LCT.. ------------------------------------------------------------------------ #include<cstdio> ...

  9. [bzoj2843&&bzoj1180]极地旅行社 (lct)

    双倍经验双倍的幸福... 所以另一道是300大洋的世界T_T...虽然题目是一样的,不过2843数据范围小了一点... 都是lct基本操作 #include<cstdio> #includ ...

随机推荐

  1. 面向对象编程之Java多态

    我相信从学习计算机面向对象编程起就很多人背下了继承.封装.多态三个特性,可是多态并不是那么好理解的.通常做几道题,背下几次多态的动态绑定规律,可是依旧在一段时间后忘记了多态的存在,为什么要多态,这个程 ...

  2. 【python】——三级菜单

    作业需求: 打印三级菜单 可返回上一级 可随时退出程序 #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: __Json.Zzgx__ menu ...

  3. linux中时间命令详解

    DATE hling@hling:~$ date2018年 04月 11日 星期三 19:43:04 CSThling@hling:~$ date +%Y%M%d20184311hling@hling ...

  4. MSSQL:查看所有触发器信息的命令

    转自:http://www.2cto.com/database/201307/228708.html 编写程序,有时或为了偷懒,或为更简单地实现所需功能,使用了触发器.这可是把双刃剑,用得不好,程序出 ...

  5. ZOJ 4063 - Tournament - [递归][2018 ACM-ICPC Asia Qingdao Regional Problem F]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4063 Input Output Sample Input 2 3 ...

  6. webstorm 介绍

    最新版2017 破解 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key. ...

  7. Web开发——jQuery基础

    参考: 参考W3School:jQuery 教程 参考:jQuery 参考手册 参考(常用):jQuery API 测试 JavaScript 框架库 - jQuery 测试 JavaScript 框 ...

  8. Mybatis tinyint(1)自动转boolean

    使用Mybatis查询tinyint(1)字段数据,返回值为Map类型,那么tinyint(1)的数据默认会转化为boolean类型数据.解决方案:  1.使用ifnull(column, 0)处理该 ...

  9. npm更新指定的组件

    1.例如:react-router已经更新到4.x版本,想要下载2.x版本,可以通过下面命令 npm install --save-dev react-router@2.8.1 或 npm insta ...

  10. python练习题-day15

    1.请利用filter()过滤出1~100中平方根是整数的数,即结果应该是: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 2.列表按照其中每一个值的绝对值排序 li ...