P3224 [HNOI2012]永无乡 并查集+线段树合并

原题链接:https://www.luogu.com.cn/problem/P3224

目录

题意

P3224 [HNOI2012]永无乡 并查集+线段树合并

分析

和主席树操作有些像,查询区间K大,但这题又带上了修改,因此我们考虑用线段树合并。直接开n个权值线段树,然后用并查集维护当前连通块。由于这题比较模板,就不多解释了。

Code

#include <bits/stdc++.h>
#define lowbit(i) i & -i
#define Debug(x) cout << x << endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
const ll INF = 1e18;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
const int MOD = 998244353;
int a[N], rt[N], tot, belong[N*40], sum[N*40], ls[N*40], rs[N*40], num[N];
int fa[N], siz[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void modify(int &now, int l, int r, int pos) {
    if (!now) now = ++tot;
    sum[now]++;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (pos <= mid) modify(ls[now], l, mid, pos);
    else modify(rs[now], mid+1, r, pos);
    sum[now] = sum[ls[now]] + sum[rs[now]];
}
void merge(int &x, int y, int l, int r) {
    if (!x || !y) {x += y; return;}
    sum[x] += sum[y];
    int mid = (l + r) >> 1;
    merge(ls[x], ls[y], l, mid);
    merge(rs[x], rs[y], mid+1, r);
}
int query(int now, int l, int r, int k) {
    if (l == r) return num[l];
    int mid = (l + r) >> 1;
    if (sum[ls[now]] >= k) return query(ls[now], l, mid, k);
    else return query(rs[now], mid+1, r, k-sum[ls[now]]);
}
void solve() {
    int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        fa[i] = i; siz[i] = 1;
        modify(rt[i], 1, n, a[i]);
        num[a[i]] = i;
    }
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        u = find(u);
        v = find(v);
        merge(rt[v], rt[u], 1, n);
        siz[v] += siz[u];
        fa[u] = v;
    }

    int q; cin >> q; while (q--) {
        char op;
        int x, y; cin >> op >> x >> y;
        if (op == 'Q') {
            if (siz[find(x)] < y) cout << -1 << endl;
            else cout << query(rt[find(x)], 1, n, y) << endl;
        } else {
            x = find(x);
            y = find(y);
            merge(rt[y], rt[x], 1, n);
            siz[y] += siz[x];
            fa[x] = y;
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}
上一篇:永无乡「HNOI2012」


下一篇:P3225 [HNOI2012]矿场搭建 题解