题意:
带权联通块,添边与查询联通块中第k大.
SOL:
splay合并+并查集.
我以为splay可以用奇技淫巧来简单合并...调了一下午终于幡然醒悟...于是就只好一个一个慢慢插...什么启发式合并...说的那么高级其实那么丝帛.
实现得非常渣...中序遍历3060ms,换成队列并没有快起来...难道是我的splay本身就不对?...
%%%hzw的线段树...发现其实线段树合并更简单更快...被splay蒙蔽了眼睛...思路不够开阔...唉...
Code:
/*==========================================================================
# Last modified: 2016-03-20 10:23
# Filename: 2733.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 400000
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
int ch[maxn][2],fa[maxn],father[maxn],sz[maxn],key[maxn];
void pushup(int x){ sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1; }
int kth(int x,int k){
while (x){
if (k==sz[ch[x][0]]+1) return x;
if (k>sz[ch[x][0]]+1) k-=sz[ch[x][0]],k--,x=ch[x][1];
else x=ch[x][0];
}
}
int find(int x){
return father[x]==x?x:find(father[x]);
}
void rotate(int x){
int p=fa[x],q=fa[p],d=ch[p][1]==x;
fa[ch[p][d]=ch[x][d^1]]=p; pushup(p);
fa[ch[x][d^1]=p]=x;pushup(x);
fa[x]=q;
if (q){
if (ch[q][1]==p) ch[q][1]=x;
else ch[q][0]=x;
}
}
void splay(int x,int aim){
for (int y;(y=fa[x])!=aim;rotate(x))
if (fa[y]!=aim)
rotate((getlc(y)==x)==(getlc(fa[y])==y)?y:x);
}
void insert(int x,int root){
int node=root;
while (node){
sz[node]++;
int &d=ch[node][key[node]<key[x]];
if (d==0) {
d=x; fa[x]=node; ch[x][0]=ch[x][1]=0; sz[x]=1; node=x; break;
}
node=d;
}
//splay(node,0);
}
int q[maxn];
void join(int x,int root){
/*int lc=ch[x][0],rc=ch[x][1];
ch[x][0]=ch[x][1]=0;
if (lc) {
join(lc,root);
}
//ch[x][1]=0; fa[rc]=0;
insert(x,root);
if (rc) join(rc,root);*/
int head=1,tail=1;
q[head]=x;
while (head<=tail){
x=q[head]; head++;
int lc=ch[x][0],rc=ch[x][1];
ch[x][0]=ch[x][1]=0;
insert(x,root);
if (lc) q[++tail]=lc;
if (rc) q[++tail]=rc;
}
}
void Union(int x,int y){
int u=find(x),v=find(y);
if (u==v) return;
else {
father[u]=v;
splay(u,0); splay(v,0);
if (sz[u]<sz[v]) join(u,v);
else join(v,u);/*father[u]=v;
int a=kth(u,1),b=kth(v,sz[v]);
splay(a,0); splay(b,0);
if (key[a]<key[b]) {ch[a][1]=b; fa[b]=a; pushup(a);}
else {ch[b][1]=a; fa[a]=b; pushup(b);}
*/ }
}
void query(int x,int k){
splay(x,0);
if (k>sz[x]) printf("-1\n");
else printf("%d\n",kth(x,k));
}
int main(){
int n,m; read(n); read(m);
memset(father,0,sizeof(father));
FORP(i,1,n) { read(key[i]); father[i]=i; sz[i]=1;}
FORP(i,1,m){
int u,v; read(u); read(v); Union(u,v);
}
int q; read(q);
FORP(i,1,q){
char ch[10]; scanf("%s",ch);
if (ch[0]=='Q') {
int x,k; read(x); read(k);
query(x,k);
}
else { int u,v; read(u); read(v); Union(u,v);}
}
}