题目地址:HDU 5266

这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够。那么用线段树或者RMQ维护一下区间最值就能够了。然后就是找dfs序最大的点和dfs序最小的点的近期公共祖先了。

代码例如以下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
#define root 1, n, 1
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#pragma comment(linker, "/STACK:1024000000")
const int mod=1e4+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=300000+10;
int fir[MAXN], F[MAXN<<1], tot, deg[MAXN], rmq[MAXN<<1];
int head[MAXN], cnt, n;
int dp[MAXN<<1][30];
int Max[MAXN<<2], Min[MAXN<<2], q_max, q_min;
struct node
{
int u, v, next;
}edge[MAXN<<1];
void add(int u, int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void init()
{
memset(head,-1,sizeof(head));
cnt=tot=0;
}
void dfs(int u, int dep, int fa)
{
F[++tot]=u;
rmq[tot]=dep;
fir[u]=tot;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue ;
dfs(v,dep+1,u);
F[++tot]=u;
rmq[tot]=dep;
}
}
struct ST
{
int i, j;
void init()
{
for(i=1;i<=tot;i++) dp[i][0]=i;
for(j=1;(1<<j)<=tot;j++){
for(i=1;i<=tot-(1<<j)+1;i++){
dp[i][j]=rmq[dp[i][j-1]]<rmq[dp[i+(1<<j-1)][j-1]]?dp[i][j-1]:dp[i+(1<<j-1)][j-1];
}
}
}
int Query(int l, int r)
{
if(r<l) swap(l,r);
int k=0;
while((1<<k+1)<=r-l+1) k++;
return rmq[dp[l][k]]<rmq[dp[r+1-(1<<k)][k]]? dp[l][k]:dp[r+1-(1<<k)][k];
}
}st;
void PushUp(int rt)
{
Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
Min[rt]=min(Min[rt<<1],Min[rt<<1|1]);
}
void Build(int l, int r, int rt)
{
if(l==r){
Max[rt]=Min[rt]=fir[l];
return ;
}
int mid=l+r>>1;
Build(lson);
Build(rson);
PushUp(rt);
}
void Query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r){
q_max=max(q_max,Max[rt]);
q_min=min(q_min,Min[rt]);
return ;
}
int mid=l+r>>1;
if(ll<=mid) Query(ll,rr,lson);
if(rr>mid) Query(ll,rr,rson);
}
int main()
{
int q, u, v, l, r, i;
while(scanf("%d",&n)!=EOF){
init();
for(i=1;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1,0,-1);
st.init();
Build(root);
scanf("%d",&q);
while(q--){
scanf("%d%d",&u,&v);
if(u>v) swap(u,v);
q_min=INF;
q_max=0;
Query(u,v,root);
//printf("%d %d\n",q_min,q_max);
printf("%d\n",F[st.Query(q_min,q_max)]);
}
}
return 0;
}

HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)的更多相关文章

  1. 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 ...

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

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

  3. 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 ...

  4. HDU 5266 pog loves szh III(区间LCA)

    题目链接 pog loves szh III 题意就是  求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...

  5. HDU 5266 pog loves szh III

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

  6. HDU 5266 pog loves szh III (LCA)

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

  7. 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 ...

  8. 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 ...

  9. hdu 5264 pog loves szh I 水题

    pog loves szh I Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

随机推荐

  1. Web弹框类

    using System; using System.Text; namespace Core { /// <summary> /// MessageBox 的摘要说明. /// < ...

  2. 条件注释判断浏览器版本<!--[if lt IE 9]>(转载)

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![ ...

  3. Linux命令行下svn ignore忽略文件或文件夹用法

    一.忽略单个目录 1.忽略文件夹 假如目录oa.youxi.com是从svn checkout出来的,在服务器本地目录添加了material,但是不希望把material加入版本控制,因此我们需要忽略 ...

  4. cursor 与refcursor及sys_refcursor的区别 (转载)

    楼主标明是转载的,我只把我转载的地址发上来 http://www.cnblogs.com/honliv/archive/2011/07/21/2112639.html 显式是相对与隐式cursor而言 ...

  5. iOS横竖屏切换的一些坑(持续更新)

    最近在做视频类的App,遇到视频滚动播放的坑,紧接着就是横竖屏问题.之前太过天真不想做横竖屏配置.只是想旋转视频View,但是分享什么的包括AlertView还是竖屏样式,项目着急上线(1周提交一次也 ...

  6. 用core dump来调试程序段错误

    有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的.但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的调试变得困难起来 ...

  7. javascript写的ajax请求

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. win7 IIS7.0 【IIS 管理器无法验证此内置帐户是否有访问权】

    异常信息: 服务器配置为将传递身份验证和内置帐户一起使用,以访问指定的物理路径.但是,IIS 管理器无法验证此内置帐户是否有访问权.请确保应用程序池标识具有该物理路径的读取访问权.如果此服务器加入到域 ...

  9. WinPcap编程(二)

    0. 这一次具体讲抓包的两种方法. (建议)清除ARP表,最好自己写个批处理命令.快一点. 1.0 抓包步骤 步骤很简单:先打开适配器列表 --> 选择适配器 --> 通过遍历链表的方式到 ...

  10. Hybrid----优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案-备

    本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发送.接 ...