HDU 5266 pog loves szh III(区间LCA)
题目链接 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)的更多相关文章
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- 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 ...
- HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- 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 ...
- HDU 5266 pog loves szh III (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...
- HDU 5266 pog loves szh III
题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python文件打包为exe可执行文件的方法
我自己常用Pyinstaller库打包 第一步: 安装pyinstaller库 pip install pyinstaller 第二步: 在py文件所在目录输入 mydemo.py是自己写的py文 ...
- uncaught exception 'NSInternalInconsistencyException, reason:[UITableViewController loadView] loaded the "Controller" nib but didn't get a UITableView
http://blog.csdn.net/ryantang03/article/details/7941058#reply 上面那篇文章是我查找的ios实现下拉刷新功能,在我下载完代码运行的过程中发现 ...
- 线段树: CDOJ1598-加帕里公园的friends(区间合并,单点更新)
加帕里公园的friends Time Limit: 3000/1000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Others) 我还有很 ...
- python网络编程相关
什么是网络套接字socket?简述基于tcp协议的套接字的通信流程. 为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了称为套接字 (Socket)的接口,区分 ...
- POJ-3278 广度优先搜索入门
#include<stdio.h> #include<stdlib.h> struct node{ int x; int s; }s[]; int main(){ ]={}; ...
- [转] 对 forEach(),map(),filter(),reduce(),find(),every(),some()的理解
1.forEach() 用法:array.forEach(function(item,index){}) 没有返回值,只是单纯的遍历 2.map() 用法:array.map(function(ite ...
- JSONP分享-- 在JavaScript中跨域请求
如果你正在开发一个现代的基于web的应用程序,那么你: 在客户端使用JavaScript. 需要集成那些没有完全在你控制之下的服务(或者那些来自不同的域). 在你的浏览器控制台中遇到过这个错误信息: ...
- day01_04.变量
变量的命名规则 变量名由字母小写a-z,大写A-Z,_下划线,数字0-9组成,php的变量名区分大小写;python的变量名也是区分大小写的 注意: PHP变量名必须以美元$符号开始; 变量名开头可以 ...
- 鼠标在窗口中的坐标转换到 canvas 中的坐标
鼠标在窗口中的坐标转换到 canvas 中的坐标 由于需要用到isPointInPath函数,所以必须得将鼠标在窗口中的坐标位置转换到canvas画布中的坐标,今天发现网上这种非常常见的写法其 ...
- TOJ2680: 最大矩阵连乘次数
2680: 最大矩阵连乘次数 Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: 144 ...