http://www.lydsy.com/JudgeOnline/problem.php?id=2843

https://blog.csdn.net/clove_unique/article/details/50992341

和之前那道题lct求两点距离用lca不同,这道题因为给的边的两个端点是没有顺序的(没法直接按照给的点直接将某个点连到树上),所以bridge需要区间翻转的操作,因为splay维护的是链,所以区间翻转相当于将叶子变成了根,根变成叶子(链翻转过来),然后再把此时的根(x)连到y上就可以了。

penguins操作之后要access一下

excursion操作时,将x翻转为根再access y就能求出这一段上的企鹅数。。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=;
int n,m; char f[]={};
int fa[maxn]={},ch[maxn][]={},siz[maxn]={};
int val[maxn]={},rev[maxn]={};
int sta[maxn]={},tail=;
int ds[maxn]={};
inline void updata(int x){ siz[x] = siz[ch[x][]] + siz[ch[x][]]+val[x];}
inline bool isroot(int x){ return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
int getfa(int x){if(ds[x]==x)return x; return ds[x]=getfa(ds[x]);}
void rotate(int x){
int y=fa[x];int fy=fa[y];
int l=ch[y][]==x?:;int r=l^;
if(!isroot(y)){
if(ch[fy][]==y) ch[fy][]=x;
else ch[fy][]=x;
}
fa[ch[x][r]]=y;fa[x]=fy;fa[y]=x;
ch[y][l]=ch[x][r];ch[x][r]=y;
updata(y);
}
void Swapdata(int x){
if(rev[x]){
swap(ch[x][],ch[x][]);
if(ch[x][])rev[ch[x][]]^=;
if(ch[x][])rev[ch[x][]]^=;
rev[x]=;
}
}
void splay(int x){
int w=x;
int y,fy;sta[++tail]=x;
while(w&&!isroot(w)){sta[++tail]=fa[w];w=fa[w];}
while(tail){Swapdata(sta[tail--]);}
while(!isroot(x)){
y=fa[x];fy=fa[y];
if(!isroot(y)){
if((ch[fy][]==y)^(ch[y][]==x))rotate(x);
else rotate(y);
}rotate(x);
}updata(x);
}
void Access(int x){
int y=;
while(x){
splay(x);
ch[x][]=y;
updata(x);
y=x;x=fa[y];
}
}
void Reverse(int x){
Access(x);
splay(x);
rev[x]^=;
}
void Link(int x,int y){
Reverse(x);fa[x]=y;
splay(x);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&val[i]);
for(int i=;i<=n;i++)ds[i]=i;
scanf("%d",&m);
int x,y,xx,yy;
for(int i=;i<=m;i++){
scanf("%s",f);
scanf("%d%d",&x,&y);
if(f[]=='b'){
xx=getfa(x);yy=getfa(y);
if(xx==yy)printf("no\n");
else{
printf("yes\n");ds[xx]=yy;
if(x!=y)Link(x,y);
}
}
else if(f[]=='p'){
val[x]=y;Access(x);splay(x);
}
else{
xx=getfa(x);yy=getfa(y);
if(xx!=yy)printf("impossible\n");
else{
Reverse(x);Access(y);splay(y);
printf("%d\n",siz[y]);
}
}
}
return ;
}

BZOJ 2843: 极地旅行社 lct splay的更多相关文章

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

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

  2. bzoj 2843 极地旅行社(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2843 [题意] 给定一个森林,要求提供连边,修改点值,查询路径和的操作. [思路] L ...

  3. bzoj 2843: 极地旅行社

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1077  Solved: 645[Submit][Status][Discuss] Descripti ...

  4. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

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

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

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

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

  7. 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...

  8. 【bzoj2843】极地旅行社 LCT

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

  9. BZOJ 3091: 城市旅行 [LCT splay 期望]

    3091: 城市旅行 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1454  Solved: 483[Submit][Status][Discuss ...

随机推荐

  1. MUI上拉加载下拉刷新

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. CSS line-height应用

    一.固定高度的容器,单行文本垂直居中 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf- ...

  3. SPI子系统分析之二:数据结构【转】

    转自:http://www.cnblogs.com/jason-lu/articles/3164901.html 内核版本:3.9.5 spi_master struct spi_master用来描述 ...

  4. Django-自动HTML转义

    一.自动HTML转义 从模板生成HTML时,总会有变量包含影响最终HTML的字符风险,例如,考虑这个模板的片段: Hello, {{ name }} 起初,这是一种显示用户名的无害方式,但考虑用户输入 ...

  5. 1->小规模集群架构规划

    "配置无人值守批量安装系统(Cobbler)" "搭建PPTP VPN/ NTP/Firewalld内部共享上网 " "搭建跳板机服务jumpserv ...

  6. 【小程序开发总结】微信小程序开发常用技术方法总结

    1.获取input的值 <input bindinput="bindKeyInput" placeholder="输入同步到view中"/>   b ...

  7. 洛谷P1195口袋的天空

    传送门啦 一个裸的最小生成树,输出 $ No Answer $ 的情况只有 $ k < n $ 的时候. 开始令 $ num =n $ ,如果 $ num = k $ ,直接输出 $ 0 $ , ...

  8. 洛谷P1938 找工就业

    传送门啦 这个题本质就是跑一边最长路,重点就是在怎么建图上. 我们可以把点权放到边权上面,即将每一个边的终点点权当做这个边的边权,这个题里就是将工钱 $ d $ 当做边权. 如果这一条边需要坐飞机才能 ...

  9. Centos之字符串搜索命令grep

    grep [选项] 字符串 文件名 在文件当中匹配符合条件的字符串 选项: -i 忽略大小写 -v 排除指定字符串 [root@localhost ~]# grep "work" ...

  10. Bad connection to FS. command aborted. exception: Call to chaoren/192.168.80.100:9000 failed on connection exception: java.net.ConnectException: Connection refused

    Bad connection to FS. command aborted. exception: Call to chaoren/192.168.80.100:9000 failed on conn ...