洛谷:P3690 【模板】Link Cut Tree (动态树)

  1. /*诸多细节,不注意就会调死去! 见注释。*/
  2. #include<cstdio>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int MAXN=;
  7. int n,m;
  8. int fa[MAXN],rev[MAXN],val[MAXN],xr[MAXN],Q[MAXN],ch[MAXN][];
  9. bool isroot(int x){ return ch[fa[x]][]!=x && ch[fa[x]][]!=x; }
  10. void Update(int x){ xr[x]=xr[ch[x][]]^xr[ch[x][]]^val[x]; }
  11. bool get(int x){ return ch[fa[x]][]==x ;}
  12. void Down(int x){ if(rev[x]){ rev[ch[x][]]^=; rev[ch[x][]]^=; rev[x]^=; swap(ch[x][],ch[x][]); } }
  13. void Rotate(int x)
  14. {
  15. int old=fa[x],oldf=fa[old],op=get(x);
  16. if(!isroot(old)) ch[oldf][ch[oldf][]==old]=x; //这一条一定要放在改变父子关系之前!在纯Splay中是放在后面的,因为直接看oldf是否为0可知old是否为根。
  17. ch[old][op]=ch[x][op^]; fa[ch[x][op^]]=old; //但这里使用isroot,改变之后就不能判断了!
  18. ch[x][op^]=old; fa[old]=x; fa[x]=oldf;
  19. Update(old); Update(x);
  20. }
  21. void Splay(int x)
  22. {
  23. int tp=; Q[]=x;
  24. for(int i=x;!isroot(i);i=fa[i]) Q[++tp]=fa[i]; //对于LCT的判断是否是根节点,需要使用isroot,在纯Splay中使用的是fa,不要搞混!
  25. for(int i=tp;i;i--) Down(Q[i]);
  26. for(int FA; !isroot(x) ; Rotate(x))
  27. {
  28. FA=fa[x];
  29. if(!isroot(FA))
  30. Rotate(get(x)==get(FA)?FA:x);
  31. }
  32. }
  33. void Access(int x){ int t=; while(x){ Splay(x); ch[x][]=t; Update(x); t=x; x=fa[x]; } } //记得Update
  34. void Makeroot(int x){ Access(x); Splay(x); rev[x]^=;}
  35. void Link(int x,int y){ Makeroot(x); fa[x]=y; }
  36. void Cut(int x,int y){ Makeroot(x); Access(y); Splay(y); if(ch[y][]==x) fa[x]=ch[y][]=;}
  37. int Find(int x){ Access(x); Splay(x); while(ch[x][]) x=ch[x][]; return x; }
  38. int main()
  39. {
  40. scanf("%d%d",&n,&m);
  41. for(int i=;i<=n;i++) scanf("%d",&val[i]) , xr[i]=val[i];
  42. while(m--)
  43. {
  44. int x,y,op;
  45. scanf("%d%d%d",&op,&x,&y);
  46. if(op==){ Makeroot(x); Access(y); Splay(y); printf("%d\n",xr[y]); }
  47. if(op==){ if(Find(x)!=Find(y)) Link(x,y); }
  48. if(op==){ Cut(x,y); }
  49. if(op==){ Makeroot(x); val[x]=y; Update(x); }
  50. }
  51. return ;
  52. }

LCT 动态树 模板的更多相关文章

  1. [HNOI2010]弹飞绵羊 (平衡树,LCT动态树)

    题面 题解 因为每个点都只能向后跳到一个唯一的点,但可能不止一个点能跳到后面的某个相同的点, 所以我们把它抽象成一个森林.(思考:为什么是森林而不是树?) 子节点可以跳到父节点,根节点再跳就跳飞了. ...

  2. Fzu Problem 2082 过路费 LCT,动态树

    题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528    Submit: 1654Time Limit ...

  3. LCT动态树入门

    LCT,link-cut-tree,一种基于splay的高级数据结构,常用于维护动态森林问题,但ta只能维护子树信息,无法修改子树信息. 首先,如果你不会splay,来这里看看吧. 接下来步入正题. ...

  4. Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 5020  Solved: 1872[Submit][Status ...

  5. Hdu 3966-Aragorn's Story LCT,动态树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966 Aragorn's Story Time Limit: 10000/3000 MS (Java/Ot ...

  6. Hdu 4010-Query on The Trees LCT,动态树

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  7. 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 12266  Solved: 4945[Submit ...

  8. HDU4010 Query on The Trees (LCT动态树)

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  9. Hdu 2475-Box LCT,动态树

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 从S3中导入数据到Dynamodb

    本节如果你已经从Dynamodb中导出过数据,而且导出的文件以及被存入S3.文件内部结构会在Verify Data Export File 中描写叙述. 我们称之前导出数据的原始表为source ta ...

  2. 安装Ubuntn 和 pycharm

    Ubuntu安装之python开发   什么??公司要用Ubuntu(乌班图)?不会用??怎么进行python开发??? 乌班图操作系统下载地址:http://releases.ubuntu.com/ ...

  3. Erlang function guards NOTE

    Note: I've compared , and ; in guards to the operators andalso and orelse. They're not exactly the s ...

  4. Exam 70-762 Developing SQL Databases

    这个考试还是很有用的,教了很多有用的东西,不错,虽然考试需要很多钱,不过值的尝试.虽然用了sql server 这么多年但是对于事务.多并发的优化还是处于小学生的水平,通过这次考试争取让自己提一个档次 ...

  5. HZNU 2154 ldh发奖金【字符串】

    题目链接 http://acm.hznu.edu.cn/OJ/problem.php?id=2154 思路 先判断不能拆分的情况 以为需要拆分成两个正整数 所以我们可以知道 只有个位的数字 是不能够拆 ...

  6. spring事件广播

    可参考:http://www.cnblogs.com/atyou/archive/2013/01/07/2850106.html 其中的类图更是精彩,现截至如下:

  7. signal函数理解或者void (*signal(int signum,void(*handler)(int)))(int)理解

    把void (*signal(int signum,void(*handler)(int)))(int)分成两部分: typedef void (*sighandler_t)(int); sighan ...

  8. Spring Boot缓存源码分析

    前言 项目里面要增加一个应用缓存,原本想着要怎么怎么来整合ehcache和springboot,做好准备配置这个配置那个,结果只需要做三件事: pom依赖 写好一个ehcache的配置文件 在boot ...

  9. 动态区间第K大

    整体二分. 主要需要注意的一点是,对于每个删除操作,若删除操作被算入贡献,则最开始的插入操作也一定会被算入,所以不必担心删除删错. #include<cstdio> #include< ...

  10. javascrip中ajax

    移动端对加载速度要求比较高,由于jquery插件有270多k,无形中增加加载的速度,下面整理一下原生js中ajax: 先了解ajax的基础知识 (1)XMLHttpRequest 对象 XMLHttp ...