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

1455: 罗马游戏

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 2285  Solved: 994
[Submit][Status][Discuss]

5
100 90 66 99 10
7
M 1 5
K 1
K 1
M 2 3
M 3 4
K 5
K 4

10
100
0
66

HINT

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<cmath>
6 #include<algorithm>
7 #define maxn 1000005
8 using namespace std;
9 int d[maxn],ls[maxn],rs[maxn],a[maxn],size[maxn],fa[maxn];
10 int n,m;
11 int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
12 int merger(int x,int y){
13     if(!x) return y;
14     if(!y) return x;
15     if(a[x]>a[y]) swap(x,y);
16     rs[x]=merger(rs[x],y);
17     if(size[ls[x]]<size[rs[x]]) swap(ls[x],rs[x]);
18     size[x]=size[rs[x]]+1;
19     return x;
20
21 }
22 int main() {
23     scanf("%d",&n);
24     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
25     scanf("%d",&m);
26     for(int i=1;i<=n;i++) fa[i]=i;
27     for(int i=1;i<=m;i++) {
28         char ch[10];
29         scanf("%s",ch);
30         if(ch[0]=='M') {
31             int x,y;
32             scanf("%d%d",&x,&y);
33             if(d[x]||d[y]) continue;
34             int f1=find(x),f2=find(y);
35             if(f1!=f2) {fa[f1]=fa[f2]=merger(f1,f2);}
36         }
37         else {
38             int x;
39             scanf("%d",&x);
40             if(d[x]) printf("0\n");
41             else {
42                 int f=find(x);d[f]=1;
43                 printf("%d\n",a[f]);
44                 fa[f]=merger(ls[f],rs[f]);
45                 fa[fa[f]]=fa[f];
46             }
47         }
48     }
49 }
View Code

O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~
posted @ 2017-10-30 16:45  wls001  阅读(129)  评论(0编辑  收藏  举报