题目地址: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. 解决第三方DLL没有强签名

    -----转载:http://blog.csdn.net/zyming0815/article/details/5939090 创建一个新的随机密钥对:sn -k myTest.snk 第一步: 将D ...

  2. (转)PHP正则表达式的快速学习方法

    1.入门简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或 ...

  3. big_table练习数据表

    big_table练习数据表 create table big_table as select rownum id, a.* from all_objects a / alter table big_ ...

  4. HTTP 状态响应码 意思详解/大全

    HTTP 状态响应码 意思详解/大全 转:http://blog.csdn.net/helin916012530/article/details/29842595 HTTP状态码(HTTP Statu ...

  5. macbook Android开发环境搭建,真机调试

    买了一台MacBook,本以为可以鼓捣一下iOS开发之类的,可惜导师要我做Android开发.无奈开始了在MacBook上开发Android的工作. 从开始配置环境到应用成功在真机上运行,也是曲曲折折 ...

  6. hive 桶相关特性分析

    1. hive 桶相关概念     桶(bucket)是指将表或分区中指定列的值为key进行hash,hash到指定的桶中,这样可以支持高效采样工作.     抽样( sampling )可以在全体数 ...

  7. Asp.net的IP地址屏蔽功能设计

    "IP地址的长度为32位,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用句点隔开." 由此我们了解到,IP地址实际上是一个32位正整数,在C#中可以使 ...

  8. 修改一行SQL代码 性能提升了N倍

    在PostgreSQL中修改了一行不明显的代码,把(ANY(ARRAY[...]) 改成 ANY(VALUES(...))),结果查询时间从20s变为0.2s.最初我们学习使用EXPLAN ANALY ...

  9. HTML语义化标签(二)

    为了保证网页去样式后的可读性,并且又符合web标准,应该注意一下几点: 1  尽可能少的使用无语义的标签div和span: 2  在语义不明显时,既可以使用div或者p时,尽量用p, 因为p在默认情况 ...

  10. ecshop json类的使用

    ecshop中有2个地方使用了json,一个是cls_json.php文件,一个是transport.js文件. cls_json 封装了json类,可以调用里面的encode的方法,根据参数不同,进 ...