题目链接 pog loves szh III

题意就是  求一个区间所有点的$LCA$。

我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c[i]$的反函数。

区间的$LCA$其实就是,$DFS$序最大和最小的两个点的$LCA$。

(话说$2017$女生赛里面有一题要用的结论和这题的差不多)

然后求出区间的$DFS$序最大值$x$和最小值$y$。

然后求一下$LCA(pc[x],pc[y])$即可。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 300010;
const int A = 21; int c[N];
int deep[N];
int f[N][A], g[N][A], st[N][A];
vector <int> v[N];
int ti;
int n, q;
int pc[N]; void ST(){
rep(i, 1, n) f[i][0] = c[i];
rep(j, 1, 20) rep(i, 1, n)
if ((i + (1 << j) - 1) <= n) f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]); rep(i, 1, n) g[i][0] = c[i];
rep(j, 1, 20) rep(i, 1, n)
if ((i + (1 << j) - 1) <= n) g[i][j] = max(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);
} inline int solvemin(int l, int r){
int k = (int)log2((double)(r - l + 1));
return min(f[l][k], f[r - (1 << k) + 1][k]); } inline int solvemax(int l, int r){
int k = (int)log2((double)(r - l + 1));
return max(g[l][k], g[r - (1 << k) + 1][k]);
} void dfs(int x, int fa, int dep){
c[x] = ++ti; pc[ti] = x;
deep[x] = dep;
if (fa){
st[x][0] = fa;
for (int i = 0; st[st[x][i]][i]; ++i) st[x][i + 1] = st[st[x][i]][i];
} for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x, dep + 1);
}
} int LCA(int a, int b){
if (deep[a] < deep[b]) swap(a, b);
for (int i = 0, delta = deep[a] - deep[b]; delta; delta >>= 1, ++i) if (delta & 1) a = st[a][i];
if (a == b) return a;
dec(i, 19, 0) if (st[a][i] != st[b][i]) a = st[a][i], b = st[b][i];
return st[a][0];
} int main(){ while (~scanf("%d", &n)){
rep(i, 0, n + 1) v[i].clear();
memset(c, 0, sizeof c);
ti = 0;
rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} memset(st, 0, sizeof st);
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
dfs(1, 0, 0);
ST();
for (scanf("%d", &q); q--; ){
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", LCA(pc[solvemax(x, y)], pc[solvemin(x, y)])); }
} return 0;
}

HDU 5266 pog loves szh III(区间LCA)的更多相关文章

  1. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  2. HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )

    pog loves szh III Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  3. HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)

    题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...

  4. HDU 5266 pog loves szh III 线段树,lca

    Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...

  5. HDU 5266 pog loves szh III (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...

  6. HDU 5266 pog loves szh III

    题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...

  7. hdu5266 pog loves szh III 【LCA】【倍增】

    Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...

  8. hdu 5265 pog loves szh II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 pog loves szh II Description Pog and Szh are pla ...

  9. hdu 5264 pog loves szh I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5264 pog loves szh I Description Pog has lots of stri ...

随机推荐

  1. Java使用Apache的HttpClient组件发送https请求

    如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...

  2. 使用Github第一节

    学习Github 1.目的: 借助github托管代码 2.基本概念(1): 仓库(Repository) 仓库用来存放项目代码,每个项目对应一个仓库,多个项目则对应多个仓库 收藏(Start) 收藏 ...

  3. Divisibility by 25 CodeForces - 988E

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  4. Nginx快速安装

    登录nginx官网,点击download 点击稳定版本 复制箭头上面的内容 vim /etc/yum.repos.d/nginx.repo 修改yum文件 将复制的内容粘贴上去 yum list | ...

  5. C++智能指针实现

    #include <iostream> #include <string> #define unsigned int size_t using namespace std; / ...

  6. Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)

    题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...

  7. Python学习-day4

    学习装饰器,首先听haifeng老师讲解了一下准备知识. 1.函数即变量 2.高阶函数+嵌套函数==>装饰器 装饰器的作用是在,1)不改变源代码,2)不改变原函数的调用方式的前提下为函数增加新的 ...

  8. Baum Welch估计HMM参数实例

    Baum Welch估计HMM参数实例 下面的例子来自于<What is the expectation maximization algorithm?> 题面是:假设你有两枚硬币A与B, ...

  9. Leetcode 452.用最少数量的箭引爆气球

    用最少数量的箭引爆气球 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐 ...

  10. struts转换器详解

    struts转换器:在B/S应用中,将字符串请求参数转换为相应的数据类型,是MVC框架提供的功能,而Struts2是很好的MVC框架实现者,理所当然,提供了类型转换机制. 一.类型转换的意义 对于一个 ...