zoukankan      html  css  js  c++  java
  • LuoguP3690 【模板】Link Cut Tree (LCT)

    勉强算是结了个大坑吧或者才开始

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
    #define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
    #define ll long long
    #define u32 unsigned int
    #define u64 unsigned long long
     
    #define ON_DEBUGG
     
    #ifdef ON_DEBUGG
     
    #define D_e_Line printf("
    ----------
    ")
    #define D_e(x) cout << (#x) << " : " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt", "r", stdin)
    #define FileSave() freopen("out.txt", "w", stdout)
    #include <ctime>
    #define TIME() fprintf(stderr, "
    time: %.3fms
    ", clock() * 1000.0 / CLOCKS_PER_SEC)
    
    #else
     
    #define D_e_Line ;
    #define D_e(x) ;
    #define Pause() ;
    #define FileOpen() ;
    #define FileSave() ;
    #define TIME() ;
    //char buf[1 << 21], *p1 = buf, *p2 = buf;
    //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
     
    #endif
     
    using namespace std;
    struct ios{
        template<typename ATP>inline ios& operator >> (ATP &x){
            x = 0; int f = 1; char ch;
            for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
            while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
            x *= f;
            return *this;
        }
    }io;
     
    template<typename ATP>inline ATP Max(ATP a, ATP b){
        return a > b ? a : b;
    }
    template<typename ATP>inline ATP Min(ATP a, ATP b){
        return a < b ? a : b;
    }
    template<typename ATP>inline ATP Abs(ATP a){
        return a < 0 ? -a : a;
    }
    
    const int N = 300007;
    
    struct node {
        int ch[2], fa, val, sum;
        bool rev;
    }t[N];
    #define ls t[u].ch[0]
    #define rs t[u].ch[1]
    
    inline bool Ident(int u) {
        return t[t[u].fa].ch[1] == u;
    }
    
    inline bool IsRoot(int u) {
        return t[t[u].fa].ch[0] != u && t[t[u].fa].ch[1] != u;
    }
    
    inline void Pushup(int u) {
        t[u].sum = t[u].val ^ t[ls].sum ^ t[rs].sum;
    }
    
    inline void Pushrev(int u) {
        Swap(t[u].ch[0], t[u].ch[1]);
        t[u].rev ^= 1;
    }
    
    inline void Pushdown(int u) {
        if(!t[u].rev) return;
        if(t[u].ch[0]) Pushrev(t[u].ch[0]);
        if(t[u].ch[1]) Pushrev(t[u].ch[1]);
        t[u].rev = 0;
    }
    
    int sta[N], top;
    
    inline void Rotate(int x) {
    	int y = t[x].fa, z = t[y].fa, k = Ident(x);
    	t[x].fa = z; if(!IsRoot(y)) t[z].ch[Ident(y)] = x;
    	t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
    	t[x].ch[k ^ 1] = y, t[y].fa = x;
    	Pushup(y), Pushup(x);
    }
    
    inline void Splay(int u) {
    	int x = u;
        top = 0;
        while(!IsRoot(u)) {
        	sta[++top] = u;
        	u = t[u].fa;
    	}
    	sta[++top] = u;
    	nR(i,top,1){
    		Pushdown(sta[i]);
    	}
        while(!IsRoot(x)) {
        	int y = t[x].fa;
        	if(!IsRoot(y)){
        		Ident(x) == Ident(y) ? Rotate(y) : Rotate(x); 
    		}
    		Rotate(x);
    	}
    	Pushup(x);
    }
    
    inline void Access(int u) {
        for(int v = 0; u; v = u, u = t[u].fa){
            Splay(u);
            t[u].ch[1] = v;
            Pushup(u);
        }
    }
    
    inline void MakeRoot(int u) {
        Access(u);
        Splay(u);
        Pushrev(u);
    }
    
    inline int FindRoot(int u) {
        Access(u);
        Splay(u);
        while(ls) u = ls;
        Splay(u);
        return u;
    }
    
    inline void Split(int u, int v) {
        MakeRoot(u);
        Access(v);
        Splay(v);
    }
    
    inline void Link(int u, int v) {
        Split(u, v);
        t[u].fa = v;
    }
    
    inline void Cut(int u, int v){
        MakeRoot(u);
        if(FindRoot(v) == u && t[v].fa == u && !t[v].ch[0]){
        	t[v].fa = t[u].ch[1] = 0;
        	Pushup(u);
    	}
    }
    
    inline void Modify(int u, int w) {
    //    Access(u);
        Splay(u);
        t[u].val = w;
        Pushup(u);
    }
    
    int main() {
    //FileOpen();
    //FileSave();
    	int n, m;
    	io >> n >> m;
        R(i,1,n){
        	io >> t[i].val;
        	t[i].sum = t[i].val;
    	}
        while(m--){
        	int opt, x, y;
        	io >> opt >> x >> y;
            switch(opt){
    	        case 0:
    	            Split(x, y);
    	            printf("%d
    ", t[y].sum);
    	            break;
    	        case 1:
    	            Link(x, y);
    	            break;
    	        case 2:
    	            Cut(x, y);
    	            break;
    	        case 3:
    	            Modify(x, y);
    	            break;
    	        }
        }
        return 0;
    }
    
  • 相关阅读:
    第五次实验报告
    第四次实验报告
    [_UICascadingTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
    真机测试时出现 could not find developer disk image问题
    UItableview正在滚动的时候进行操作容易出问题
    NSArray NSMutableArray 初始化
    日志报错Can't add self as subview
    设置statusBar状态栏颜色
    网站视频url地址获取
    ios9 xcode7以后编译需要进行的几项设置
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11620706.html
Copyright © 2011-2022 走看看