bzoj 3674 可持久化并查集加强版

传送门:http://hzwer.com/3997.html(题目在里头)
思路:一直想搞这玩意,,,结果呢,,不会做,上网看了一下题解,,用什么可持久化数组?哦,,要用主席树维护一下,,貌似很简单啊,,自己YY一下写一发代码,,WA,,查错无果,,对拍无果,,最后不得已看了hzw题解什么数组开小了所以他T了,,我想切,,,我会那么傻逼开小了吗?,,结果最后改了好久失望的改了数组,,,就轻松A了,,心里是崩溃的,,为啥他T我WA,,,?
代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#define maxn 10000000
#define N 200000
using namespace std;
struct node { int deep,data; int son[2];};
node A[maxn+5];
int n,m,cnt,root[(N<<1)+5]; 
bool lastans;
inline int getnum(){
    char c; int num;
    while (!isdigit(c = getchar()));
    num = c - '0';
    while (isdigit(c = getchar())) num = 10 * num + c -'0';
    return num;
}

inline void build(int l,int r,int &rt){
    int mid = (l + r)>>1;
    rt = ++cnt;
    if (l == r) {  A[rt].data = l; A[rt].deep = 1; return;}
    build(l,mid,A[rt].son[0]);
    build(mid+1,r,A[rt].son[1]);
}

void init(){
    n = getnum(); m = getnum(); cnt = 0;
    build(1,n,root[0]);
}

inline int query(int l,int r,int rt,int x){
    int mid = (l + r)>>1;
    if (l == r) return rt;
    if (x <= mid) return query(l,mid,A[rt].son[0],x);
    if (x > mid) return query(mid + 1,r,A[rt].son[1],x); 
}

inline int find(int x,int rot){
    int fa = query(1,n,rot,x);
    if (A[fa].data == x) return fa;
    return find(A[fa].data,rot);
}

inline void change1(int last,int l,int r,int &rt,int a,int b){
    rt = ++cnt;
    int mid = (l + r)>>1;
    A[rt] = A[last];
    if (l == r) { A[rt].data = b; return; }
    if (a <= mid) change1(A[last].son[0],l,mid,A[rt].son[0],a,b);
    if (a > mid) change1(A[last].son[1],mid + 1,r,A[rt].son[1],a,b); 
} 

inline void change2(int last,int l,int r,int &rt,int x){
    rt = ++cnt;
    int mid = (l + r)>>1;
    A[rt] = A[last];
    if (l == r) { A[rt].deep++; return; }
    if  (x <= mid) change2(A[last].son[0],l,mid,A[rt].son[0],x);
    if (x > mid) change2(A[last].son[1],mid + 1,r,A[rt].son[1],x);
}

inline void make_it(int i){
    int a = getnum()^lastans,b = getnum()^lastans;
    root[i] = root[i - 1];
    int x = find(a,root[i]),y = find(b,root[i]);
    if (A[x].data != A[y].data){
    if (A[x].deep > A[y].deep) swap(x,y);
    change2(root[i],1,n,root[i],A[y].data);
    change1(root[i],1,n,root[i],A[x].data,A[y].data);}
}

void DO_IT(){
    int op;
    lastans = 0;
    for (int i = 1;i <= m; ++i){
        op = getnum();
        if (op == 1) make_it(i);
        if (op == 2) root[i] = root[getnum()^lastans];
        if (op == 3) {
            int a = getnum()^lastans,b = getnum()^lastans;
            root[i] = root[i-1];
            bool t =(A[find(a,root[i])].data == A[find(b,root[i])].data);
            printf("%d\n",t);
            lastans = t;
        }
    }
}

int main(){
    init();
    DO_IT();
    return 0;
} 

你可能感兴趣的:(bzoj 3674 可持久化并查集加强版)