# bzoj1455

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

## Sample Output

10
100
0
66

 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string>
4 #include<string.h>
5 #include<iostream>
6 #include<algorithm>
7 #include<queue>
8 #include<math.h>
9 #include<vector>
10 #include<map>
11 #include<set>
12 #define il inline
13 #define re register
14 using namespace std;
15 const int N=1000010;
16 int n,f[N],l[N],r[N],d[N],v[N],Q;
17 bool die[N];
19     re int hs=0;re char c=getchar();
20     while(!isdigit(c)) c=getchar();
21     while(isdigit(c)){
22         hs=(hs<<3)+(hs<<1)+c-'0';
23         c=getchar();
24     }
25     return hs;
26 }
27 il void swap(re int &a,re int &b){
28     a^=b;
29     b^=a;
30     a^=b;
31 }
32 il int getfather(re int u){
33     return f[u]==u?u:f[u]=getfather(f[u]);
34 }
35 il int merge(re int a,re int b){
36     if(!a) return b;
37     if(!b) return a;
38     if(v[a]>v[b]) swap(a,b);
39     r[a]=merge(r[a],b);
40     if(d[r[a]]>d[l[a]]) swap(l[a],r[a]);
41     d[a]=d[r[a]]+1;
42     return a;
43 }
44 int main(){
47     for(int i=1;i<=n;i++) f[i]=i;
49     for(re int i=1,p,q,fp,fq;i<=Q;i++){
51         if(str[0]=='M'){
53             if(die[p]||die[q]) continue;
54             fp=getfather(p);
55             fq=getfather(q);
56             if(fp!=fq) f[fp]=f[fq]=merge(fp,fq);
57         }
58         else{
59             if(die[p]) printf("0\n");
60             else{
61                 q=getfather(p);die[q]=1;
62                 printf("%d\n",v[q]);
63                 f[q]=merge(l[q],r[q]);
64                 f[f[q]]=f[q];
65             }
66         }
67     }
68     return 0;
69 }

posted @ 2016-08-24 15:34  ExiledPoet  阅读(225)  评论(0编辑  收藏  举报