Description
Solution
每次合并时考虑将较小的Splay中的节点依次插入较大的Splay中,发现每个点最多会被插入 log2n l o g 2 n 次,时间复杂度为 O(nlog22n) O ( n l o g 2 2 n )
Source
/************************************************
* Au: Hany01
* Date: Feb 27th, 2018
* Prob: [BZOJ2733][HNOI2012]永无乡
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void File()
{
#ifdef hany01
freopen("bzoj2733.in", "r", stdin);
freopen("bzoj2733.out", "w", stdout);
#endif
}
const int maxn = 100005;
int n, fa[maxn], ch[maxn][2], sz[maxn], val[maxn], BEG, fc[maxn];
int find(int x) { return fc[x] == x ? x : fc[x] = find(fc[x]); }
#define maintain(u) sz[u] = sz[ch[u][0]] + sz[ch[u][1]] + 1
#define dir(u) (ch[fa[u]][1] == u)
inline void rotate(int u) {
register int f = fa[u], gf = fa[f], d = dir(u), gd = dir(f);
fa[ch[f][d] = ch[u][d ^ 1]] = f,
ch[fa[f] = u][d ^ 1] = f,
ch[fa[u] = gf][gd] = u,
maintain(f), maintain(u);
}
inline void splay(int u, int gl) {
for ( ; fa[u] != gl; rotate(u))
if (fa[fa[u]] != gl) rotate(dir(u) == dir(fa[u]) ? fa[u] : u);
}
inline void Insert(int t)
{
int u = BEG;
for ( ; ; ) {
if (val[t] < val[u]) {
if (!ch[u][0]) { ch[u][0] = t; break; }
u = ch[u][0];
} else {
if (!ch[u][1]) { ch[u][1] = t; break; }
u = ch[u][1];
}
}
fa[t] = u, ch[t][0] = ch[t][1] = 0;
maintain(t), maintain(u);
splay(t, BEG);
}
void DFS(int u)
{
if (ch[u][0]) DFS(ch[u][0]);
if (ch[u][1]) DFS(ch[u][1]);
Insert(u);
}
inline void Merge(int x, int y)
{
register int fx = find(x), fy = find(y);
if (fx == fy) return ;
fc[fx] = fy;
splay(x, 0), splay(y, 0);
if (sz[x] > sz[y]) swap(x, y);
BEG = y, DFS(x);
}
int kth(int u, int k)
{
if (sz[ch[u][0]] >= k) return kth(ch[u][0], k);
if (sz[ch[u][0]] + 1 == k) return u;
return kth(ch[u][1], k - sz[ch[u][0]] - 1);
}
inline void Init()
{
static int u, v, m;
n = read(), m = read();
For(i, 1, n) val[i] = read(), sz[i] = 1, fc[i] = i;
while (m --) u = read(), v = read(), Merge(u, v);
}
inline void Solve()
{
static int a, b, m;
static char s[2];
for (m = read(); m --; ) {
scanf("%s", s), a = read(), b = read();
if (s[0] == 'B') Merge(a, b);
else {
splay(a, 0);
if (sz[a] >= b) printf("%d\n", kth(a, b)); else puts("-1");
}
}
}
int main()
{
File();
Init();
Solve();
return 0;
}
//野旷天低树,江清月近人。
// -- 孟浩然《宿建德江》