bzoj1455罗马游戏*

bzoj1455罗马游戏

题意:

维护数据结构支持合并和弹出最小值。n≤1000000,m≤100000

题解:

可并堆,注意本题合并时要判断两个节点是否在同一个堆中。本弱写了左偏树和斜堆,发现斜堆比左偏树快,不知道为什么,求神犇解答。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 1000010
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12     return f*x;
13 }
14 int ch[maxn][2],v[maxn],fa[maxn]; bool die[maxn];
15 int find(int x){while(fa[x])x=fa[x]; return x;}
16 int merge(int x,int y){
17     if(!x||!y)return x+y; if(v[x]>v[y])swap(x,y); ch[x][1]=merge(ch[x][1],y);
18     fa[ch[x][1]]=x; swap(ch[x][0],ch[x][1]); return x;
19 }
20 void pop(int x){
21     fa[ch[x][0]]=fa[ch[x][1]]=0; merge(ch[x][0],ch[x][1]); ch[x][0]=ch[x][1]=0;
22 }
23 int n,m; char opt[3];
24 int main(){
25     n=read(); inc(i,1,n)v[i]=read(); m=read();
26     inc(i,1,m){
27         scanf("%s",opt);
28         if(opt[0]=='M'){
29             int a=read(),b=read(); if(die[a]||die[b])continue; int aa=find(a),bb=find(b);
30             if(aa==bb)continue; merge(aa,bb);
31         }
32         if(opt[0]=='K'){
33             int a=read(); if(die[a]||a>n)puts("0");else{
34                 int aa=find(a); pop(aa); die[aa]=1; printf("%d\n",v[aa]);
35             }
36         }
37     }
38     return 0;
39 }

 

20160810

转载于:https://www.cnblogs.com/YuanZiming/p/5769449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值