HDU5266-pog loves szh III
题目
给出一棵\(n\)个点的树,从1到\(n\)编号,\(m\)次询问\({LCA} _{v\in[L,R]}\)。
\(n,m\le 3\times 10^5\)
分析
我的做法是直接对LCA进行倍增,即\(f[i][j]\)表示从\(i\)号点开始的\(2^j\)个点的LCA,\(O(n\log ^2 n)\)预处理\(O(\log n)\)查询(分成前后两段,类似RMQ问题中ST表的做法)。
实际上还有复杂度更低的方法。
求一大堆点的共同LCA其实就是求其中dfn序最小和最大的点的LCA。直观的证明如下。取得询问点的中dfn序最小的那个,设为\(x\),另一个点\(v\)点的位置有两种情况:
- \(v\)在\(x\)的子树内(能满足\(dfn_v>dfn_x\)),那么他们的LCA就是\(x\)
- \(v\)在\(x\)的子树外,那么它必定在\(x\)的某一个祖先的子树内。这个祖先越往上,\(dfn_v\)就越大。
综上,一堆点的LCA为其中dfn序最小和最大的两点的LCA。
于是这个问题就变成了一个每次得到dfn序的极值点,求一次LCA的了。可以用线段树方便地实现。复杂度为\(O((n+m)\log n)\)。
代码
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
#define M(x) memset(x,0,sizeof x)
using namespace std;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=3e5+1;
const int maxj=19;
int n,st[maxn][maxj],bin[maxn];
namespace tree {
vector<int> g[maxn];
int top[maxn],size[maxn],son[maxn],dep[maxn],fat[maxn];
void clear(int n) {
for (int i=1;i<=n;++i) g[i].clear();
M(top),M(size),M(son),M(dep);
}
void add(int x,int y) {g[x].push_back(y);}
int dfs(int x,int fa) {
int &sz=size[x]=1,&sn=son[x]=0;
dep[x]=dep[fat[x]=fa]+1;
for (int v:g[x]) if (v!=fa) {
sz+=dfs(v,x);
if (size[v]>size[sn]) sn=v;
}
return sz;
}
void Top(int x,int fa,int tp) {
top[x]=tp;
if (son[x]) Top(son[x],x,tp);
for (int v:g[x]) if (v!=fa && v!=son[x]) Top(v,x,v);
}
int lca(int x,int y) {
for (;top[x]!=top[y];dep[top[x]]>dep[top[y]]?x=fat[top[x]]:y=fat[top[y]]);
return dep[x]<dep[y]?x:y;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
while (~scanf("%d",&n)) {
tree::clear(n);
for (int i=2;i<=n;++i) bin[i]=bin[i>>1]+1;
M(st);
for (int i=1;i<n;++i) {
int x=read(),y=read();
tree::add(x,y),tree::add(y,x);
}
tree::dfs(1,1);
tree::Top(1,1,1);
for (int i=1;i<=n;++i) st[i][0]=i;
for (int j=1;j<maxj;++j) for (int i=1;i<=n;++i) {
st[i][j]=st[i][j-1];
if ((i+(1<<(j-1)))<=n) st[i][j]=tree::lca(st[i][j],st[i+(1<<(j-1))][j-1]);
}
int m=read();
while (m--) {
int l=read(),r=read();
int len=r-l+1,d=bin[len];
int ans=tree::lca(st[l][d],st[r-(1<<d)+1][d]);
printf("%d\n",ans);
}
}
return 0;
}
HDU5266-pog loves szh III的更多相关文章
- 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 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)
题目链接 pog loves szh III 题意就是 求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...
- 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
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 ...
- HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- 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 ...
随机推荐
- 存一下emacs配置
(global-set-key [f9] 'compile-file)(global-set-key [f10] 'gud-gdb)(global-set-key (kbd "C-z&quo ...
- Struts 2(三):示例→基于Struts 2的用户注册模块
示例→基于Struts2的用户注册模块 1.用户注册模块需求描述 在用户注册页面中填写用户信息,包括用户名.用户密码.确认密码.姓名等信息,填写完成后提交注册表单给Struts 2的业务控制器Acti ...
- Unity ScriptableObject自定义属性显示
1. 继承Editor,重写OnInspectorGUI方法 Editor官方文档 需求 将TestClass中intData属性和stringData按指定格式显示. 实现 定义一个测试类TestC ...
- 广东ACM省赛 E题
题意: 输入一个P 使得存在一个一个N大于等于P, 并且存在m 等于 m/n * (m-1)/(n-1)=1/2. 思路 此题可以利用佩尔方程求解, 也可以打表解决.本次我解决利用的是佩尔方程(其实也 ...
- CDQ分治_占坑
准备系统地学习一波CDQ分治,持续更新中... 首先,CDQ分治也还是分治的一种,只不过普通分治是独立的解决两个子问题,而CDQ分治还要计算第一个子问题对于第二个的影响. CDQ分治几乎都是用来解决多 ...
- 算法笔记(c++)--使用一个辅助栈排列另一个栈
算法笔记(c++)--使用一个辅助栈排列另一个栈 仅仅使用一个辅助栈,不使用其他数据结构来排列一个栈,要求,上大下小. 分析下.肯定是先吧主栈中的数据都放到辅助栈中,在辅助栈中上小下大. 1.首先循环 ...
- xpath抓取的值有\r\n\t时,去掉的方法
解决办法: normalize-space() 例子: 原来的xpath为: user=selector.xpath('//*[@id="Con"]/tr[1]/th/text() ...
- 你应该知道的PHP库
Libchart – 这也是一个简单的统计图库. JpGraph – 一个面向对象的图片创建类. Open Flash Chart – 这是一个基于Flash的统计图. RSS 解析 解释RSS并是一 ...
- Minimum Sum of Array(map迭代器)
You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...
- Python:字符串操作总结
所有标准的序列操作(索引.分片.乘法.判断成员资格.求长度.取最小值最大值)对字符串同样适用,且字符串是不可变的. 一.字符串格式化 转换说明符 [注]: 这些项的顺序至关重要 (1)%字符:标记转换 ...