洛谷 P3224 [HNOI2012]永无乡
(splay_tree_tag没有比rb_tree_tag快)
1 #include<cstdio> 2 #include<algorithm> 3 #include<ext/pb_ds/assoc_container.hpp> 4 #include<ext/pb_ds/tree_policy.hpp> 5 using namespace std; 6 using namespace __gnu_pbds; 7 typedef std::pair<int,int> P; 8 typedef tree<P,null_type,less<P>,rb_tree_tag,tree_order_statistics_node_update> S; 9 S dat[100100]; 10 int fa[100100],n,m; 11 char s[10]; 12 int find(int x) 13 { 14 return fa[x]==x ? x : fa[x]=find(fa[x]); 15 } 16 int main() 17 { 18 int i,t,ta,tb,a,b,q; 19 S::iterator it; 20 scanf("%d%d",&n,&m); 21 for(i=1;i<=n;i++) fa[i]=i; 22 for(i=1;i<=n;i++) 23 { 24 scanf("%d",&t); 25 dat[i].insert(P(t,i)); 26 } 27 for(i=1;i<=m;i++) 28 { 29 scanf("%d%d",&a,&b); 30 ta=find(a);tb=find(b); 31 if(ta==tb) continue; 32 fa[ta]=tb; 33 if(dat[ta].size()>dat[tb].size()) swap(dat[ta],dat[tb]); 34 for(it=dat[ta].begin();it!=dat[ta].end();++it) dat[tb].insert(*it); 35 } 36 scanf("%d",&q); 37 while(q--) 38 { 39 scanf("%s%d%d",s,&a,&b); 40 if(s[0]=='Q') 41 { 42 ta=find(a); 43 if(dat[ta].size()<b) printf("-1\n"); 44 else printf("%d\n",dat[ta].find_by_order(b-1)->second); 45 } 46 else if(s[0]=='B') 47 { 48 ta=find(a);tb=find(b); 49 if(ta==tb) continue; 50 fa[ta]=tb; 51 if(dat[ta].size()>dat[tb].size()) swap(dat[ta],dat[tb]); 52 for(it=dat[ta].begin();it!=dat[ta].end();++it) dat[tb].insert(*it); 53 } 54 } 55 return 0; 56 }