【题目分析】

明显的LCT维护连通性的题目。

access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点。

而且需边的思想也很巧妙,保证了复杂度。

但是只能用于修改路径上的点的权值,不能用于修改整棵子树的信息。

相比Splay,只能说是阉割了修改信息的作用,而增加了删边,加边的操作。

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

#define maxn 1000005
#define inf (0x3f3f3f3f)

int read()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

int ch[maxn][2],fa[maxn],num[maxn],xs[maxn],st[maxn],top=0,rev[maxn],n,m,sta[maxn];

void update(int k)
{xs[k]=num[k]^xs[ch[k][0]]^xs[ch[k][1]];}

bool isroot(int k)
{return ch[fa[k]][0]!=k&&ch[fa[k]][1]!=k;}

void pushdown(int k)
{
    if (rev[k])
    {
        rev[k]^=1;
        rev[ch[k][0]]^=1;
        rev[ch[k][1]]^=1;
        swap(ch[k][0],ch[k][1]);
    }
}

void rot(int x)
{
    int y=fa[x],z=fa[y],l,r;
    if (ch[y][0]==x) l=0; else l=1;
    r=l^1;
    if (!isroot(y))
    {
        if (ch[z][0]==y) ch[z][0]=x;
        else ch[z][1]=x;
    }
    fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
    ch[y][l]=ch[x][r]; ch[x][r]=y;
    update(y); update(x);
}

void splay(int x)
{
    top=0; sta[++top]=x;
    for (int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
    while (top) pushdown(sta[top--]);
    while (!isroot(x))
    {
        int y=fa[x],z=fa[y];
        if (!isroot(y))
        {
            if (ch[y][0]==x^ch[z][0]==y) rot(y);
            else rot(x);
        }
        rot(x);
    }
}

void access(int x)
{
    for (int t=0;x;t=x,x=fa[x])
        splay(x),ch[x][1]=t,update(x);
}

void makeroot(int x)
{
    access(x); splay(x); rev[x]^=1;
}

int find(int x)
{
    access(x);
    splay(x);
    while (ch[x][0]) x=ch[x][0];
    return x;
}

void cut(int x,int y)
{
    makeroot(x);
    access(y);
    splay(y);
    if (ch[y][0]==x) ch[y][0]=fa[x]=0;
}

void link(int x,int y)
{
    makeroot(x);
    fa[x]=y;
}

int main()
{
    n=read(); m=read();
    for (int i=1;i<=n;++i) xs[i]=num[i]=read();
    int opt,x,y;
    while (m--)
    {
        opt=read();x=read();y=read();
        switch (opt)
        {
            case 0:makeroot(x); access(y); splay(y); printf("%d\n",xs[y]); break;
            case 1:if (find(x)!=find(y)) link(x,y); break;
            case 2:if (find(x)==find(y)) cut(x,y); break;
            case 3:access(x); splay(x); num[x]=y; update(x); break;
        }
    }
}

  

BZOJ 3282 Tree ——KD-Tree的更多相关文章

  1. BZOJ 2648 / 2716 K-D Tree 模板题

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  2. BZOJ 3282 Link Cut Tree (LCT)

    题目大意:维护一个森林,支持边的断,连,修改某个点的权值,求树链所有点点权的异或和 洛谷P3690传送门 搞了一个下午终于明白了LCT的原理 #include <cstdio> #incl ...

  3. [模板] K-D Tree

    K-D Tree K-D Tree可以看作二叉搜索树的高维推广, 它的第 \(k\) 层以所有点的第 \(k\) 维作为关键字对点做出划分. 为了保证划分均匀, 可以以第 \(k\) 维排名在中间的节 ...

  4. 浅谈K-D Tree

    初步认识\(K-D\) \(Tree\) \(K-D\) \(Tree\)是一种基于空间分割的二叉树形数据结构,一般用于高维信息检索.因为\(OI\)中很多问题都能转化为高维信息检索,所以\(K-D\ ...

  5. P4169-CDQ分治/K-D tree(三维偏序)-天使玩偶

    P4169-CDQ分治/K-D tree(三维偏序)-天使玩偶 这是一篇两种做法都有的题解 题外话 我写吐了-- 本着不看题解的原则,没写(不会)K-D tree,就写了个cdq分治的做法.下面是我的 ...

  6. BZOJ 3489: A simple rmq problem(K-D Tree)

    Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 2579  Solved: 888[Submit][Status][Discuss] Descripti ...

  7. BZOJ 3053: The Closest M Points(K-D Tree)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1235  Solved: 418[Submit][Status][Discuss] Descripti ...

  8. BZOJ 4520: [Cqoi2016]K远点对(k-d tree)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1162  Solved: 618[Submit][Status][Discuss] Descripti ...

  9. BZOJ 1941: [Sdoi2010]Hide and Seek(k-d Tree)

    Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 1712  Solved: 932[Submit][Status][Discuss] Descripti ...

  10. BZOJ 2648: SJY摆棋子(K-D Tree)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 6051  Solved: 2113[Submit][Status][Discuss] Descript ...

随机推荐

  1. linux下mysql开启关和重启

    开启: /etc/init.d/mysql start关闭: /etc/init.d/mysql stop重启: /etc/init.d/mysql restart 查看字符集show variabl ...

  2. myeclipse破解

    由于内容比较多,我就直接转载了 ,同时感谢原博主 http://blog.itpub.net/27042095/viewspace-1164998/

  3. 清除Windows系统桌面快捷方式小箭头

    清除Windows桌面快捷方式小箭头,需要重启,且不会导致软件无法锁定到任务栏.新建.reg的注册表文件,命名随意,内容如下: Windows Registry Editor Version 5.00 ...

  4. MetaWeblog博客客户端工具之Windows Live Writer

    吐槽&注意的坑: 刚听说了有这么一个东西,据说Windows Live Writer开源之后就改名为Open Live Writer,我以为Open Live Writer就要比Windows ...

  5. 转:不再以讹传讹,GET和POST的真正区别

    如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这个问题.我说GET是用于获取数据的,POST,一般用于将数据发给服务器之用. 这个答案好像并不是他想要的.于是他继 ...

  6. 聊聊SOA面向服务架构

    什么是SOA SOA(Service-Oriented Architecture),即面向服务的架构.SOA是一种粗粒度.松耦合服务架构,服务之间通过简单.精确定义接口进行通讯,不涉及底层编程接口和通 ...

  7. 重温WCF之消息契约(MessageContract)(六)

    对于SOAP来说主要由两部分构成Header和Body,他们两个共同构成了SOAP的信封,通常来说Body保存具体的数据内容,Header保存一些上下文信息或关键信息.比如:在一些情况下,具有这样的要 ...

  8. HDU1899 Sum the K-th's(树状数组)

    枚举,每次增加点,删除点 #include<cstdio> #include<iostream> #include<cstdlib> #include<cst ...

  9. MD5与Base64的思考

    MD5加密是对任意长的数据使用MD5哈稀算法散列为4个32位组,若格式化为ASCII字符则为16字符,若格式化16进制表示,则为32字符.  (MD5的具体算法请参阅相关书籍和资料) MD5广泛用于数 ...

  10. git 本地仓库和远程仓库及本地分支和远程分支

    从远程git仓库签出代码:   $ git clone git://aaa.com/git_project.git (远程git服务器项目所在地址) 当你需要克隆远程项目到本地时,默认会把项目保存在名 ...