题意

题目链接

Sol

从上午九点淦到现在qwq

思路比较简单,就是把每次加入的一坨点看成一个,然后直接倍增搞。。

然后慢慢调就可以了。。。

最后数量级会到达\(10^{10}\),所以应该开long long

#include<bits/stdc++.h>
#define Pair pair<LL, LL>
#define MP make_pair
#define fi first
#define se second
#define LL long long
#define int long long
using namespace std;
const int MAXN = 1e5 + 10, B = 17, SS = 7e6 + 10;
inline LL read() {
char c = getchar(); LL x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, Q;
vector<int> v[MAXN];
int siz[MAXN], top[MAXN], son[MAXN], fa[MAXN], ID[MAXN], rev[MAXN], tim;
LL dep[MAXN];
void dfs1(int x, int _fa) {
siz[x] = 1; dep[x] = dep[_fa] + 1; fa[x] = _fa;
ID[x] = ++tim; rev[tim] = x;
for(auto &to : v[x]) {
if(to == _fa) continue;
dfs1(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void dfs2(int x, int topf) {
top[x] = topf;
if(!son[x]) return ;
dfs2(son[x], topf);
for(auto &to : v[x]) {
if(top[to]) continue;
dfs2(to, to);
}
}
int LCA(int x, int y) {
while(top[x] ^ top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
return y;
}
LL GetDis(int x, int y) {
int lca = LCA(x, y);
return dep[x] + dep[y] - 2 * dep[lca];
}
int tot;
struct Ope {
int l, r, id, fa, ti;//id所在的根节点是哪个,fa连到了大树哪个节点下面 ti第几次操作
bool operator < (const Ope &rhs) const {
return r < rhs.r;
}
}md[MAXN];
set<Ope> lin;
int root[SS], si[SS], ls[SS], rs[SS], cnt;
void insert(int &k, int pre, int l, int r, int v) {
k = ++cnt; ls[k] = ls[pre]; rs[k] = rs[pre]; si[k] = si[pre] + 1;
if(l == r) return;
int mid = (l + r) >> 1;
if(v <= mid) insert(ls[k], ls[pre], l, mid, v);
else insert(rs[k], rs[pre], mid + 1, r, v);
}
int Query(int tl, int tr, int l, int r, int k) {
if(l == r) return l;
int cur = si[ls[tr]] - si[ls[tl]], mid = (l + r) >> 1;
if(cur >= k) return Query(ls[tl], ls[tr], l, mid, k);
else return Query(rs[tl], rs[tr], mid + 1, r, k - cur);
}
int Kth(int x, int k) {//以x节点为根的子树中第k小的节点
int l = ID[x], r = ID[x] + siz[x] - 1;
int tmp = Query(root[l - 1], root[r], 1, N, k);
//printf("%d %d %d\n", x, k, tmp);
return tmp;
}
Pair GetId(int x) {//大树中编号为x节点对应的小树中的节点编号,以及该节点被加入的时间
Ope nl = *lin.lower_bound((Ope){0, x, 0, 0, 0});
return {Kth(nl.id, x - nl.l + 1), nl.ti};
}
vector<int> V[MAXN];
int Fa[MAXN][B + 1];
LL Dep[MAXN], Dis[MAXN][B + 1], t[MAXN][B + 1];
void Dfs(int x, int fa) {
Dep[x] = Dep[fa] + 1;
for(auto &to : V[x]) {
if(to == fa) continue;
Dfs(to, x);
}
}
void Pre() {
for(int j = 1; j <= B; j++)
for(int i = 1; i <= M; i++) {
Fa[i][j] = Fa[Fa[i][j - 1]][j - 1];
t[i][j] = t[Fa[i][j - 1]][j - 1];
Dis[i][j] = Dis[i][j - 1] + Dis[Fa[i][j - 1]][j - 1];
}
}
LL calc(int x) {//计算大树中编号为x的节点到所在大节点的根的距离
Pair tmp = GetId(x);
return dep[tmp.fi] - dep[md[tmp.se].id];
}
LL Query(LL x, LL y) {//大树中编号为x, y的节点的距离
Pair bx = GetId(x), by = GetId(y);
if(bx.se == by.se) return GetDis(bx.fi, by.fi);
LL prex = x, prey = y, gx, gy;
x = bx.se; y = by.se;
if(Dep[x] < Dep[y]) swap(x, y), swap(prex, prey);
LL ans = calc(prex);
for(int i = B; ~i; i--)
if(Dep[Fa[x][i]] >= Dep[y]) {
if(Fa[x][i] == y) {
gx = GetId(t[x][i]).fi;
gy = GetId(prey).fi;
return ans + Dis[x][i] + GetDis(gx, gy) - (dep[gx] - dep[md[Fa[x][i]].id]);
}
ans += Dis[x][i], x = Fa[x][i];
}
// if(x == y) return ans - 2 * calc(prey);
for(int i = B; ~i; i--)
if(Fa[x][i] != Fa[y][i]) {
ans += Dis[x][i]; ans += Dis[y][i];
x = Fa[x][i], y = Fa[y][i];
}
gx = GetId(md[x].fa).fi, gy = GetId(md[y].fa).fi;
return ans + 2 + GetDis(gx, gy) + calc(prey);
}
signed main() {
//freopen("tree13.in", "r", stdin);freopen("b.out", "w", stdout);
N = read(); M = read() + 1; Q = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
dfs1(1, 0);
dfs2(1, 1);//树剖
for(int i = 1; i <= N; i++) insert(root[i], root[i - 1], 1, N, rev[i]);
md[1] = {1, siz[1], 1, 0, 1};
lin.insert(md[1]);
tot = siz[1] + 1;
for(int i = 2; i <= M; i++) {
int x = read(), to = read();
md[i] = {tot, tot + siz[x] - 1, x, to, i};
lin.insert(md[i]);
tot += siz[x];
}
for(int i = 2; i <= M; i++) {
Ope x = md[i];
int u = x.ti, v = GetId(x.fa).se;//大树以操作次序来标号
V[v].push_back(u);
Dis[u][0] = dep[GetId(md[u].fa).fi] - dep[md[v].id] + 1;
Fa[u][0] = v;
t[u][0] = md[u].fa;
}
Dfs(1, 0);
Pre(); while(Q--) {
LL x = read(), y = read();
cout << Query(x, y) << '\n';
}
return 0;
}

洛谷P3248 [HNOI2016]树(主席树 倍增 )的更多相关文章

  1. 洛谷P2617 Dynamic Rankings (主席树)

    洛谷P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a ...

  2. 洛谷P3567 KUR-Couriers [POI2014] 主席树/莫队

    正解:主席树/莫队 解题报告: 传送门! 这题好像就是个主席树板子题的样子,,,? 毕竟,主席树的最基本的功能就是,维护一段区间内某个数字的个数 但是毕竟是刚get到主席树,然后之前做的一直是第k大, ...

  3. 洛谷P3567[POI2014]KUR-Couriers(主席树+二分)

    题意:给一个数列,每次询问一个区间内有没有一个数出现次数超过一半 题解: 最近比赛太多,都没时间切水题了,刚好日推了道主席树裸题,就写了一下 然后 WA80 WA80 WA0 WA90 WA80 ?? ...

  4. 洛谷P3567 [POI2014]KUR-Couriers 主席树

    挺裸的,没啥可讲的. 不带修改的主席树裸题 Code: #include<cstdio> #include<algorithm> using namespace std; co ...

  5. 洛谷$P3302$ 森林 $[SDOI2013]$ 主席树

    正解:主席树 解题报告: 传送门! 口胡一时爽代码火葬场 这题想法不难,,,但显然的是代码应该还挺难打的 但反正我也不放代码,就写下题解趴$QwQ$ 第一问就是个$Count\ on\ a\ tree ...

  6. 洛谷P4602 [CTSC2018]混合果汁(主席树)

    题目描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 nn 种果汁,编号为 0,1,\cdots,n-10,1,⋯,n−1 . ii 号果汁的美味度是 d_idi​ ,每升价格为 p_ipi ...

  7. 洛谷P2617 Dynamic Rankings 主席树 单点修改 区间查询第 K 大

    我们将线段树套在树状数组上,查询前预处理出所有要一起移动的节点编号,并在查询过程中一起将这些节点移到左右子树上. Code: #include<cstdio> #include<cs ...

  8. 洛谷4137 mex题解 主席树

    题目链接 虽然可以用离线算法水过去,但如果强制在线不就gg了. 所以要用在线算法. 首先,所有大于n的数其实可以忽略,因为mex的值不可能大于n 我们来设想一下,假设已经求出了从0到n中所有数在原序列 ...

  9. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

随机推荐

  1. Swift5 语言指南(二十八) 高级运算符

    除了Basic Operators中描述的运算符之外,Swift还提供了几个执行更复杂值操作的高级运算符.这些包括C和Objective-C中您熟悉的所有按位和位移运算符. 与C中的算术运算符不同,S ...

  2. 【ElasticSearch】:Mapping相关

    Mapping 类似数据库中的表结构定义,主要作用如下: 定义Index下的字段名(Field Name). 定义字段类型,例如数值型.字符串型.布尔型等. 定义倒排索引相关配置,比如是否索引.记录p ...

  3. Mac下如何安装配置Homebrew

    Last login: Mon Aug 7 13:57:29 on consolexiashenbindeMacBook-Pro:~ xiashenbin$ ruby -e "$(curl ...

  4. linux上安装redis4.0.9

    redis安装从3.0的版本到现在4.0的版本,现在装一个4.0的版本供大家学习使用. 先yum安装gcc yum -y install gcc 已加载插件:fastestmirror, langpa ...

  5. JS脚本实现CSDN免登陆免关闭广告插件自动展开“阅读更多”内容

    最近在CSDN查资料,总是弹出以下弹窗,然后就自动跳转到登录页面,蛋疼! 于是重新捣腾了一下,修改了原来的脚本,最新的脚本代码如下: 温馨提示:在打开CSDN页面后立刻执行以下脚本即可免登陆免关闭广告 ...

  6. 不一样的日期、时间转换(moment.js)

    无意中遇到了一种很奇怪的日期格式,从接口中返回的日期是这样的,如 2018-02-06T11:59:22+08:00 .然而这却不是我们想要的,我们要的是这种,YYYY-MM-DD HH:mm:ss. ...

  7. Mxonline3.6 在阿里云服务器上的部署(uwsgi nginx)

    我的项目结构 1. 执行`python manage.py migrate`命令,将迁移文件,映射到数据库中,创建相应的表. 进入数据库 use mxonline数据库     source /hom ...

  8. 继承extends、super、this、方法重写overiding、final、代码块_DAY08

    1:Math类的随机数(掌握) 类名调用静态方法.  包:java.lang 类:Math 方法:public static double random(): Java.lang包下的类是不用导包就可 ...

  9. Strom的trident小例子

    上代码: public class TridentFunc { /** * 类似于普通的bolt */ public static class MyFunction extends BaseFunct ...

  10. (转)Python 实现双向链表(图解)

    原文:https://blog.csdn.net/qq490691606/article/details/49948263 Python 实现双向链表(图解)双向链表双向链表也叫双链表,是链表的一种, ...