【题目分析】

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

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

其实并没有快很多。

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

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;

#define maxn 1000005

int read()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

int fa[maxn],v[maxn];
int n,q,l[maxn],r[maxn],die[maxn],d[maxn];
char opt[11];

int gf(int k)
{
    if (fa[k]==k) return k;
    else return fa[k]=gf(fa[k]);
}

int merge(int x,int y)
{
    if (!(x*y)) return x+y;
    if (v[x]>v[y]) swap(x,y);
    r[x]=merge(r[x],y);
    if (d[l[x]]<d[r[x]]) swap(l[x],r[x]);
    d[x]=d[r[x]]+1;
    return x;
}

int main()
{
    n=read();
    for (int i=1;i<=n;++i) v[i]=read(),fa[i]=i;
    q=read();
    while (q--)
    {
        scanf("%s",opt);
        if (opt[0]=='M')
        {
            int x=read(),y=read();
            if (die[x]||die[y]) continue;
            int fx=gf(x),fy=gf(y);
            if (fx!=fy) fa[fx]=fa[fy]=merge(fx,fy);
        }
        else
        {
            int x=read();
            if (die[x])
            {
                printf("0\n");
                continue;
            }
            else
            {
                int fx=gf(x);
                die[fx]=1;
                printf("%d\n",v[fx]);
                int tmp=merge(l[fx],r[fx]);
                fa[fx]=fa[tmp]=tmp;
            }
        }
    }
}

  

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. 100_1小记ressons analysis

    想到一首诗用以自勉: look to the master, follow the master, walk with the master, see through the master, beco ...

  2. [转载]抓包,端口镜像,monitor session命令(转)

    原文地址:抓包,端口镜像,monitor session命令(转)作者:浮云皓月 一.SPAN简介 SPAN技术主要是用来监控交换机上的数据流,大体分为两种类型,本地SPAN和远程SPAN. --Lo ...

  3. Centos6.5 Zabbix3 server端安装(一)

    一.准备阶段: 1.>关闭防火墙 /etc/init.d/iptables stop 2.>关闭selinux vim /etc/selinux/config SELINUX=disabl ...

  4. LinqPad工具:帮你快速学习Linq

    LinqPad工具:帮你快速学习Linq 参考: http://www.cnblogs.com/li-peng/p/3441729.html ★:linqPad下载地址:http://www.linq ...

  5. [linux]Socket编程的头文件

    socket编程中需要用到的头文件 sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构 netinet/in.h:定义数据结构sockaddr_in arp ...

  6. PHP函数 addslashes() 和 mysql_real_escape_string() 的区别 && SQL宽字节,绕过单引号注入攻击

    首先:不要使用 mysql_escape_string(),它已被弃用,请使用 mysql_real_escape_string() 代替它. mysql_real_escape_string() 和 ...

  7. Android实现页面跳转及传递参数的方法

    简单的示例 实现的效果是这样的: 第一个页面有一个按钮,一个文本框,点击按钮,将文本框的内容传递到第二个页面,并显示在第二个页面中. 首先是在给按钮添加点击事件 setOnClickListener( ...

  8. linux安装php & nginx

    1.安装libxml2 地址:http://ftp.gnome.org/pub/GNOME/sources/libxml2/ wget http://caesar.acc.umu.se/pub/GNO ...

  9. 多重网格法简介(Multi Grid)

    原文链接 多重网格法是一种用于求解方程组的方法,可用于插值.解微分方程等. 从专业角度讲多重网格法实际上是一种多分辨率的算法,由于直接在高分辨率(用于求解的间隔小)上进行求解时对于低频部分收敛较慢,与 ...

  10. table的border重合问题

    1. table { border-collapse: collapse; } td { border: 1px solid blue; } 用css的方法直接实现 2. border="1 ...