BZOJ1180 [CROATIAN2009]OTOCI LCT
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ1180
本题和BZOJ2843一样。
BZOJ2843 极地旅行社 LCT
题意概括
有n座岛
每座岛上的企鹅数量虽然会有所改变,但是始终在[0, 1000]之间。你的程序需要处理以下三种命令:
1."bridge A B"——在A与B之间建立一座大桥(A与B是不同的岛屿)。由于经费限制,这项命令被接受,当且仅当A与B不联通。若这项命令被接受,你的程序需要输出"yes",之后会建造这座大桥。否则,你的程序需要输"no"。
2."penguins A X"——根据可靠消息,岛屿A此时的帝企鹅数量变为X。这项命令只是用来提供信息的,你的程序不需要回应。
3."excursion A B"——一个旅行团希望从A出发到B。若A与B连通,你的程序需要输出这个旅行团一路上所能看到的帝企鹅数量(包括起点A与终点B),若不联通,你的程序需要输出"impossible"。
题解
本题仍然是LCT模版题。
bridge:先判断A和B是否联通,然后连边即可。
penguins:直接把A搞到付诸树根,然后修改就可以了。
excursion:先判断是否联通,然后如果不联通,那么我们走类似于cut的过程,让a成为b的左子节点且为最左子节点,那么答案就是size[a]+val[b]
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cmath> using namespace std; const int N=30005; int n,m; int fa[N],son[N][2],size[N],rev[N],val[N]; bool isroot( int x){ return son[fa[x]][0]!=x&&son[fa[x]][1]!=x; } void pushup( int x){ size[x]=val[x]+size[son[x][0]]+size[son[x][1]]; } void pushdown( int x){ if (rev[x]){ rev[x]=0; rev[son[x][0]]^=1; rev[son[x][1]]^=1; swap(son[x][0],son[x][1]); } } void pushadd( int x){ if (!isroot(x)) pushadd(fa[x]); pushdown(x); } int wson( int x){ return son[fa[x]][1]==x; } void rotate( int x){ if (isroot(x)) return ; int y=fa[x],z=fa[y],L=wson(x),R=L^1; if (!isroot(y)) son[z][wson(y)]=x; fa[x]=z,fa[y]=x,fa[son[x][R]]=y; son[y][L]=son[x][R],son[x][R]=y; pushup(y); pushup(x); } void splay( int x){ pushadd(x); for ( int y=fa[x];!isroot(x);rotate(x),y=fa[x]) if (!isroot(y)) rotate(wson(x)==wson(y)?y:x); } void access( int x){ int t=0; while (x){ splay(x); son[x][1]=t; pushup(x); t=x; x=fa[x]; } } void rever( int x){ access(x); splay(x); rev[x]^=1; } void link( int x, int y){ rever(x); fa[x]=y; } void cut( int x, int y){ rever(x); access(y); splay(y); fa[x]=son[y][0]=0; } int find( int x){ access(x); splay(x); while (1){ pushdown(x); if (son[x][0]) x=son[x][0]; else break ; } return x; } int main(){ scanf ( "%d" ,&n); for ( int i=1;i<=n;i++){ scanf ( "%d" ,&val[i]); size[i]=val[i],son[i][0]=son[i][1]=fa[i]=rev[i]=0; } scanf ( "%d" ,&m); while (m--){ char op[10]; int a,b; scanf ( "%s%d%d" ,op,&a,&b); if (op[0]== 'b' ){ if (find(a)==find(b)) puts ( "no" ); else puts ( "yes" ),link(a,b); } else if (op[0]== 'p' ){ rever(a); val[a]=b; pushup(a); } else { if (find(a)==find(b)){ rever(a); access(b); splay(b); printf ( "%d\n" ,size[son[b][0]]+val[b]); } else puts ( "impossible" ); } } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)