BZOJ2733 [HNOI2012]永无乡

直接平衡树启发式合并就好了。。。貌似是个很高端的东西。。

貌似可以证明splay的启发式合并是均摊$O(nlogn)$的。。。而其他平衡树都不行,所以其他的复杂度都是$O(nlog^2n)的$的

所以就用平板电视里的splay好啦!2333

 /**************************************************************
Problem: 2733
User: rausen
Language: C++
Result: Accepted
Time:2148 ms
Memory:9756 kb
****************************************************************/ #include <cstdio>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> using namespace std;
using namespace __gnu_pbds;
typedef tree<int, int, less<int>, splay_tree_tag, tree_order_statistics_node_update> Tree;
typedef Tree :: iterator iter;
const int N = 1e5 + ; int read();
int get_op(); int n, m;
int a[N];
int fa[N], sz[N];
Tree T[N]; int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
} void _union(int x, int y) {
iter it;
x = find(x), y = find(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
for (it = T[y].begin(); it != T[y].end(); ++it)
T[x][it -> first] = it -> second;
fa[y] = x, sz[x] += sz[y], T[y].clear();
} int query() {
int x = find(read()), k = read();
if (k > T[x].size()) return -;
return T[find(x)].find_by_order(k - ) -> second;
} int main() {
int i, Q, oper;
n = read(), m = read();
for (i = ; i <= n; ++i) {
a[i] = read(), fa[i] = i;
T[i][a[i]] = i;
}
for (i = ; i <= m; ++i) _union(read(), read());
for (Q = read(); Q; --Q) {
oper = get_op();
if (oper == ) _union(read(), read());
else printf("%d\n", query());
}
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
} inline int get_op() {
static char ch;
ch = getchar();
while (ch != 'Q' && ch != 'B') ch = getchar();
return ch == 'Q';
}
上一篇:在表单(input)中id和name的区别


下一篇:vscode 前端插件推荐