求助一下LCT
查看原帖
求助一下LCT
84626
sss7020楼主2023/2/12 00:07

RT,第一个样例过了,第二个样例错了。初学LCT,对着题解看了几遍,不明白出了什么问题

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;

namespace lct{
    int ch[MAXN][2], siz[MAXN], fa[MAXN], lazy[MAXN], val[MAXN], s[MAXN];

    inline bool isroot(int u){return ch[fa[u]][0] != u && ch[fa[u]][1] != u;}
    inline void push_up(int u){
        siz[u] = siz[ch[u][0]] + siz[ch[u][1]];
        s[u] = s[ch[u][0]] ^ s[ch[u][1]] ^ val[u];
    }

    inline int get(int x){return ch[fa[x]][1] == x;}

    inline void tag(int u){
        swap(ch[u][0], ch[u][1]);
        lazy[u] ^= 1;
    }

    inline void push_down(int u){
        if(lazy[u]){
            if(ch[u][0]) tag(ch[u][0]);
            if(ch[u][1]) tag(ch[u][1]);
            lazy[u] = 0;
        }
    }

    inline void rotate(int x){
        int y = fa[x], z = fa[y], chk = get(x);
        if(!isroot(y)) ch[z][y == ch[z][1]] = x;
        ch[y][chk] = ch[x][chk ^ 1];
        if(ch[x][chk ^ 1]) fa[ch[x][chk ^ 1]] = y;
        ch[x][chk ^ 1] = y, fa[y] = x, fa[x] = z;
        push_up(y), push_up(x);
    }

    inline void update(int x){
        if(!isroot(x)) return update(fa[x]);
        push_down(x);
    }

    inline void splay(int x){
        update(x);
        for(register int f = fa[x]; f = fa[x], !isroot(x);  rotate(x)){
            if(!isroot(f)) rotate(get(x) == get(f) ? f : x);
        }
        push_up(x);
    }

    inline void access(int x){
        for(register int p = 0; x; p = x, x = fa[x]) splay(x), ch[x][1] = p, push_up(x);
    }

    inline void make_root(int p){
        access(p);
        splay(p);
        tag(p);
    }

    inline int findroot(int x){
        access(x);
        splay(x);
        while(ch[x][0]) push_down(x), x = ch[x][0];
        splay(x);
        return x;
    }

    inline void split(int x, int y){
        make_root(x);
        access(y);
		splay(y);
    }

    inline void link(int x, int y){
        make_root(x);
        if(findroot(y) != x) fa[x] = y;
    }

    inline void cut(int x, int y){
        make_root(x);
        if(findroot(y) == x && fa[y] == x && !ch[y][0]){
        	fa[y] = ch[x][1] = 0;
        	push_up(x);
		} 
    }
}

inline int read(){
    int x = 0,f = 1, c = getchar();
    while(!isdigit(c)) f = (c == '-' ? -f : f), c = getchar();
    while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
    return x * f;
}

int main(){
	int n = read(), m = read();
	for(register int i = 1; i <= n; ++i) lct::val[i] = lct::s[i] = read();
	while(m--){
		int opt = read(), x = read(), y = read();
		switch(opt){
			case 0: lct::split(x, y); printf("%d\n", lct::s[y]); break;
			case 1: lct::link(x, y); break;
			case 2: lct::cut(x, y); break;
			case 3: lct::splay(x); lct::val[x] = y; 
		}
	}
    return 0;
}
2023/2/12 00:07
加载中...