直接向原树加子树是不可能的
考虑重新建立这样一颗树,我们称之为 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. List集合转换为数组类型方法

    list集合转换为数组可以使用list集合的toArray(T[] a)方法, topicDetailsVo.setUrl(urls.toArray(new String[]{})); url是个数组 ...

  2. Inline Hook 钩子编写技巧

    Hook 技术通常被称为钩子技术,Hook技术是Windows系统用于替代中断机制的具体实现,钩子的含义就是在程序还没有调用系统函数之前,钩子捕获调用消息并获得控制权,在执行系统调用之前执行自身程序, ...

  3. luogu P4887 莫队二次离线

    珂朵莉给了你一个序列$a$,每次查询给一个区间$[l,r]$ 查询$l≤i<j≤r$,且$ai⊕aj$的二进制表示下有$k$个$1$的二元组$(i,j)$的个数.$⊕$是指按位异或. 直接暴力莫 ...

  4. codeforce 849D. Make a Permutation!

    D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. WPF打印控件内容

    当我们想打印控件内容时,如一个Grid中的内容,可以用WPF中PrintDialog类的PrintVisual()方法来实现 界面如下: XAML代码如下 <Grid> <Grid. ...

  6. C#汉字转换成全拼的拼音

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Hadoop Local(本地)模式搭建

    1. 下载压缩包 2. 配置环境变量 3. 配置Hadoop的JAVA_HOME路径 4. WordCount 1. 下载压缩包 下载Hadoop binary二进制压缩包 https://hadoo ...

  8. MacOS中创建Sublime Text3快捷方式返回Operation not permitted的原因及解决

    在类Unix系统中我们可以很随心的添加一些程序在终端里快捷方法,比如将一些常用的工具放在/usr/bin下面 Sublime Text3是一个小巧精致而又功能强大的程序,而且本猫也安装了Swift语言 ...

  9. drf-过滤组件|分页组件|过滤器

    目录 drf-过滤组件|分页组件|过滤器 群查接口各种筛选组件数据准备 drf过滤组件 搜索过滤组件 | SearchFilter 案例: 排序过滤组件 | OrderingFilter 案例: dr ...

  10. C#-调试记Log文件

    using System.IO; //捕获异常写入Log catch (Exception ex) { string msg = ex.Message + ex.StackTrace; string ...