直接向原树加子树是不可能的
考虑重新建立这样一颗树,我们称之为 S 树
将每次需要添加的子树看做一个点,称之为 S 点
新建的树就是由这些点构成的,
那么树的大小是合理的
初始节点为整棵原树
由于添加的子树的节点的编号一定是连续的一段区间
树上的每个节点维护 l, r, rt
分别表示 左端点, 右端点, 这棵子树的根在原树上的编号
定义
宏观树:只有 S 点构成的树 -> S 树
微观树:按照题目描述所形成的树,当然这棵树是不会建立出来的
所有的询问操作都需要微观树的信息
首先考虑对于任意合法的点 B, 如何找出它在原树上的对应点 b, 显然 b 的值域为 [1, n]
S 点满足 l, r 单调
因此首先二分出 B 点在那个 S 点中,
S 点代表的子树的编号是 [S.l, S.r]
所以 B 在该子树中的编号的大小次序为第 B - S.l + 1;
查询区间 k 大 => 主席树
这样的话只需对原树建立主席树
查询以 S.rt 为根的子树中编号第 B - S.l + 1 小的编号就是 b
Link_super 函数是核心
然后考虑查询时所要维护的东西
由于查询树上点对之间的距离,所以只需知道深度就可以解决
维护这么 3 种深度
deep[] 原树的深度
Super_dep[] S 树的深度(宏观树)
Big_deep[] 微观树的深度,当然并不去维护所有存在的点的深度
Big_deep[] 相当于是维护的宏观树,只不过所有 S 点的 Big_deep[] 都是微观树上 S 的真实深度值
这里可以这样理解:
若 Link_super(u, v), u 的值域 [1, n],
查询的 v 所在 S 树的 S 点的编号为 X;
S 的当前总数 + 1 为 Y;
这样的话 Big_deep[y] = Big_deep[x] + Dis(v, X.rt);
Dis 函数为查询原树上两点之间的距离
查询时分类讨论

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std; #define LL long long #define gc getchar()
inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = 5e5 + ; int n, m, q;
int head[N], cnt;
LL deep[N];
int Super_node;
LL total;
struct Node {int u, v, nxt;} G[N << ]; inline void Link(int u, int v) {
G[++ cnt].v = v, G[cnt].nxt = head[u], head[u] = cnt;
} int Hjtjs; struct NOde {
int fa[N], size[N], topp[N], tree[N], lst[N], rst[N], tree_js, son[N], bef[N];
int Lson[N << ], Rson[N << ], Size[N << ], Root[N]; void Dfs_1(int u, int F_, int depth) {
deep[u] = depth, fa[u] = F_, size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
if(G[i].v == F_) continue;
Dfs_1(G[i].v, u, depth + );
size[u] += size[G[i].v];
if(size[G[i].v] > size[son[u]]) son[u] = G[i].v;
}
} void Dfs_2(int u, int tp) {
topp[u] = tp, tree[u] = ++ tree_js, lst[u] = tree[u], bef[tree_js] = u;
if(!son[u]) {rst[u] = tree[u]; return ;}
Dfs_2(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt) {if(G[i].v != fa[u] && G[i].v != son[u]) Dfs_2(G[i].v, G[i].v);}
rst[u] = tree_js;
} void Fill(int x, int y) {Lson[x] = Lson[y], Rson[x] = Rson[y], Size[x] = Size[y];} void Insert(int &rt, int l, int r, int x) {
Fill(++ Hjtjs, rt);
rt = Hjtjs;
Size[rt] ++;
if(l == r) return ;
int mid = (l + r) >> ;
if(x <= mid) Insert(Lson[rt], l, mid, x);
else Insert(Rson[rt], mid + , r, x);
} void Build_tree() {
Dfs_1(, , );
Dfs_2(, );
for(int i = ; i <= n; i ++) {Root[i] = Root[i - ]; Insert(Root[i], , n, bef[i]);}
} inline int Lca(int x, int y) {
int tpx = topp[x], tpy = topp[y];
while(tpx != tpy) {
if(deep[tpx] < deep[tpy]) swap(x, y), swap(tpx, tpy);
x = fa[tpx], tpx = topp[x];
}
return deep[x] < deep[y] ? x : y;
} inline int Dis(int x, int y) {int lca = Lca(x, y); return deep[x] + deep[y] - * deep[lca];} int Sec_A(int ljd, int rjd, int l, int r, int k) {
if(l == r) return l;
int mid = (l + r) >> ;
int imp = Size[Lson[rjd]] - Size[Lson[ljd]];
if(k <= (Size[Lson[rjd]] - Size[Lson[ljd]])) return Sec_A(Lson[ljd], Lson[rjd], l, mid, k);
else return Sec_A(Rson[ljd], Rson[rjd], mid + , r, k - (Size[Lson[rjd]] - Size[Lson[ljd]]));
} } Tree; struct Node_ {
#define E exit(0) struct Node_3 {LL l, r; int rt;} Super_graph[N]; struct Pair {
// yvan shu zhong de bian hao, fei shu shang
int Bef_number;
// chao ji shu de dian de gen , shi yvan shu shang de dian de bian hao
int Super_rt;
// chao ji shu de dian de bian hao
int Super_number;
}; int Super_dep[N];
LL Big_deep[N];
int f[N][];
int fa[N]; Pair Get_information(LL x) {
int l = , r = Super_node, ans;
while(l <= r) {
int mid = (l + r) >> ;
if(x <= Super_graph[mid].r) ans = mid, r = mid - ;
else l = mid + ;
}
int nottree = Super_graph[ans].rt;
int imp = Tree.Sec_A(Tree.Root[Tree.lst[nottree] - ], Tree.Root[Tree.rst[nottree]], , n, x - Super_graph[ans].l + );
return (Pair) {imp, nottree, ans};
} void Link_Super(LL u, LL v) {
Super_node ++;
Pair Nodev = Get_information(v);
Super_graph[Super_node].l = total + , Super_graph[Super_node].r = total + Tree.size[u], Super_graph[Super_node].rt = u;
total += Tree.size[u];
Super_dep[Super_node] = Super_dep[Nodev.Super_number] + ;
int a = Nodev.Super_rt, b = Nodev.Bef_number;
Big_deep[Super_node] = Big_deep[Nodev.Super_number] + Tree.Dis(b, a) + ;
f[Super_node][] = Nodev.Super_number;
fa[Super_node] = Nodev.Bef_number;
for(int i = ; i <= ; i ++) f[Super_node][i] = f[f[Super_node][i - ]][i - ];
} int Lca(int x, int y) {
if(Super_dep[x] < Super_dep[y]) swap(x, y);
int del = Super_dep[x] - Super_dep[y];
for(int i = ; i <= ; i ++) if(( << i) & del) x = f[x][i];
if(x == y) return x;
for(int i = ; i >= ; i --) if(f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][];
} LL Ask(LL x, LL y) {
Pair Nodex = Get_information(x), Nodey = Get_information(y);
if(Super_dep[Nodex.Super_number] <= Super_dep[Nodey.Super_number]) swap(Nodex, Nodey);
if(Nodex.Super_number == Nodey.Super_number) return Tree.Dis(Nodex.Bef_number, Nodey.Bef_number);
else {
int lca = Lca(Nodex.Super_number, Nodey.Super_number);
LL ret;
if(lca == Nodey.Super_number) {
int g = Nodex.Super_number;
for(int i = ; i >= ; i --) if(f[g][i] && Super_dep[f[g][i]] > Super_dep[lca]) g = f[g][i];
ret = Big_deep[Nodex.Super_number] - Big_deep[g] + + Tree.Dis(Nodex.Super_rt, Nodex.Bef_number);
ret += Tree.Dis(fa[g], Nodey.Bef_number);
} else {
int a = Nodex.Super_number, b = Nodey.Super_number;
for(int i = ; i >= ; i --) {if(f[a][i] && Super_dep[f[a][i]] > Super_dep[lca]) a = f[a][i];}
for(int i = ; i >= ; i --) {if(f[b][i] && Super_dep[f[b][i]] > Super_dep[lca]) b = f[b][i];}
ret = Big_deep[Nodex.Super_number] - Big_deep[a] + + Big_deep[Nodey.Super_number] - Big_deep[b] + + Tree.Dis(fa[a], fa[b]);
ret += Tree.Dis(Nodex.Super_rt, Nodex.Bef_number) + Tree.Dis(Nodey.Super_rt, Nodey.Bef_number);
}
return ret;
}
}
#undef E
} S_graph; int main() {
n = read(), m = read(), q = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++) {int u = read(), v = read(); Link(u, v), Link(v, u);}
Tree.Build_tree();
total = n, Super_node = ;
S_graph.Super_dep[] = ;
S_graph.f[][] = ;
S_graph.Super_graph[].l = , S_graph.Super_graph[].r = n, S_graph.Super_graph[].rt = ;
for(int i = ; i <= m; i ++) {
LL x = read_LL(), y = read_LL();
S_graph.Link_Super(x, y);
}
for(int i = ; i <= q; i ++) {
LL x = read_LL(), y = read_LL();
cout << S_graph.Ask(x, y) << "\n";
} return ;
}

luogu 3248的更多相关文章

  1. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  2. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  3. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  4. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  5. Luogu 考前模拟Round. 1

    A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...

  6. luogu P2580 于是他错误的点名开始了

    luogu  P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...

  7. CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)

    CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...

  8. Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)

    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...

  9. Luogu 1962 斐波那契数列(矩阵,递推)

    Luogu 1962 斐波那契数列(矩阵,递推) Description 大家都知道,斐波那契数列是满足如下性质的一个数列: f(1) = 1 f(2) = 1 f(n) = f(n-1) + f(n ...

随机推荐

  1. [BZOJ4755][JSOI2016]扭动的回文串(manacher+Hash)

    前两种情况显然直接manacher,对于第三种,枚举回文中心,二分回文半径,哈希判断即可. #include<cstdio> #include<algorithm> #defi ...

  2. django.http.request中HttpRequest对象的一些属性与方法

    HttpRequest对象的属性 属性 描述 path 表示提交请求页面完整地址的字符串,不包括域名,如 "/music/bands/the_beatles/". method 表 ...

  3. Hadoop HA 搭建

    Hadoop HA 什么是 HA HA是High Available缩写,是双机集群系统简称,指高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节点及备用节点.通常 ...

  4. Qt定时器

    PS: 本案例使用的是Qt 4.8.4版本,不同版本代码可能会有差异. 第一步: // 重写此虚函数(继承自QObject) virtual void timerEvent(QTimerEvent* ...

  5. Json:Restful

    JArray & JObject JArray与JObject在json的应用:无需定义相应的类对象,直接解析 JArray jarr = JArray.Parse(jsonStr); //数 ...

  6. MySQL处理达到百万级数据时,如何优化?

    1.两种查询引擎查询速度(myIsam 引擎 ) InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算有多少行. ...

  7. java - day018 - 线程续

    生产者,消费者 线程间的通信模型 等待和通知 在生产者和消费者模型中 消费者暂停等待数据 生产者产生数据后发出通知 object 方法 wait(); notify(); 通知一个 notifyAll ...

  8. HTML基础之HTML常用标签

    下面小编为大家整理一些HTML的常用标签 a.布局标签 div标签定义文档中的分区或节(division/section),可以把文档分割为独立的.不同的部分,主要用于布局. aside标签的内容可用 ...

  9. Python基础——细琐知识点

    注释 Python注释有两种方式 使用# 类似于Shell脚本的注释方式,单行注释 使用'''或者""" 使用成对的'''或者""".这种注 ...

  10. Linux——发行版

    主流发行版 1. Red Hat Linux Red Hat 公司一直是Linux 乃至开源世界的领导者.其有两个不同的发行版本: 一个商用版,称为Red Hat Enterprise Linux,专 ...