【题目分析】

左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆。

感觉和启发式合并基本相同。

其实并没有快很多。

本人的左偏树代码自带大常数,借鉴请慎重

【代码】

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. #include <set>
  7. #include <map>
  8. #include <string>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <iostream>
  12. #include <queue>
  13. using namespace std;
  14.  
  15. #define maxn 1000005
  16.  
  17. int read()
  18. {
  19. int x=0,f=1; char ch=getchar();
  20. while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
  21. while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
  22. return x*f;
  23. }
  24.  
  25. int fa[maxn],v[maxn];
  26. int n,q,l[maxn],r[maxn],die[maxn],d[maxn];
  27. char opt[11];
  28.  
  29. int gf(int k)
  30. {
  31. if (fa[k]==k) return k;
  32. else return fa[k]=gf(fa[k]);
  33. }
  34.  
  35. int merge(int x,int y)
  36. {
  37. if (!(x*y)) return x+y;
  38. if (v[x]>v[y]) swap(x,y);
  39. r[x]=merge(r[x],y);
  40. if (d[l[x]]<d[r[x]]) swap(l[x],r[x]);
  41. d[x]=d[r[x]]+1;
  42. return x;
  43. }
  44.  
  45. int main()
  46. {
  47. n=read();
  48. for (int i=1;i<=n;++i) v[i]=read(),fa[i]=i;
  49. q=read();
  50. while (q--)
  51. {
  52. scanf("%s",opt);
  53. if (opt[0]=='M')
  54. {
  55. int x=read(),y=read();
  56. if (die[x]||die[y]) continue;
  57. int fx=gf(x),fy=gf(y);
  58. if (fx!=fy) fa[fx]=fa[fy]=merge(fx,fy);
  59. }
  60. else
  61. {
  62. int x=read();
  63. if (die[x])
  64. {
  65. printf("0\n");
  66. continue;
  67. }
  68. else
  69. {
  70. int fx=gf(x);
  71. die[fx]=1;
  72. printf("%d\n",v[fx]);
  73. int tmp=merge(l[fx],r[fx]);
  74. fa[fx]=fa[tmp]=tmp;
  75. }
  76. }
  77. }
  78. }

  

BZOJ 1455 罗马游戏 ——左偏树的更多相关文章

  1. bzoj 1455: 罗马游戏 左偏树+并查集

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 668  Solved: 247[Submit][Status] Descriptio ...

  2. BZOJ 1455 罗马游戏 左偏树

    题目大意:给定n个点,每一个点有一个权值,提供两种操作: 1.将两个点所在集合合并 2.将一个点所在集合的最小的点删除并输出权值 非常裸的可并堆 n<=100W 启示式合并不用想了 左偏树就是快 ...

  3. 1455: 罗马游戏[左偏树or可并堆]

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1861  Solved: 798[Submit][Status][Discuss] ...

  4. BZOJ1455 罗马游戏 左偏树 可并堆

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1455 题意概括 n个人,2种操作. 一种是合并两个人团,一种是杀死某一个人团的最弱的人. 题解 左 ...

  5. [BZOJ1455]罗马游戏 左偏树+并查集

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 2285  Solved: 994[Submit][Status][Discuss] ...

  6. BZOJ 1455: 罗马游戏 [可并堆]

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1715  Solved: 718[Submit][Status][Discuss] ...

  7. bzoj 1455: 罗马游戏

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MB Description 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最 ...

  8. BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )

    可并堆水题 --------------------------------------------------------- #include<bits/stdc++.h>   usin ...

  9. bzoj 4003: 城池攻占 左偏树

    题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=4003 题解 一开始看漏条件了 题目保证当占领城池可以使攻击力乘上\(v_i\)时,一定有\ ...

随机推荐

  1. mybatis一个怪异的问题: Invalid bound statement not found

    ssm中报一下错误: invalid bound statement (not found): me.tspace.pm.dao.userdao.getuser    at org.apache.ib ...

  2. python更新后yum问题

    How to switch between Python versions on Fedora Linux Currently, the default python version on Fedor ...

  3. Android热修复AndFix

    热修复主要用来修复代码.修复bug.添加独立的功能,他的原理主要是操作PathClassLoader.DexClassLoader. PathClassLoader是类加载器,DexClassLoad ...

  4. spring aop的使用

    使用上次整合的s2sh工程,加入aop的功能.aop(面向切面编程)是一种对oop的完善,比如在进行数据库操作的时候执行方法前或者后加入一条日志记录,使用一个额外的类去做日志操作,让dao类专注地做它 ...

  5. 搭建haproxy

    1:下载haproxy wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz2:解压,编译,安装 tar zxf hapr ...

  6. css清楚浮动的方法

  7. zendstudio快捷键收录

    360截屏快捷键:ctrl+shift+x zendstudio:注释代码:ctrl+shift+/ 删除光标所在行:ctrl+D 复制当前行:ctrl+alt+↓ 上下行互换:alt+↑/↓ 代码格 ...

  8. poj1062 昂贵的聘礼

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  9. 【html】学习记录-拖放(drag and drop)

    目的:实现拖动目标并放置到指定区域.   使元素可拖动,涉及到元素的全局属性draggable <img draggable="true" /> 即img元素设置为可拖 ...

  10. Android高手速成--第四部分 开发工具及测试工具

    第四部分 开发工具及测试工具 主要介绍和Android开发工具和测试工具相关的开源项目. 一.开发效率工具 Json2Java根据JSon数据自动生成对应的Java实体类,还支持Parcel.Gson ...