1、题目大意:动态树问题,点修改,链查询xor和

裸题一道。。
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
namespace LinkCutTree{
    struct Node{
        Node *ch[2], *fa;
        int sum, num;
        bool rev;

        inline int which();

        inline void reverse(){
            if(this) rev ^= 1;
        }

        inline void pd(){
            if(rev){
                swap(ch[0], ch[1]);
                ch[0] -> reverse();
                ch[1] -> reverse();
                rev = false;
            }
        }

        inline void maintain(){
            sum = num ^ ch[0] -> sum ^ ch[1] -> sum;
        }

        Node();
    } *null = new Node, tree[300010], *pos[300010];

    Node::Node(){
        num = sum = 0;
        rev = false;
        ch[0] = ch[1] = fa = null;
    }

    inline int Node::which(){
        if(fa == null || (this != fa -> ch[0] && this != fa -> ch[1])) return -1;
        return this == fa -> ch[1];
    }

    inline void rotate(Node *o){
        Node *p = o -> fa;
        int l = o -> which(), r = l ^ 1;
        o -> fa = p -> fa;
        if(p -> which() != -1) p -> fa -> ch[p -> which()] = o;
        p -> ch[l] = o -> ch[r];
        if(o -> ch[r]) o -> ch[r] -> fa = p;
        o -> ch[r] = p; p -> fa = o;
        o -> ch[r] -> maintain();
        o -> maintain();
    }

    inline void splay(Node *o){
        static stack<Node*> st;
        if(!o) return;
        Node *p = o;
        while(1){
            st.push(p);
            if(p -> which() == -1) break;
            p = p -> fa;
        }
        while(!st.empty()){
            st.top() -> pd(); st.pop();
        }

        while(o -> which() != -1){
            p = o -> fa;
            if(p -> which() != -1){
                if(p -> which() ^ o -> which()) rotate(o);
                else rotate(p);
            }
            rotate(o);
        }
    }

    inline void Access(Node *o){
        Node *y = null;
        while(o != null){
            splay(o);
            o -> ch[1] = y;
            o -> maintain();
            y = o; o = o -> fa;
        }
    }

    inline void MovetoRoot(Node *o){
        Access(o);
        splay(o);
        o -> reverse();
    }

    inline Node* FindRoot(Node *o){
        Access(o);
        splay(o);
        while(o -> ch[0] != null) o = o -> ch[0];
        return o;
    }

    inline void Link(Node *x, Node *y){
        MovetoRoot(x);
        x -> fa = y;
    }

    inline void Cut(Node *x, Node *y){
        MovetoRoot(x);
        Access(y);
        splay(y);
        y -> ch[0] = x -> fa = null;
        y -> maintain();
    }
}
int main(){
    using namespace LinkCutTree;
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i ++){
        int x; scanf("%d", &x);
        pos[i] = &tree[i];
        pos[i] -> num = pos[i] -> sum = x;
    }
    int op, x, y;
    while(m --){
        scanf("%d%d%d", &op, &x, &y);
        if(op == 0){
            MovetoRoot(pos[x]);
            Access(pos[y]);
            splay(pos[y]);
            printf("%d\n", pos[y] -> sum);
        }
        else if(op == 1){
            if(FindRoot(pos[x]) != FindRoot(pos[y])) Link(pos[x], pos[y]);
        }
        else if(op == 2){
            if(FindRoot(pos[x]) == FindRoot(pos[y])) Cut(pos[x], pos[y]);
        }
        else{
            Access(pos[x]);
            splay(pos[x]);
            pos[x] -> num = y;
            pos[x] -> maintain();
        }
    }
    return 0;
}

BZOJ3282——Tree的更多相关文章

  1. BZOJ3282: Tree

    传送门 又是权限题= =,过了NOIp我就要去当一只权限狗! LCT裸题,get到了两个小姿势. 1.LCA操作应该在access中随时updata 2.Link操作可以更简单 void Link(i ...

  2. BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)

    蛋蛋用链剖A的,我写的LCT 3282: Tree Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1241 Solved: 542 [Submit][ ...

  3. [bzoj3282]Tree (lct)

    昨天看了一天的lct..当然幸好最后看懂了(也许吧..) 论善良学长的重要性T_T,老司机带带我! 这题主要是删边的时候还要判断一下..蒟蒻一开始天真的以为存在的边才能删结果吃了一发wa... 事实是 ...

  4. BZOJ3282: Tree (LCT模板)

    Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...

  5. 【学习心得】Link-cut Tree

    Link-cut Tree是一种支持改变树(森林)的形态(link和cut),同时维护树的路径上节点信息的数据结构.lct通过splay来维护每次的perferred path,说白了就是一个动态的树 ...

  6. AHOI2018训练日程(3.10~4.12)

    (总计:共90题) 3.10~3.16:17题 3.17~3.23:6题 3.24~3.30:17题 3.31~4.6:21题 4.7~4.12:29题 ZJOI&&FJOI(6题) ...

  7. 【BZOJ3282】Tree (Link-Cut Tree)

    [BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...

  8. 【BZOJ3282】Tree LCT

    1A爽,感觉又对指针重怀信心了呢= =,模板题,注意单点修改时splay就好,其实按吾本意是没写的也A了,不过应该加上能更好维护平衡性. ..还是得加上好= = #include <iostre ...

  9. 【bzoj3282】Tree

    LCT模板题: 话说xor和的意思是所有数xor一下: #include<iostream> #include<cstdio> #include<cstring> ...

随机推荐

  1. 忘记常访问网站密码怎么办?教你如何查看浏览器已保存的密码,如何简单查看Chome浏览器保存的密码?

    利用场景: 同事或朋友外出有事,电脑未锁屏离开座位.可以利用这一间隙,查看Ta在Chrome浏览器上保存的账号密码 查看逻辑: 当我们要查看Chrome浏览器上保存的密码时,点击显示,会弹出一个对话框 ...

  2. css011 表格和表单的格式化

    css011 表格和表单的格式化 一.    让表格专司其职    Html中创建一个三行三列的表格 <table> <caption align="bottom" ...

  3. ci查询数据库

    public function getUsers() { $this->load->database();//载入数据库 $users = $this->db->get(&qu ...

  4. 10月21日上午MySQL数据库学习内容复习

    1.创建数据库create database 数据库名称删除数据库drop database 数据库名称 2.创建表create table 表名(列名 类型(长度) 自增长 主键 非空,)自增长:a ...

  5. winSocket数据传输

    服务器端: #include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib" ...

  6. Codeforces 697A - Pineapple Incident

    题目链接:http://codeforces.com/problemset/problem/697/A 题目大意: 输入三个数 t,s,x; 判断x是否合适 合适的位置位 t , t+s, t+s+1 ...

  7. 初识Python类

    吐槽:学习面向对象就像你追一个女神一样,刚刚有点感觉了,过几天又陷入绝望的感觉,很蛋疼. 类的语法 class Person(object): print("learning class&q ...

  8. array_filter移除空数组

    $arr = array( "0"=> "564645", "1"=>"", "2"=& ...

  9. 性能工具MiniProfiler在Asp.Net WebForm跟踪EntityFramework

    http://www.xuebuyuan.com/356638.html 选择MiniProfiler.EF 默认会把MiniProfiler安装上,笔者程序是Asp.Net WebForm 不需要安 ...

  10. Java并发编程核心方法与框架-TheadPoolExecutor的使用

    类ThreadPoolExecutor最常使用的构造方法是 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAli ...