BZOJ1455: 罗马游戏

可并堆模板??又敲了一次,一遍过,很好。

 1 #include<string.h>
 2 #include<stdlib.h>
 3 #include<stdio.h>
 4 #include<math.h>
 5 //#include<assert.h>
 6 #include<algorithm>
 7 //#include<iostream>
 8 using namespace std;
 9 
10 int n,m;
11 #define maxn 1000011
12 struct leftist
13 {
14     struct Node
15     {
16         int ls,rs,v,dis;
17     }a[maxn];
18     leftist() {a[0].dis=-1;}
19     int merge(int x,int y)
20     {
21         if (!x || !y) return x^y;
22         if (a[x].v>a[y].v) {int t=x; x=y; y=t;}
23         a[x].rs=merge(a[x].rs,y);
24         if (a[a[x].ls].dis<a[a[x].rs].dis) {int t=a[x].ls; a[x].ls=a[x].rs; a[x].rs=t;}
25         a[x].dis=a[a[x].rs].dis+1;
26         return x;
27     }
28     void push(int id,int &root,int val)
29     {
30         a[id].v=val; a[id].ls=a[id].rs=a[id].dis=0;
31         root=merge(root,id);
32     }
33     void pop(int &root) {root=merge(a[root].ls,a[root].rs);}
34     int top(int root) {return a[root].v;}
35 }q;
36 
37 int root[maxn]; bool die[maxn];
38 int find(int x) {return x==root[x]?x:(root[x]=find(root[x]));}
39 int main()
40 {
41     scanf("%d",&n);
42     for (int i=1,x;i<=n;i++) scanf("%d",&x),q.push(i,root[i],x);
43     scanf("%d",&m);
44     char c; int x,y;
45     while (m--)
46     {
47         while ((c=getchar())!='K' && c!='M');
48         if (c=='K')
49         {
50             scanf("%d",&x);
51             if (die[x]) puts("0");
52             else
53             {
54                 x=find(x); die[x]=1; printf("%d\n",q.top(x));
55                 q.pop(root[x]); root[root[x]]=root[x];
56             }
57         }
58         else
59         {
60             scanf("%d%d",&x,&y);
61             if (die[x] || die[y]) continue;
62             x=find(x),y=find(y);
63             if (x==y) continue;
64             root[x]=root[y]=q.merge(root[x],root[y]);
65         }
66     }
67     return 0;
68 }
View Code

 

posted @ 2018-01-11 08:55  Blue233333  阅读(140)  评论(0编辑  收藏  举报