BZOJ 3673 可持久化并查集 可持久化线段树

嘛。。可持久化数组实现可持久化并查集。。

可持久化数组用可持久化线段树实现。。

专门写了个包装好了的可持久化数组模板可用。

自认为写法还是能看的。。完全将所有操作包装成了数组了,就差实现operator[]。。

76ms挺慢的。。嘛,为了代码好看牺牲一点时间也值得。

路径压缩并无乱用。。毕竟可持久化线段树的修改是logn的,这样find就成(logn)^2了?

#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
const int M = 200001;
class PersistantArray {
    struct Node {
        int v;
        Node *lc, *rc;
    } *a[M];
    Node pool[M * 20], *C;
    Node *new_node(Node *lc, Node *rc, int v) {
        C->lc = lc; C->rc = rc; C->v = v; return C++;
    }
    int version, last, n;

    Node *modify(Node *p, int l, int r, int pos, int val) {
        int mid = l + r >> 1;
        if (l == r)
            return new_node(NULL, NULL, val);
        else if (pos <= mid)
            return new_node(modify(p->lc, l, mid, pos, val), p->rc, 0);
        else
            return new_node(p->lc, modify(p->rc, mid + 1, r, pos, val), 0);
    }
    
    int get(Node *p, int l, int r, int pos) {
        int mid = l + r >> 1;
        if (l == r)
            return p->v;
        else if (pos <= mid)
            return get(p->lc, l, mid, pos);
        else
            return get(p->rc, mid + 1, r, pos);
    }

public:
    PersistantArray(int initial_capacity, int initial_data)
        : C(pool), version(0), last(0) {
        a[0] = C++; a[0]->lc = a[0]->rc = a[0]; a[0]->v = initial_data;
        n = initial_capacity;
    }
    void roll(int new_version) { version = new_version; }
    int latest_version() { return last; }
    int get(int x) { return get(a[version], 1, n, x); }
    void set(int x, int y) { a[++last] = modify(a[version], 1, n, x, y); }
} *fa, *sz;
int now = 0, ver[M];
int find(int x) {
    int y;
    fa->roll(now);
    while (x) x = fa->get(y = x);
    return y;
}
void merge(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    sz->roll(now); fa->roll(now);
    int sx = sz->get(x), sy = sz->get(y);
    if (sx < sy) swap(x, y), swap(sx, sy);
    fa->set(y, x); sz->set(x, sx + sy);
    now = fa->latest_version();
}

int main() {
    int i, p, x, y, n, m;
    scanf("%d%d", &n, &m);
    fa = new PersistantArray(n, 0);
    sz = new PersistantArray(n, 1);
    for (int i = 1; i <= m; i++) {
        scanf("%d", &p);
        switch (p) {
        case 1: scanf("%d%d", &x, &y); merge(x, y); break;
        case 2: scanf("%d", &x); now = ver[x]; break;
        case 3: scanf("%d%d", &x, &y); printf("%d\n", find(x) == find(y)); break;
        }
        ver[i] = now;
    }

    return 0;
}

3673: 可持久化并查集 by zky

Time Limit: 5 Sec   Memory Limit: 128 MB
Submit: 958   Solved: 422
[ Submit][ Status][ Discuss]

Description

n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0

0<n,m<=2*10^4

Input

Output

Sample Input

5 6
1 1 2
3 1 2
2 0
3 1 2
2 1
3 1 2

Sample Output

1
0
1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值