【BZOJ4530】大融合(Link-Cut Tree)

题面

讨厌权限题!!!

Loj链接

题目描述

小强要在 N个孤立的星球上建立起一套通信系统。这套通信系统就是连接 N个点的一个树。这个树的边是一条一条添加上去的。在某个时刻,一条边的负载就是它所在的当前能够联通的树上路过它的简单路径的数量。

例如,在上图中,现在一共有五条边。其中,(3,8)这条边的负载是 6,因为有六条简单路径 2−3−8, 2−3−8−7, 3−8, 3−8−7, 4−3−8, 4−3−8−72-3-8,\ 2-3-8-7,\ 3-8,\ 3-8-7,\ 4-3-8,\ 4-3-8-72−3−8, 2−3−8−7, 3−8, 3−8−7, 4−3−8, 4−3−8−7 路过了 (3,8)。

现在,你的任务就是随着边的添加,动态的回答小强对于某些边的负载的询问。

输入格式

第一行包含两个整数 N,QN,QN,Q,表示星球的数量和操作的数量。星球从 111 开始编号。

接下来的 QQQ 行,每行是如下两种格式之一:

A x y 表示在 xxx 和 yyy 之间连一条边。保证之前 xxx 和 yyy 是不联通的。

Q x y 表示询问 (x,y)(x,y)(x,y) 这条边上的负载。保证 xxx 和 yyy 之间有一条边。

输出格式

对每个查询操作,输出被查询的边的负载。

样例

样例输入

8 6

A 2 3

A 3 4

A 3 8

A 8 7

A 6 5

Q 3 8

样例输出

6

数据范围与提示

对于所有数据,\(1≤N,Q≤100000\)

题解

\(Link-Cut\ Tree\)维护子树信息

不仅仅维护实儿子

还需要维护虚儿子

就相当于在原树中的儿子数量啦

我也不知道为什么

。。。

以后知道了就补一下

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. using namespace std;
  12. #define MAX 120000
  13. #define lson (t[x].ch[0])
  14. #define rson (t[x].ch[1])
  15. inline int read()
  16. {
  17. int x=0,t=1;char ch=getchar();
  18. while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
  19. if(ch=='-')t=-1,ch=getchar();
  20. while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
  21. return x*t;
  22. }
  23. struct Node
  24. {
  25. int ch[2],ff;
  26. int rev,size,sum;
  27. }t[MAX];
  28. int S[MAX],top;
  29. int n,Q;
  30. bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
  31. void pushup(int x){t[x].sum=t[lson].sum+t[rson].sum+t[x].size+1;}
  32. void rotate(int x)
  33. {
  34. int y=t[x].ff,z=t[y].ff;
  35. int k=t[y].ch[1]==x;
  36. if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
  37. t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
  38. t[x].ch[k^1]=y;t[y].ff=x;
  39. pushup(y);pushup(x);
  40. }
  41. void pushdown(int x)
  42. {
  43. if(!t[x].rev)return;
  44. swap(lson,rson);
  45. if(lson)t[lson].rev^=1;
  46. if(rson)t[rson].rev^=1;
  47. t[x].rev^=1;
  48. }
  49. void Splay(int x)
  50. {
  51. S[top=1]=x;
  52. for(int i=x;!isroot(i);i=t[i].ff)S[++top]=t[i].ff;
  53. while(top)pushdown(S[top--]);
  54. while(!isroot(x))
  55. {
  56. int y=t[x].ff,z=t[y].ff;
  57. if(!isroot(y))
  58. (t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
  59. rotate(x);
  60. }
  61. }
  62. void access(int x){for(int y=0;x;y=x,x=t[x].ff)Splay(x),t[x].size+=t[rson].sum-t[y].sum,t[x].ch[1]=y,pushup(x);}
  63. void makeroot(int x){access(x);Splay(x);t[x].rev^=1;}
  64. void split(int x,int y){makeroot(x);access(y);Splay(y);}
  65. void cut(int x,int y){split(x,y);t[y].ch[0]=t[x].ff=0;pushup(y);}
  66. void link(int x,int y){makeroot(x);makeroot(y);t[x].ff=y;t[y].size+=t[x].sum;pushup(y);}
  67. int findroot(int x){makeroot(x);while(lson)x=lson;return x;}
  68. int main()
  69. {
  70. n=read();Q=read();
  71. char ch[2];int u,v;
  72. while(Q--)
  73. {
  74. scanf("%s",ch);u=read(),v=read();
  75. if(ch[0]=='A')link(u,v);
  76. else split(u,v),printf("%lld\n",1ll*t[u].sum*(t[v].sum-t[u].sum));
  77. }
  78. return 0;
  79. }

【BZOJ4530】大融合(Link-Cut Tree)的更多相关文章

  1. [BJOI2014]大融合(Link Cut Tree)

    [BJOI2014]大融合(Link Cut Tree) 题面 给出一棵树,动态加边,动态查询通过每条边的简单路径数量. 分析 通过每条边的简单路径数量显然等于边两侧节点x,y子树大小的乘积. 我们知 ...

  2. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  3. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  4. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  5. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

  6. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  7. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  8. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  9. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

随机推荐

  1. Java基础点滴

    1. 关于interface的定义 [修饰符] interface 接口名 [extends 父接口名列表]{ [public] [static] [final] 常量;[public] [abstr ...

  2. mac攻略(1) -- 简单配置php开发环境

    [http://www.cnblogs.com/redirect/p/6112154.html]   最简单直接的方式还是使用 Mac 上自带的 Apache 和 PHP.   1.启动 Apache ...

  3. configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.

    Apache在2.4版本以后,编译时: # ./configure \ --prefix=/usr/local/apache2 \ --with-included-apr \ --enable-so ...

  4. 洛谷 P2194 HXY烧情侣【Tarjan缩点】 分析+题解代码

    洛谷 P2194 HXY烧情侣[Tarjan缩点] 分析+题解代码 题目描述: 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里 ...

  5. 让Python输出更漂亮

    print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="": student_age = 18 print("学生的年龄为:", stude ...

  6. Java经典编程题50道之四十四

    求0~7所能组成的奇数个数.分析:组成1位数是4个,组成2位数是7*4个,组成3位数是7*8*4个,组成4位数是7*8*8*4个…… public class Example44 {    publi ...

  7. ElasticSearch和solr的差别

    Elasticsearch简介 Elasticsearch是一个实时分布式搜索和分析引擎.它让你以前所未有的速度处理大数据成为可能.它用于全文搜索.结构化搜索.分析以及将这三者混合使用:维基百科使用E ...

  8. 回归模型效果评估系列1-QQ图

    (erbqi)导语 QQ图全称 Quantile-Quantile图,也就是分位数-分位数图,简单理解就是把两个分布相同分位数的值,构成点(x,y)绘图:如果两个分布很接近,那个点(x,y)会分布在y ...

  9. Action里面的自带的字段的含义

  10. HDU - 1248 寒冰王座 数学or暴力枚举

    思路: 1.暴力枚举每种面值的张数,将可以花光的钱记录下来.每次判断n是否能够用光,能则输出0,不能则向更少金额寻找是否有能够花光的.时间复杂度O(n) 2.350 = 200 + 150,买350的 ...